简体   繁体   中英

Yii framework - Different validation rules depending on selected options

Is it possible to create model rules that are dependent from selection?

I have a model "Deposit" which is used to enter the money transfer details..I have drop down list with two possible choices "Cash Transfer","Cheque Transfer", and i have fields cash_deposit_date,bank_name.. which is required only for cash transfer and cheque_date, cheque_no and in_favour_of.. which is required only at the time of cheque transfer.. how can i do that..?

I know i can use scenarios like this,

$model=new Deposit("cash_transfer");

or

$model=new Deposit("cheque_transfer");

but how can I change scenario depending on the value selected in dropdown list?

Couldn't you just do this:

Generated HTML:

<form>
....
<select name="scenario">
    <option value="cash_transfer">Cash Transfer</option>
    <option value="cheque_transfer">Checque Transfer</option>
</select>
....
</form>

Code:

// allow only lowercase letters and underscore
$scenario = preg_replace('/[^a-z_]/', '', $_POST['scenario']);

if (!empty($scenario)) {
    $model = new Deposit($scenario);
} else {
    die('Missing scenario!');
}

Extending from jupaju answer,

That dropdown list also a model field(transfer_type), so I have done something like this.. After checking POST values are set, I use $model->scenario = $model->transfer_type==1 ? 'cash_transfer':'cheque_transfer' $model->scenario = $model->transfer_type==1 ? 'cash_transfer':'cheque_transfer' to change the scenario.

My code is

$model=new Deposit;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Deposit']))
{
    $model->attributes=$_POST['Deposit'];
    $model->scenario = $model->transfer_type==1 ? 'cash_transfer':'cheque_transfer';    
    if($model->save())
        $this->redirect(array('admin'));
}
$this->render('create',array('model'=>$model));

Now it is working..

This is more simple form me

In your model put:

public $pay_type;
/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        ...
        array('pay_type', 'required'),
        array('cash_deposit_date', 'validationTransfer'),
        ...

    );
}

public function validationTransfer($attribute,$params)
{
     // this a sample, put all your need
     if($this->pay_type=='cash_transfer' and $this->cash_deposit_date==='')
          $this->addError('cash_deposit_date','If you will pay to Cash Transfer, enter your cahs deposit date');

     // this a sample, put all your need
     if($this->pay_type=='cheque_transfer' and $this->cheque_date==='')
          $this->addError('cheque_date','If you will pay to Cheque Transfer, enter your cheque date');

     // this a sample, put all your need
     if($this->pay_type=='cheque_transfer' and $this->cheque_no==='')
          $this->addError('cheque_no','If you will pay to Cheque Transfer, enter your Cheque No');

}

In the view into your widget form

<?php echo $form->labelEx($model,'pay_type',array('class'=>'control-label')); ?>
<?php echo $form->dropDownList($model,'pay_type',array("cash_transfer"=>"Cash Transfer","cheque_transfer"=>"Cheque Transfer"),array('class'=>'form-control','empty'=>'Pay Type...')); ?>
<?php echo $form->error($model,'pay_type',array('class'=>'help-block')); ?>

Note: css class like "help-block", "control-label", "form-control" on witget form are optionals, maybe you use Bootstrap 3 and it will looks good

I think the best way would be to use a personalized validation rule in your model and then check if the model's "scenario" property is set to one or other option.

You can read more about custom validation rules in here

Hope this helps. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM