3
我在静态表单中使用Yii用户端验证,这很好。 但我不知道如何为ajax加载的元素添加验证器。Yii - 用户端验证ajax加载表单元素
我有简单的窗体小部件,我想通过AJAX加载更多的输入字段(这对小jQuery脚本没有问题)。但我不知道如何添加的Yii验证器的JavaScript加载的元素 - 我的意思是创建JS汽车验证器,如:
<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) {
$('#newsletter-form-footer').yiiactiveform({'validateOnSubmit':true,'validateOnChange':false,'afterValidate':Form.handleByAjax,'attributes':[{'id':'NewsletterForm_emailaddress','inputID':'NewsletterForm_emailaddress','errorID':'NewsletterForm_emailaddress_em_','model':'NewsletterForm','name':'emailaddress','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {
if($.trim(value)=='') {
messages.push(" VALIDATOR_REQUIRED");
}
if($.trim(value)!='' && !value.match(/^[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/)) {
messages.push(" VALIDATOR_EMAIL");
}
}}]});
});
/*]]>*/
</script>
有什么办法如何添加或删除验证?
以标准方式添加验证器:通过渲染使用适当输入元素渲染器的视图(例如,与[本](http://www.yiiframework.com/doc/api/1.1/CHtml#activeTextField-detail)和类似的方法或通过'CActiveForm') – Jon 2012-08-17 09:39:49
我已经有了它们。但我有另一个问题: 我有验证器的元素: '$ email = new Yp_Form_Element(Yp_Form_Element :: FIELD_textField,'email',$ this); $ email-> setRequired()' 但Yii在渲染页面后创建了javascript客户端验证脚本。但我需要为这个脚本添加一些Ajax加载元素。 – emte 2012-08-17 10:22:52
如何渲染所有输入并使用jquery隐藏它们,而不是通过ajax加载它们。如果没有太多的话。 – Asgaroth 2012-08-27 15:19:02