Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate tabular data in yii?
    text
    copied!<p>I have created a form which allows user to save tabular data. I have followed this <a href="http://www.yiiframework.com/doc/guide/1.1/en/form.table" rel="nofollow noreferrer">tutorial</a> . I have managed to add multiple instance for a Model and I am getting the data in the post and it is getting validate. The problem is with the error summary and the AJAX validation.</p> <p>Below is my controller code, I have created an array for the second Model and passed it to the form.</p> <pre><code>$model = new UserAccountDetail; $addresses = array(); array_push($addresses, new UserAddress); array_push($addresses, new UserAddress); $this-&gt;validateUserAccount($model,$addresses); if(isset($_POST['UserAccountDetail']) &amp;&amp; isset($_POST['UserAddress'])) { $model-&gt;attributes = $_POST['UserAccountDetail']; $model-&gt;validate(); $addresses = array(); for($i=0;$i&lt;2;$i++) { if(isset($_POST['UserAddress'][$i])) { $address = new UserAddress; $address-&gt;attributes = $_POST['UserAddress'][$i]; array_push($addresses, $address); $address-&gt;validate(); } } } $this-&gt;render('accountinformation',array('model'=&gt;$model,'addresses'=&gt;$addresses)); </code></pre> <p>Below is my ajax validation function:</p> <pre><code>protected function validateUserAccount($model,$addresses) { if(isset($_POST['ajax']) &amp;&amp; $_POST['ajax']==='user-account-detail-form') { echo CActiveForm::validate($model).CActiveForm::validateTabular($addresses); Yii::app()-&gt;end(); } } </code></pre> <p>When I run this code the Ajax validation does not work. The onclick validation does work but for the tabular data the messages are not shown in the error summary but the fields are highlighted in red.</p> <p>I any thing else is required then please let me know. Thanks for your time. Cheers!!!!!</p> <p>Update View File:</p> <pre><code>&lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'user-account-detail-form', 'enableAjaxValidation'=&gt;true, )); ?&gt; &lt;?php echo $form-&gt;errorSummary($model,$address); ?&gt; &lt;h2&gt; Account Details &lt;/h2&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'Title'); ?&gt; &lt;?php echo $form-&gt;dropDownList($model,'Title', $model-&gt;getAllTitles()); ?&gt; &lt;?php echo $form-&gt;error($model,'Title'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'firstName'); ?&gt; &lt;?php echo $form-&gt;textField($model,'firstName',array('size'=&gt;50,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($model,'firstName'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'middleName'); ?&gt; &lt;?php echo $form-&gt;textField($model,'middleName',array('size'=&gt;50,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($model,'middleName'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'lastName'); ?&gt; &lt;?php echo $form-&gt;textField($model,'lastName',array('size'=&gt;50,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($model,'lastName'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'displayName'); ?&gt; &lt;?php echo $form-&gt;textField($model,'displayName',array('size'=&gt;50,'maxlength'=&gt;200)); ?&gt; &lt;?php echo $form-&gt;error($model,'displayName'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'birthDate'); ?&gt; &lt;?php echo $form-&gt;textField($model,'birthDate',array('size'=&gt;50,'maxlength'=&gt;15)); ?&gt; &lt;?php echo $form-&gt;error($model,'birthDate'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'lenderType'); ?&gt; &lt;?php echo $form-&gt;textField($model,'lenderType',array('size'=&gt;50,'maxlength'=&gt;15)); ?&gt; &lt;?php echo $form-&gt;error($model,'lenderType'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'businessName'); ?&gt; &lt;?php echo $form-&gt;textField($model,'businessName',array('size'=&gt;60,'maxlength'=&gt;200)); ?&gt; &lt;?php echo $form-&gt;error($model,'businessName'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'hearAboutUs'); ?&gt; &lt;?php echo $form-&gt;dropDownList($model,'hearAboutUs', $model-&gt;getAllHearAbout()); ?&gt; &lt;?php echo $form-&gt;error($model,'hearAboutUs'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'promotionalCode'); ?&gt; &lt;?php echo $form-&gt;textField($model,'promotionalCode',array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($model,'promotionalCode'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'mobileNumber'); ?&gt; &lt;?php echo $form-&gt;textField($model,'mobileNumber',array('size'=&gt;50,'maxlength'=&gt;15)); ?&gt; &lt;?php echo $form-&gt;error($model,'mobileNumber'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($model,'workLandline'); ?&gt; &lt;?php echo $form-&gt;textField($model,'workLandline',array('size'=&gt;50,'maxlength'=&gt;15)); ?&gt; &lt;?php echo $form-&gt;error($model,'workLandline'); ?&gt; &lt;/div&gt; &lt;?php echo $form-&gt;textField($model,'thirdAnswer',array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($model,'thirdAnswer'); ?&gt; &lt;/div&gt; &lt;h2&gt;Address &lt;/h2&gt; &lt;?php foreach ($addresses as $i=&gt;$address) { ?&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]Flat"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]Flat",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]Flat"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]buildingName"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]buildingName",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]buildingName"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]buildingNumber"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]buildingNumber",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]buildingNumber"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]street"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]street",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]street"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]district"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]district",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]district"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]town"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]town",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]town"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]county"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]county",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]county"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]postCode"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]postCode",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]postCode"); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;labelEx($address,"[$i]isCorresppondence"); ?&gt; &lt;?php echo $form-&gt;textField($address,"[$i]isCorresppondence",array('size'=&gt;60,'maxlength'=&gt;100)); ?&gt; &lt;?php echo $form-&gt;error($address,"[$i]isCorresppondence"); ?&gt; &lt;/div&gt; &lt;?php } ?&gt; &lt;div class="row buttons"&gt; &lt;?php echo CHtml::submitButton('Continue'); ?&gt; &lt;/div&gt; &lt;?php $this-&gt;endWidget(); ?&gt; </code></pre> <p><strong>Update</strong> : I have checked the response from the server and it is giving the correct response. Below is the screen shot: <img src="https://i.stack.imgur.com/rWnCs.png" alt="enter image description here"></p> <p>Update :: I have managed to correct the on click validation issue by passing the array of models to the errorSummary() function like:</p> <pre><code>&lt;?php $error = array(); array_push($error, $model); foreach ($addresses as $address) { array_push($error, $address); } echo $form-&gt;errorSummary($error); ?&gt; </code></pre> <p>But the AJAX validation is still not working. Can any one help me with that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload