php - How to add required field validation to custmizable field in YII2 (Advanced Template)? -


i have insert form specific model , required fields validations works through rules function in model. want add field in form table , give required validation. how this?

consider following example

contact.php // model1

... class contact extends model {     public function rules()     {         return [             ['contact_name', 'string', 'required'],             // other attributes         ];     }     ... 

users.php // model2

... class users extends model {     public function rules()     {         return [             ['user_name', 'string', 'required'],             // other attributes         ];     }     ... 

contactcontroller.php

... use \app\models\users; ... class contactcontroller extends controller {     public function actioncreate()     {         $contact_model = new contact;         $users_model = new users;         if($contact_model->load(yii::$app->request->post()) && $users_model->load(yii::$app->request->post()))         {             // saving code         }         else         {             return $this->render('create', ['contact_model'=>$contact_model, 'users_model'=>$users_model]);         }     }     ... 

in views/contact/_form.php

... <?php $form = activeform::begin(); ?>          <?= $form->field($contact_model, 'contact_name')->textinput(['maxlength' => 255]) ?>          <?= $form->field($user_model, 'user_name')->textarea(['rows' => 6]) ?>          <!-- other inputs here -->          <?= html::submitbutton($contact_model->isnewrecord ? yii::t('app', 'create')              : yii::t('app', 'update'), ['class' => $contact_model->isnewrecord              ? 'btn btn-success' : 'btn btn-primary']) ?>          <?= html::a(yii::t('app', 'cancel'), ['article/index'], ['class' => 'btn btn-default']) ?>  <?php activeform::end(); ?> ... 

here input 2 different models validated too, , make sure both input in same 1 form.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -