I want to make some of my fields compulsory when I select a checkbox.
Right now my fields are compulsory through the model.
I do not understand how I will make it compulsory when check box is selected
I want to make some of my fields compulsory when I select a checkbox.
Right now my fields are compulsory through the model.
I do not understand how I will make it compulsory when check box is selected
Any Ideas?
Ok, suppose i have two employee id (1 and 2),
now if i select 2 , the field1 and field2 will be required, but not for 1. To do this, follow the code below,
In your model:
public function rules() {
return array(
array('field1, field2', 'required','on'=>'rq'),
array('field1, field2', 'safe', 'on' => 'search'),
);
}
In your controller:
public function actionCreate() {
$model = new Users;
$this->performAjaxValidation($model);
if (isset($_POST['Users'])) {
$model->attributes = $_POST['Users'];
$model->scenario = $model->employee_id=='2' ? 'rq':'';
if ($model->save())
$this->redirect(array('view', 'id' => $model->id));
}
$this->render('create', array(
'model' => $model,
));
}
In your view/_form.php:
<?php echo $form->labelEx($model, 'field1'); ?>
<?php echo $form->textField($model, 'field1', array('size' => 60, 'maxlength' => 255)); ?>
<?php echo $form->error($model, 'field1'); ?>
<?php echo $form->labelEx($model, 'field2'); ?>
<?php echo $form->passwordField($model, 'field2', array('size' => 60, 'maxlength' => 255)); ?>
<?php echo $form->error($model, 'field2'); ?>
<?php echo $form->labelEx($model, 'employee_id'); ?>
<?php
echo $form->dropDownList($model, 'employee_id', CHtml::listData(Employee::model()->findAll(), 'id', 'name'), array(
'prompt' => 'Select One',
));
?>
<?php echo $form->error($model, 'employee_id'); ?>
Thanks this is working but I have 2 scenarios
$model->scenario = $model->employee_id=='2' ? 'rq':'';
$model->scenario = $model->employee2_id=='2' ? 'rq2':'';
The scenario is being assigned to the last one that is rq2 it is not taking rq
Did you tried this?
public function rules() {
return array(
array('field1', 'required','on'=>'rq'),
array('field2', 'required','on'=>'rq2'), array('field1, field2', 'safe', 'on' => 'search'),
);
}
public function actionCreate() {
$model = new Users;
$this->performAjaxValidation($model);
if (isset($_POST['Users'])) {
$model->attributes = $_POST['Users'];
$model->scenario = $model->employee_id=='1' ? 'rq':'';
$model->scenario = $model->employee_id=='2' ? 'rq2':'';
if ($model->save())
$this->redirect(array('view', 'id' => $model->id));
}
$this->render('create', array(
'model' => $model,
));
}
not tested… if work please inform me, thanks