CActiveForm验证的问题 [ 新手入门 ]
用CActiveForm的时候,如下代码所示。
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
正常使用的时候,如果验证不通过,$form->error($model,'firstName')
会输出一个错误,验证通过后,错误信息会隐藏。
现在需要在用户输入前在输入框后显示一个提示。验证不通过在输入框后报错。验证通过在输入框后显示一个绿色的小勾。
要实现如上所说该怎么做呢?CActiveForm能坐到这种效果么,请帮忙提供解决办法。
共 12 条回复
-
可以的:
<?php $form = $this->beginWidget('CActiveForm', array( 'id'=>'user-form', 'enableAjaxValidation'=>true, 'enableClientValidation'=>true, 'focus'=>array($model,'firstName'), )); ?>
-
http://www.yiiframework.com/doc/api/1.1/CActiveForm 文档里是这么说的:
What makes CActiveForm extremely useful is its support for data validation. CActiveForm supports data validation at three levels: server-side validation: the validation is performed at server side after the whole page containing the form is submitted. If there is any validation error, CActiveForm will render the error in the page back to user. AJAX-based validation: when the user enters data into an input field, an AJAX request is triggered which requires server-side validation. The validation result is sent back in AJAX response and the input field changes its appearance accordingly. client-side validation (available since version 1.1.7): when the user enters data into an input field, validation is performed on the client side using JavaScript. No server contact will be made, which reduces the workload on the server.
-
可以通过Ajax或客户端验证。
<? $form = $this->beginWidget('CActiveForm', array( 'id'=>$name, 'enableAjaxValidation'=>true, 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, 'validateOnChange'=>false, 'validateOnType'=>false ), 'focus'=>array($model,'subject') )); ?>
-
<div class="row"> <?php echo $form->labelEx($model,'firstName'); ?> <?php echo $form->textField($model,'firstName'); ?> <?php echo $form->error($model,'firstName',array( 'afterValidateAttribute'=>'js:function(form, attribute, data, hasError) { //属性验证后执行 if (hasError) jQuery("#LoginForm_username_em_").val(data[attribute.id]); //选择器id用FF可以查看 else jQuery("#LoginForm_username_em_").html("1111111").show(); //这里你就可以实现验证通过显示对勾图片 }')); ?> </div>
-
同时附上一篇老外的文章
http://www.yiiframework.com/forum/index.php/topic/35701-displaying-success-messages-in-cactiveform/page__p__171582__hl__cactiveform#entry171582
跳过前面所有内容,直接看如下部分'clientOptions'=>array('afterValidateAttribute'=>new CJavaScriptExpression("function(form, attribute, data, hasError){ var id=attribute.id; var eid=attribute.errorID; if(data[id].toString().substring(0,7)=='Success') { $('#'+eid+'.flash-error').css('display','none'); $('#'+id).parent().children('label.required').css('color','green'); $('#'+id).css('background','#E6EFC2'); } else { $('#'+eid+'.flash-success').css('display','none'); $('#'+id).parent().children('label.required').css('color','red'); $('#'+id).css('background','').addClass('error'); } }")), ));
-
附上文档说明
afterValidateAttribute: function, the function that will be invoked after performing ajax-based validation triggered by a single attribute input change. The expected function signature should be afterValidateAttribute(form, attribute, data, hasError) {...}, where 'form' is the jquery representation of the form object; 'attribute' refers to the js options for the triggering attribute (see error); 'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether there is any validation error. Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpressoin to prevent it from being encoded as a string. This option has been available since version 1.1.3.
hxppb
注册时间:2011-09-08
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱60
- 威望0
- 积分60