Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scenario for validation rules in yii

I was wandering is there any chance to use scenario for rules,

in my model I have

public function rules()
{
    return array(
        array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling,
            postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery,
            cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required'),
        array('active', 'numerical', 'integerOnly'=>true),
    );
}

and in my view I have something like this

    <tr>
        <td>
            <p><?php echo $form->label($model,'telephoneBilling'); ?><span>:&nbsp;</span><span class="required">*</span></p>
        </td>
        <td>
            <?php echo $form->textField($model,'telephoneBilling'); ?>
            <?php echo $form->error($model,'telephoneBilling'); ?>
        </td>
    </tr>
</table>

<p><?php echo $form->checkBox($model,'active', array('class' => 'change')); ?>
    Delivery information: Please check the box if your delivery address differs from your billing address and enter the
    required delivery address in the fields provided below.</p>

    <div id="deliveryFormWrapper" style="display: none">
    <table class="cartReviewTable">
    <tr>
        <td colspan="4">
            <span class="blueTitle"><?php echo CHtml::encode(Yii::t('app', 'Delivery Information ')); ?></span>
        </td>
    </tr>
    <tr>
        <td>
            <p><?php echo $form->label($model,'firstNameDelivery'); ?><span>:&nbsp;</span><span class="required">*</span></p>
        </td>
        <td>
            <?php echo $form->textField($model,'firstNameDelivery'); ?>
            <?php echo $form->error($model,'firstNameDelivery'); ?>
        </td>

This is just a part to give you a picture what i do, so when i click on checkbox i show this hidden div, and he has a rules for his fields (the first div contains Billing fields, and the hidden contains delivery fields.

When i want to submit the form and the checkbox is not selected, i cant do it, because of required fields, so i was wandering Is there any chance to use scenario for that situation and how, i need a clue.

Thanks, i hope you can understand my question.

like image 643
Ime Prezime Avatar asked Jul 02 '13 07:07

Ime Prezime


1 Answers

Yes its possible. In your controller you can check if checkbox checked or no, then set scenario. Something like that

  if($_POST['my_checbox']==1)
   $model->setscenario('checked');  

Then just do $model->validate() to check for errors. In your model rules just set validators for scenarios you have:

array('delivery, firstNameBilling, lastNameBilling, addressBilling, cityBilling, countryBilling,
            postBilling, telephoneBilling, mailBilling, firstNameDelivery, lastNameDelivery, addressDelivery,
            cityDelivery, countryDelivery, postDelivery, telephoneDelivery, mailDelivery', 'required','on'=>'checked'),

Thats all. Pretty simple.

like image 109
ineersa Avatar answered Nov 05 '22 22:11

ineersa