我有 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 tuias.</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");
}'
),
)); ?>
这篇关于嗯.如何添加css“错误"在表单提交上输入的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!