I have added a field allergy in the user table. But after submitting the form it is not get inserted. I am new to Yii. I am not using any Gii generators since this is the existing code base. Is it possible to add the new field as I tried here or should I run any Yii generators (model,CRUD)
I changed the following areas.
\protected\views\userManagement\user.php
<?php echo $form->labelEx($model,'allergy'); ?>
<?php echo $form->error($model,'allergy'); ?>
\protected\models\User.php
In the attributeLabels() function:
'allergy' => 'Allergy',
In the search() function:
$criteria->compare('allergy',$this->allergy);
In the beforeSave() function:
if($this->allergy=='')
$this->setAttribute('allergy', NULL);
else
$this->setAttribute('allergy', $this->allergy);
Related
I am generating Models and CRUD for my database tables using giix in YII web framework, the thing is I want to change some of the attributes that showed to me but I dont know how ? I get into the code _FORM.php of the generated CRUD to one of the table and I knew the piece of code that I must change it to get a different attribute instead of one that shown to me without knowing why ?
<div class="row">
<?php echo $form->labelEx($model,'idEmployee'); ?>
<?php echo $form->dropDownList($model, 'idEmployee', GxHtml::listDataEx(Employee::model()->findAllAttributes(null, true))); ?>
<?php echo $form->error($model,'idEmployee'); ?>
</div><!-- row -->
in the previous code the form showed a drop-down list from another table jointed with the current table according to idEmployee, he's showing an attribute that I dont want, I want to know how to render the FirstName and the LastName in the drop-down list, any help please ?
I believe it is easier when you just create your own dropdown list provider
in the Employee.php you add these two functions:
public function getFullName()
{
return $this->first_name.' '.$this->last_name; // or what ever you want to be shown on the drop list
}
public static function getNamesList() {
return CHtml::listData(self::model()->findAll(), 'idEmployee', 'fullName');
}
in the _FORM.php write:
<div class="row">
<?php echo $form->labelEx($model,'idEmployee'); ?>
<?php echo $form->dropDownList($model, 'idEmployee', Employee::getNamesList()); ?>
<?php echo $form->error($model,'idEmployee'); ?>
</div><!-- row -->
Hello and thanks for reading my question. I have a typical list view:
<?php $this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'emptyText'=>'No Jobs',
)); ?>
In my _view file I have a div and a button that slideToggles the div. If I just put the Javascript at the top of the page, it does not work because the results are dynamic and the name of the div changes with the id returned, eg:
id="detailsDiv-<?php echo $data->id_employer_contract;?>"
The problem is in my Javascript, which is as follows:
<?php Yii::app()->clientScript->registerScript('details', "$('#details-$data-id_employer_contract').click(function(){
$('#detailsDiv-$data->id_employer_contract').slideToggle();
return false;});");?>
How can I make this Javascript code dynamic? Meaning, how can I loop through the id? I tried adding the code to the listview property ajaxUpdate but it's still not working. Can someone tell me how I can loop a Javascript in a list view?
Add the id to your toggle buttons as data attribute:
<button class="toggleDetails" data-id="<?php echo $data->id_employer_contract ?>">
Then you can access these data attributes like this js:
<?php Yii::app()->clientScript->registerScript('toggleDetails', "
$('.toggleDetails').click(function(e){
var id = $(this).data('id');
$('#detailsDiv-' + id).slideToggle();
e.preventDefault();
});
", CClientScript::POS_READY) ?>
NOTE: You should not put this javascript into _view.php but into the main file where you render the List View. You only need this one single snippet to deal with all your buttons.
I have two models message and messageto and I am using message models attributes in the messageto form page how to validate those attributes using ajax validation, I am new to YII.
I am using application.extensions.tokeninput.TokenInput to display the fields and I am unable to validate fields on these widgets. Thank you waiting for your answer.
A good understanding of How-Yii-Ajax-Validation-Works will help you a lot in using this feature & do some customization to it.
I am afraid i did not use tokeninput extension but regarding your two models ajax validation, the following general plan should work:
in your View, make sure you have:
$form = $this->beginWidget('CActiveForm', array(
'id'=>'some-id-for-your-form',
'enableAjaxValidation'=>true //turn on ajax validation on the client side
));
Moreover in the View, any field with validation rule should have:
<?php echo $form->textField($model, 'some_attribute'); ?>
<?php echo $form->error($model, 'some_attribute'); ?> // This is used to present validations error
and in your Controller, in create or update action before you load View inputs through POST, put the following lines:
$messageModel = new Message;
$messageToModel = New MessageTo;
if(Yii::app()->getRequest()->getIsAjaxRequest())
{
echo CActiveForm::validate( array( $messageModel,$messageToModel));
Yii::app()->end();
}
/*
The rest of your code goes here
*/
As for the extension you are using, if its auto-generate the View code, then you need to know how to configure it to put the needed enableAjaxValidation => true & the $form->error($model,'some_attribute') Parts.
Hope this helped!!
I'm playing around with a small app in order to learn to use Yii.
I've created a small webapp with 2 models / tables: Projects and tasks. (1-to-many relationship, properly configured in the model classes).
I'm now trying to customize the Task/create view, replacing the text input field with a select box proposing the list of available projects.
I opened the form view and tried this:
<div class="row">
<?php echo $form->labelEx($model,'project_id'); ?>
<?php echo $form->textField($model,'project_id'); ?>
<?php
// my hack starts here
$projects = Project::model()->findAll();
$list = CHtml::listData($projects, 'id', 'name');
echo $form->listBox($model,'project_id','', $list); ?>
// my hack ends here
<?php echo $form->error($model,'project_id'); ?>
</div>
But it keeps throwing warnings or error (such as Invalid argument supplied for foreach(), and definitely does not work. I'm failing to understand what i'm doing wrong. Can you help ?
Your arguments are not in order (it should be):
$frameworks = Framework::model()->findAll();
$list = CHtml::listData($frameworks, 'id', 'name');
echo $form->listBox($model,'framework_id', $list,array());
Check the documentation
OK, i found it, thanks to Larry Ullman excellent tutorial.
Here it is:
<?php echo $form->dropDownList($model,'project_id', CHtml::listData(Project::model()->findAll(), 'id', 'name')); ?>
I can't figure out how to properly save checkbox values in Yii. I have a MySQL column, active, defined as a tinyint. I have the following form creation code, which correctly shows the checkbox as checked if the value is 1 and unchecked if 0:
<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active'); ?>
<?php echo $form->error($model,'active'); ?>
And the code to save the form correctly changes other, text-based values:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Thing']))
{
$model->attributes=$_POST['Thing'];
if($model->save())
$this->redirect(array('thing/index'));
}
$this->render('update',array(
'model'=>$model,
));
}
The value of active is not saved. Where am I going wrong?
You can use htmlOptions array to specify value attribute. Below is the code example:
<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active', array('value'=>1, 'uncheckValue'=>0)); ?>
<?php echo $form->error($model,'active'); ?>
Since version 1.0.2, a special option named 'uncheckValue' is
available that can be used to specify the value returned when the
checkbox is not checked. By default, this value is '0'.
(This text is taken from YII Documenration)
For every input that you are accepting from user, you need to define it in model::rule(). is active defined there in rule()?
In general, if you are having problems saving to the database, i would replace
$model->save();
with
if($model->save() == false) var_dump($model->errors);
that way, you can see exactly why it did not save. it is usually a validation error.
Please follow:
1. in protected/models/Thing.php add active as a numeric
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('active', 'numerical', 'integerOnly'=>true),
//OR optional
array('active', 'safe'),
);
}
Controller action: Its ok
View:
<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active', array('value'=>1, 'uncheckValue'=>0)); ?>
<?php echo $form->error($model,'active'); ?>
Hope this will work for you...
Article which can be helpful when figuring out how to handle booleans & checkboxes in Yii
http://www.larryullman.com/2010/07/25/handling-checkboxes-in-yii-with-non-boolean-values/
I used a bit type field in my DB and it didn't work.
1.- I changed the field type to tinyint
2.- In the rules function added:
array('active','numerical'),
3.-In the form (as D3K said) do:
<?echo $form->checkBox($model,'active',array('value'=>1, 'uncheckValue'=>0));?>
You can check by printing all the attributes which are being captured. If active is not captured, it must not be safe. you need to declare the variable as safe or define a rule around that variable. This will make the variable safe.
I have similar the same problemce before,I change data type is int,so it save
We can also add a rule as safe in model to pass the values from form to controller without missing.
array('active', 'safe'),
well this post is so old but I've found a solution very useful specially for giving checkbox a value specified rather than number. The new syntax is something like this
notice I'm using ActiveForm
field($model3, 'External_Catering')->checkbox(['id' => 'remember-me-ver', 'custom' => true,'value'=>"External_Catering", 'uncheckValue'=>"vide"]) ?>
1) where my model is =>model3
2) with the name External_Catering
3) that take the value External_Catering and empty when it's uncheckValue
4) in Controller you get the value just by specifying the model and it's attribute like
$External_Catering=$model3->External_Catering.