Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to put client side validation in TbActiveForm in Yii bootstrap?
    primarykey
    data
    text
    <p>I have created a form using widget -bootstrap.widgets.TbActiveForm with form elements like textFieldRow, TbTypeahead, CJuiDatePicker and TbButton. I have implemented ajax form submission and it works fine. And I have included a simple client side validation,the rules are defined in the EmployeeRegister model. Its a simple validation for empty fields only. The problem is that when I submit the form with required fields empty, the form get submitted. That is, no validation is performed. But it displays error message "Name cannot be blank." or "Address cannot be blank.". Can anyone help me to fix this issue?? I am attaching my code below. // MOdel- EmployeeRegister</p> <pre><code>class EmployeeRegister extends CActiveRecord{ public $name; public $address; public $position; public $joinDate; public $age; public $phone; public $search; public $id; private $_identity; public function rules(){ return array( array('name,address,position,phone','required'), array('joinDate,age,search,id','safe'), ); } public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'emp_registration'; } </code></pre> <p>//View- edit.php</p> <pre><code>$form = $this-&gt;beginWidget('bootstrap.widgets.TbActiveForm', array( 'id'=&gt;'employeeregister-form', //'enableAjaxValidation'=&gt;true, 'enableClientValidation'=&gt;true, 'clientOptions'=&gt;array('validateOnSubmit'=&gt;true), 'htmlOptions'=&gt;array('class'=&gt;'well',), )); ?&gt; &lt;?php echo $form-&gt;textFieldRow($model, 'name',array('class'=&gt;'span3','style'=&gt;'height:30px','id'=&gt;'emp_name')); ?&gt; &lt;?php echo $form-&gt;textFieldRow($model, 'address', array('class'=&gt;'span3','style'=&gt;'height:30px','id'=&gt;'emp_address')); ?&gt; &lt;?php // echo $form-&gt;textFieldRow($model, 'position', array('class'=&gt;'span3','value'=&gt;$result['position'],'style'=&gt;'height:30px')); ?&gt; &lt;?php echo "&lt;br/&gt; Position &lt;br/&gt;";?&gt; &lt;?php $this-&gt;widget('bootstrap.widgets.TbTypeahead', array( 'model'=&gt;$model, 'htmlOptions'=&gt;array('style'=&gt;'height:30px','id'=&gt;'emp_position'), 'attribute'=&gt;'position', 'options'=&gt;array( 'source'=&gt;array( 'Junior Software Engineer','Software Engineer','Designer'), 'items'=&gt;4, 'matcher'=&gt;"js:function(item) { return ~item.toLowerCase().indexOf(this.query.toLowerCase()); }", ))); ?&gt; &lt;?php //echo $form-&gt;textFieldRow($model, 'joinDate', array('class'=&gt;'span3','value'=&gt;$result['joinDate'],'id'=&gt;'datepicker')); ?&gt; &lt;?php echo "&lt;br/&gt; Join Date &lt;br/&gt;";?&gt; &lt;?php $this-&gt;widget('zii.widgets.jui.CJuiDatePicker',array( 'model'=&gt;$model, 'attribute'=&gt;'joinDate', //'value'=&gt;$result['joinDate'] ? $result['joinDate'] : "", // additional javascript options for the date picker plugin 'options'=&gt;array( 'showAnim'=&gt;'fold', 'dateFormat'=&gt;'yy-mm-dd', ), 'htmlOptions'=&gt;array( 'style'=&gt;'height:30px;','id'=&gt;'emp_date' ) )); ?&gt; &lt;?php echo $form-&gt;textFieldRow($model, 'age', array('class'=&gt;'span3','style'=&gt;'height:30px','id'=&gt;'emp_age')); ?&gt; &lt;?php echo $form-&gt;textFieldRow($model, 'phone', array('class'=&gt;'span3','style'=&gt;'height:30px','id'=&gt;'emp_phone')); ?&gt; &lt;?php echo $form-&gt;textFieldRow($model, 'id', array('class'=&gt;'span3','style'=&gt;'height:30px; display:none;','id'=&gt;'emp_id')); ?&gt; &lt;?php echo "&lt;br/&gt;";?&gt; &lt;?php $this-&gt;widget('bootstrap.widgets.TbButton', array('buttonType'=&gt;'ajaxSubmit', 'label'=&gt;'Update','type'=&gt;'primary','htmlOptions'=&gt;array('name'=&gt;'update_button',), 'url'=&gt;CController::createUrl('site/update'), 'ajaxOptions'=&gt;array( 'type'=&gt;'POST', 'dataType'=&gt;'json', 'data'=&gt;'js:$("#employeeregister-form").serialize()', 'success'=&gt;'js:function(data){ //$("#update_err").html(data); alert(data); }', 'async' =&gt; true, )); ?&gt; &lt;?php //echo $form-&gt;error($model,'name,address,position,phone'); ?&gt; &lt;?php $this-&gt;endWidget(); ?&gt; &lt;div id="update_err"&gt;&lt;/div&gt; &lt;?php echo CHtml::link('Back to List',array('site/index')); ?&gt; </code></pre> <p>//Controller</p> <pre><code>$model= new EmployeeRegister(); $model-&gt;attributes = $_POST['EmployeeRegister']; if(Yii::app()-&gt;getRequest()-&gt;getIsAjaxRequest()) { echo CActiveForm::validate( array( $model)); Yii::app()-&gt;end(); } $update_id= $model-&gt;id; $name = $model-&gt;name; $address = $model-&gt;address; $position = $model-&gt;position; $joinDate = $model-&gt;joinDate; $age = $model-&gt;age; $phone = $model-&gt;phone; if(EmployeeRegister::model()-&gt;updateByPk($update_id, array('id'=&gt;$update_id,'name'=&gt;$name,'address'=&gt;$address,'position'=&gt;$position,'joinDate'=&gt;$joinDate,'age'=&gt;$age,'phone'=&gt;$phone))) echo $name."'s details updated successfully"; else echo "Update failed"; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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