3
votes

I have a wizard form in my yii2 project. My form is type of yii2 active form. Now I want to validate each part separately. for example first section of wizard includes t1, t2, t3 and second section includes t4, t5. when user click the next step button of section 1 I want just verify t1, t2 and t3 but not t4 and t5. How should I do this in yii2?

Edit

Please notice that I want use yii2 validator and don't want to write validation rules in my jquery code again.

Edit: Please check the code

$wizard_config = [
    'id' => 'stepwizard',
    'steps' => [
        1 => [
            'title' => 'اطلاعات پروژه',
            'icon' => 'fa fa-info',
            'content' =>
                $form->field($model, 'title') .
                $form->field($model, 'language_id')->dropDownList($language, ['style' => 'padding-top: 0;', 'prompt' => 'زبان']) .
                $form->field($model, 'project_type_id')->dropDownList($project_type, ['style' => 'padding-top: 0;']) .
                $form->field($model, 'text_type_id')->dropDownList($text_type, ['style' => 'padding-top: 0;']) .
                $form->field($model, 'description')->textarea(['style'=>'height: 140px;resize:none;'])
            ,

        ],
        2 => [
            'title' => 'فایل های پروژه',
            'icon' => 'fa fa-upload',
            'content' =>
                $form->field($model, 'proj_file')->fileInput() .
                $form->field($model, 'pages') .
                $form->field($model, 'template')->fileInput()
            ,
        ],
        3 => [
            'title' => 'زمان و قیمت',
            'icon' => 'fa fa-clock-o',
            'content' =>
                $form->field($model, 'deadline_day') .
                $form->field($model, 'deadline_time') .
                $form->field($model, 'doing_way_id') .
                $form->field($model, 'user_id') .
                $form->field($model, 'pawn_money') .
                $form->field($model, 'is_group')
            ,
        ],
        4 => [
            'title' => 'تایید',
            'icon' => 'fa fa-check',
            'content' => '<h3>Step 3</h3>This is step 3',
        ],
    ],
    'start_step' => 1,
];
2
i think you need to take t1,t2,t3 in different div and t4,t5 in different div and validate as per divjilesh
I have did this buddy. Please explain more about your solution. @jileshBehzad Hassani
Please paste your code @BehzadHassani .Nana Partykar
@NanaPartykar please check the updateBehzad Hassani
there is an extension available with the name yii2-formwizardMuhammad Omer Aslam

2 Answers

2
votes

You can separate steps in model for different scenarios.

 public function scenarios()
{
    $scenarios = parent::scenarios();

    $scenarios['step1'] = ['login', 'username'];
    $scenarios['step2'] = ['t1', t2];

    return $scenarios;
}

Then assign scenario for model in each steps

 $user->scenario = 'step1';

and validate only part of model's attributes. You have to write ajax action, that can map step and scenario and return validation result.

2
votes

Here is the solution. Many thanks to Amin Cheloh and Alexander Makarov providing this article. for validating just one input of form. we should use this one line code.

$('#id-of-form').yiiActiveForm('validateAttribute', 'input id');