【问题标题】:jquery validate remote validation only works if field is changedjquery validate 远程验证仅在字段更改时才有效
【发布时间】:2013-03-14 10:35:50
【问题描述】:
<?PHP
$attributes = array('class' => "profile_form", 'id' => "profile_form",'name'=>"profile_form");
echo form_open_multipart("$form_action",$attributes);
?>
<LABEL for='email'>E-Mail</LABEL>
<INPUT type='text' id='email' name="email" class="w200" value='<?PHP echo $email;?>' remote="<?PHP echo base_url()."php/unique_email.php?id=$id"; ?>">
<DIV class='ca clr-lr'>
<input type="submit" id="submitbutton" name="submitbutton" value="<?PHP echo $title;?>" class="pr mbutton"/>
</DIV>
</FORM>

    rules: {
        email: {
            required:true,
            email:true
        }
    }

这是我用来验证电子邮件字段的代码。如果我更改电子邮件字段或输入新值,它工作正常,但如果我只是提交表单,它会给我一个错误。经过短暂检查后,我发现如果我不编辑电子邮件字段,则提交按钮不包含在帖子数据中。

我做错了什么,请帮忙。

这是我的控制器代码,它可能有助于扩大调试范围。我正在检查提交按钮是否是帖子数据的一部分。

if($this->input->post('submitbutton') !== false){
        if ($this->form_validation->run('admin_profile_form') == FALSE){
            $form=(array)$this->input->post();
            $data=array_merge($form,$data);
        }else{
            //database fields
            $database = array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'email' => $this->input->post('email'),
                'phone' => $this->input->post('mobile')
            );
            $user_id=$user->id;
            $this->db->where('id',$user_id);
            $this->db->update('users',$database);
            $this->session->set_flashdata('message_type', 'info');
            $this->session->set_flashdata('message', 'Profile updated');
            redirect($this->homepage);
            return;
        }
    }

最后,Fabio Antunes 建议的解决方案奏效了。

我将规则更改如下,并从输入字段中删除了内联遥控器

        email: {
            required:true,
            email:true,
            remote: {
                url: "php/unique_email.php",
                async: false,
                type: "post",
                data: { id: $('#edit_id').val() }
            }
        }

【问题讨论】:

    标签: jquery codeigniter validation remote-validation


    【解决方案1】:

    编辑

    正如您所说,您的代码已经在运行,您可以将其放在您的 js 文件中 您必须使用此函数$("#profile_form").validate({}),以便在提交和 keyup 事件上进行验证

    <script>
    $().ready(function() {
    
            // validate signup form on keyup and submit
            $("#profile_form").validate({
        rules: {
            email: {
                email: true
            }
        },
        messages: {
            email: {
                email: "Please provide a valid email"
            }
        }
    }); 
    }); 
    

    我注意到在你看来你有这个

    <INPUT type='text' id='email' name="email" class="w200" value='<?PHP echo $email;?>' remote="<?PHP echo base_url()."php/unique_email.php?id=$id"; ?>">
    

    换成这个

    <INPUT type='text' id='email' name="email" class="w200" value='<?PHP echo $email;?>' remote="<?PHP echo base_url()."php/unique_email.php?id=".$id; ?>">
    

    如果你注意到你的 $id 在字符串中

    【讨论】:

    • 问题是我的 javascript 代码在 .js 文件中,这就是我使用远程属性的原因。对 php 和 html 标签使用 caps 只是我觉得很舒服的,没什么特别的。你的代码看起来就像我的另一个版本,虽然我不确定类型:post。您能否在我的代码中指出错误,将不胜感激。提前致谢。如果我在字段中输入内容,我的代码可以正常工作,当我提交表单时即使使用有效值而不在字段中输入任何内容(即触发远程验证)也会导致问题
    • 两个版本,即字符串中的嵌入变量或其他版本将在浏览器中产生相同的输出。但无论如何我尝试了你的建议,但现在仍然很幸运。我添加了负责处理我的问题的控制器代码。也许我在那里做错了什么。
    • 您之前的建议,即将远程值放入规则中,进行了一些调整。我认为 async: false 和 type: "post" 是这次成功的原因。
    • 我很高兴能以某种方式帮助你
    【解决方案2】:

    试试这个设置

    $(/*your form*/).validate({  
        onsubmit: true
    })
    

    【讨论】:

    • 请不要介意我问,但这会做什么。我要求确保它不会出现在我的代码的其他部分。
    • 这将始终在提交时验证您的表单,我希望这是您想要的
    • 我希望我的表单能够正确验证和提交,而无需我在电子邮件字段中输入内容。看起来删除验证导致错误,我在没有远程验证的情况下进行了测试,一切正常。感谢您之前的提示,我学到了一些新东西。
    • 默认情况下,该选项已经为真。 As per the docs, true 的布尔值不是有效值” 用于 onsubmit 选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多