Custom scenario use default rules

Hi, I try to create a scenario to specific searchform; I defined the required attributes inside custom scenario, but when form validate submit, employee_id attribute show required. Employee_id its required in default scenario but I not set it in my custom scenario.

Atrribute: employee_id

[b]Common Model Rules:

[/b]


    public function rules()

    {

        return [

            [['rate_id', 'type', 'is_promo', 'employee_id', 'country_group_id'], 'required'],

    ...



[b]App Model Rules:

[/b]


    public function rules()

    {

        return array_merge(parent::rules(), [

                [['last_name', 'rental_confirmation_code','pickup_date', 'dropoff_date'], 'required', 'on' => self::SEARCH_RESERVATION],

                [['employee_id','gsa'], 'safe', 'on' => self::SEARCH_RESERVATION],


    ...

[b]I set scenario:

[/b]


        $model = new Reservation(['scenario' => Reservation::SEARCH_RESERVATION]);



View form:


<?= $form->field($model, 'employee_id')->dropDownList(ArrayHelper::map(\common\models\User::find()->where(['agency_id'=>$user->agency_id])->all(), 'id', 'username'),

                                            ['class' => 'form-control', 'prompt'=>'- Choose a User -']

                                        )->label(false) ?>

But view validate my the employee_id required (image included).

Its a Yii2 bug or I have any error?

Default rules (those without a specific scenario) are applied to all other scenarios as well.

You could use "except": http://www.yiiframework.com/doc-2.0/yii-validators-validator.html#$except-detail

Hi Patrick, I set the except rule but problem persist, Can I ignore default rule required?




    public function rules()

    {

        return array_merge(parent::rules(), [

                ['employee_id', 'required', 'except' => self::SEARCH_RESERVATION],

                [['last_name', 'rental_confirmation_code','pickup_date', 'dropoff_date'], 'required', 'on' => self::SEARCH_RESERVATION],


    ....



As long as you "array_merge", you will have the original rule in your rule set.

I recently used scenarios for the contact form, so I could use the same form to ‘contact us’ and send an email to employees.

In ContactForm model:


    /**

     * @inheritdoc

     */

    public function rules() {

        return [

            // name, email, subject and body are required

            [['name', 'email', 'subject', 'body'], 'required'],

            // email has to be a valid email address

            ['email', 'email'],

            // verifyCode needs to be entered correctly

            ['verifyCode', 'captcha'],

            //

            [['department'], 'safe'],

        ];

    }


    /**

     * @inheritdoc

     */

    public function scenarios() {

        $scenarios = parent::scenarios();

        $scenarios['employee'] = ['subject', 'body', 'verifyCode', 'department'];

        return $scenarios;

    }



In EmployeeController action (just a copy of the site/contact code with 1 addition)


    public function actionContact() {

        $model = new ContactForm();

        $model->scenario = 'employee';

        ...

    }



In the Contact Form I modify what is displayed in accordance with the scenario.

It seems that in the ‘employee’ scenario only the ‘subject’, ‘body’, ‘verifyCode’, and ‘department’ rules are enforced

That is what you specified. What behaviour do you expect?

I was explaining that only the attributes listed in the scenario are validated and not the others