Yii - ajax 加载表单元素的用户端验证

2023-09-24php开发问题
0

本文介绍了Yii - ajax 加载表单元素的用户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在静态表单中使用 Yii 用户端验证,它很棒.但我不知道如何为 ajax 加载的元素添加验证器.

I'm using Yii user side validation in static forms and it's great. But I don't know how to add validators for ajax loaded elements.

我有一个简单的表单小部件,我想通过 AJAX 将更多的输入字段加载到其中(小 jQuery 脚本没有问题).但我不知道如何为加载的元素添加 Yii javascript 验证器 - 我的意思是自动创建的 JS 验证器,如:

I have simple form widget and I would like to load few more input fields into it via AJAX (that's not problem with small jQuery script). But I don't know how to add Yii javascript validators for loaded elements - I mean auto created JS validators like:

<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>

有什么方法可以添加或删除验证器吗?

Is there any way how to add or remove that validators?

推荐答案

这有点棘手...如果你想用 yiiactiveform 来做,你必须:

This is little bit tricky... If you want to do it using yiiactiveform, you have to:

  1. 向验证添加字段,或
  2. 从验证中删除字段

我的建议是:

  1. 创建您自己的 JavaScript 验证函数(您可能想在此处复制 yii 的验证代码)

  1. Create your own JavaScript validation functions (you may want to copy yii's validation code here)

$('#newsletter-form-footer').submit(function() {
        return MyValidator.validate();
});

  • 当您将字段加载到表单中时

  • When you load a field into the form

    //You have to get rulesORoptions
    // along with fieldName from your ajax request.
    MyValidator.add(fieldName, rulesORoptions);
    

  • 当您从表单中删除字段时

  • When you Remove a field out of the form

    MyValidator.remove(fieldName);
    

  • 此处为 MyValidator 代码...

  • MyValidator code here...

    var MyValidator = {
            fields: {},
    
            add: function(fieldName, rulesORoptions) {
                    this.fields[fieldName] = rulesORoptions;
            },
            remove: function(fieldName) {
                    delete this.fields[fieldName];
            },
    
            validate: function() {
                    var success = true;
                    for(var fieldName in this.fields) {
                            for(var rule in this.fields[fieldName]) {
    
                                    if( eval('MyValidator.'+rule+'($("#'+fieldName+'").val())') == false ) {
                                            $("#'+fieldName+'_em_").html("validation failed (some error text, rulesORoptions may contain error text )");
                                            success = false;
                                    }
                            }
                    }
    
                    return success;
            },
    
            /* Validation methods are here, 
               copy the javascript validation code from Yii
            */
            email: function(value) {
                    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])?$/)) {
                            return false;
                    }
    
                    return true;
            },
            required: function(value) {
                    if($.trim(value)=='') {
                            return false;
                    }
    
                    return true;
            },
            number: function(value) {
                    if(/*copy yii validation code */) {
                            return false;
                    }
    
                    return true;
            },
            .....
            .....
    };
    

  • 这篇关于Yii - ajax 加载表单元素的用户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

    相关推荐

    PHP实现DeepL翻译API调用
    DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
    2025-08-20 php开发问题
    168

    PHP通过phpspreadsheet导入Excel日期数据处理方法
    PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
    2024-10-23 php开发问题
    287

    mediatemple - 无法使用 codeigniter 发送电子邮件
    mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
    2024-08-23 php开发问题
    11

    Laravel Gmail 配置错误
    Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
    2024-08-23 php开发问题
    16

    将 PHPMailer 用于 SMTP 的问题
    Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
    2024-08-23 php开发问题
    4

    关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
    Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
    2024-08-23 php开发问题
    17