$options=CJavaScript::encode($options);
$cs->registerCoreScript('yiiactiveform');
$id=$this->id;
$cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiactiveform($options);");这就是根据模型中的规则来添加js验证if($model instanceof CActiveRecord && !$model->isNewRecord) $option['status']=1;这段代码的意思就是 model必须继承CActiveRecord,但是追重要的是isNewRecord (我就是通过这个status从js追到php的), 然后在继续,到这里瞬间明白了,去看同事的代码,得出以下
Object::model(); isNewRecord = false new Object(); isNewRecord = true
/**
* list of attributes to be validated. Each array element is of the following structure:
* {
* id: 'ModelClass_attribute', // the unique attribute ID
* model: 'ModelClass', // the model class name
* name: 'name', // attribute name
* inputID: 'input-tag-id',
* errorID: 'error-tag-id',
* value: undefined,
* status: 0, // 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating
* validationDelay: 200,
* validateOnChange: true,
* validateOnType: false,
* hideErrorMessage: false,
* inputContainer: undefined,
* errorCssClass: 'error',
* successCssClass: 'success',
* validatingCssClass: 'validating',
* enableAjaxValidation: true,
* enableClientValidation: true,
* clientValidation: undefined, // function (value, messages, attribute) | client-side validation
* beforeValidateAttribute: undefined, // function (form, attribute) | boolean
* afterValidateAttribute: undefined, // function (form, attribute, data, hasError)
* }
*/<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'addressForm',
'enableClientValidation' => true, //是否启用客户端验证
'clientOptions' => array(
'validateOnSubmit' => true, //提交时验证
'afterValidate' => 'js:function(form,data,hasError){
if(!hasError){
$.ajax({
"type":"POST",
"url":url,
"data":$("#addressForm").serialize(),
"success":function(data){
var res = eval("("+data+")");
if("success"==res.code){
if(res.addressId>0){
$("#addressId").val(res.addressId);
}
$("#address_info").prepend(res.html);
clearAddr();
}else{
clearAddr();
}
},
});
}
}'
),
));
?>$form.bind('reset', function () {
/*
* because we bind directly to a form reset event, not to a reset button (that could or could not exist),
* when this function is executed form elements values have not been reset yet,
* because of that we use the setTimeout
*/
setTimeout(function () {
$.each(settings.attributes, function () {
this.status = 0;
var $error = $form.find('#' + this.errorID),
$container = $.fn.yiiactiveform.getInputContainer(this, $form);
$container.removeClass(
this.validatingCssClass + ' ' +
this.errorCssClass + ' ' +
this.successCssClass
);
$error.html('').hide();
/*
* without the setTimeout() we would get here the current entered value before the reset instead of the reseted value
*/
this.value = getAFValue($form.find('#' + this.inputID));
});
/*
* If the form is submited (non ajax) with errors, labels and input gets the class 'error'
*/
$form.find('label, :input').each(function () {
$(this).removeClass(settings.errorCss);
});
$('#' + settings.summaryID).hide().find('ul').html('');
//.. set to initial focus on reset
if (settings.focus !== undefined && !window.location.hash) {
$form.find(settings.focus).focus();
}
}, 1);
}); 原来yii 已经给我们实现了, 它已经绑定好reset事件了, 所以 我们只需要恰当的执行document.getElementById("addressForm").reset();yii 使用cactiveform 创建表单时候遇到的一些验证问题和使用ajax_form时重置验证规则的解决办法
原文:http://blog.csdn.net/wjc19911118/article/details/42523839