Hello,
I’m experiencing this error for several times, again and again.
Got some classic CForm model
class OrderForm extends CFormModel {
public $userId;
public $userLogin;
// bla bla bla
public function rules() {
return array(
array('userLogin', 'required', 'on'=>'insert'),
array('userId', 'required', 'on'=>'insert', 'skipOnError'=>true),
);
}
// bla bla bla
}
Some customized template
<?php $form=$this->beginWidget('CActiveForm'); ?>
<table id="usr">
<tr>
<th><?php echo Yii::t('models', 'Login') ?></th>
<td>
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array('name'=>'OrderForm[userLogin]',
'sourceUrl' => $this->createUrl('/orders/userSuggest'),
'options'=>array(
'minLength' => '2',
'select' => 'js:afterSelect',
),
)); ?>
<?php if ($form->error($orderModel, 'userLogin')) { ?>
<div class="error">
<?php echo $form->error($orderModel, 'userLogin'); ?>
</div>
<?php } ?>
<?php if ($form->error($orderModel, 'userId')) { ?>
<div class="error">
<?php echo $form->error($orderModel, 'userId'); ?>
</div>
<?php } ?>
</td>
</tr>
bla bla bla
Worth of note - in js:afterSelect I manualy create hidden field named userId. So by this I created auto suggesting form, which send back to server found userLogin and userId.
Controller:
public function actionCreate() {
$orderModel = new OrderForm();
if (Yii::app()->request->getIsPostRequest()) {
$orderModel->userId = (isset($_POST['OrderForm']['userId'])) ? $_POST['OrderForm']['userId'] : '';
$orderModel->userLogin = $_POST['OrderForm']['userLogin'];
if ($orderModel->validate()) {
// bla bla bla
}
}
$this->render('create', array(
'orderModel' => $orderModel,
));
}
Pretty classic stuff too. Problem is, when user sends form without filling user login. Both errors get triggered and user obtain two error messages. Because user submitted form without autosuggest "loop", userId field is not even sended to server (it is created in afterSend() callback of CUI). But Controller handle this just fine a fill empty string into OrderForm. But as the topic says - both validators get fired. Any ideas?