嗯.如何添加css“错误"在表单提交上输入的类?
我有 Yii 表格.某些字段是必需的.提交表单时,我需要将 CSS 类错误"添加到文本输入中.我的代码:
I have Yii form. Some fields are required. When form is submitted i need that CSS class "error" would be added to the text input. My Code:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<ul class="contact_form">
<li class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name', array('class'=>'input')); ?>
<?php echo $form->error($model,'name'); ?>
</li>
...
现在我只收到带有错误消息的 div:
Now i just get div with error message:
<div class="errorMessage" id="ContactForm_name_em_" style="">Laukelis „Vardas, pavardė" negali būti tuščias.</div>
如何将错误"类添加到输入字段?
How to add "error" class to input field?
推荐答案
要同时进行 AJAX 验证,您应该或多或少地添加如下内容:
To have AJAX validation also, you should add something more or less like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'afterValidate' => 'js:function(form, data, hasError) {
if(hasError) {
for(var i in data) $("#"+i).addClass("error_input");
return false;
}
else {
form.children().removeClass("error_input");
return true;
}
}',
'afterValidateAttribute' => 'js:function(form, attribute, data, hasError) {
if(hasError) $("#"+attribute.id).addClass("error_input");
else $("#"+attribute.id).removeClass("error_input");
}'
),
)); ?>
相关文章