Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter使用ajax回调在表单字段旁边单独显示错误_Codeigniter_Validation - Fatal编程技术网

CodeIgniter使用ajax回调在表单字段旁边单独显示错误

CodeIgniter使用ajax回调在表单字段旁边单独显示错误,codeigniter,validation,Codeigniter,Validation,我已经使用CI制作了一个表单,并且有一个原生表单验证()库来验证每个字段输入,我使用jQuery post回调输入来检查每个字段是否有效,如果我希望每个错误填充到每个字段旁边的表单错误(),而不是验证错误() 请参阅下文: 查看: <script> $("#btnregister").click(function() { var parameters = $("#reg_form").serialize(); $.post(baseurl+'pages/regist

我已经使用CI制作了一个表单,并且有一个原生表单验证()库来验证每个字段输入,我使用jQuery post回调输入来检查每个字段是否有效,如果我希望每个错误填充到每个字段旁边的
表单错误()
,而不是
验证错误()

请参阅下文:

查看:

<script>
$("#btnregister").click(function() {
    var parameters = $("#reg_form").serialize();

    $.post(baseurl+'pages/registration', parameters, function(data) {
        if(data == "ok") {
            //show success message
        }else{

            $("#error").html(data); 

        }
    }, "html");
});
</script>


<div id="error"></div>
<form id="reg_form" method="post">
     <p>
     <label for="reg_username">Username</label><br />
     <input type="text" id="reg_username" name="reg_username" value="<?php echo set_value('reg_username'); ?>">
     <?php echo form_error('reg_username'); ?>
     </p>

     <p>
     <label for="reg_email">Email</label><br />
     <input type="text" id="reg_email" name="reg_email" value="<?php echo set_value('reg_email'); ?>">
     <?php echo form_error('reg_email'); ?>
     </p>

     <p><input type="button" id="btnregister" value="Register"></p>
</form>
</div>
public function registration(){
    $this->load->library('form_validation');

    $this->form_validation->set_rules('reg_username', 'Username', 'trim|required|min_length[4]|max_length[15]|xss_clean|is_unique[users.username]');
    $this->form_validation->set_rules('reg_email', 'Email', 'trim|required|valid_email|is_unique[users.email]');


    if($this->form_validation->run() == FALSE){
        echo validation_errors();

    }else{
        // insert to db
        echo "ok";
    }
}

谢谢您的帮助。

您必须构建自己的错误数组。如果我们能访问 表单验证的
$\u错误\u数组
,但不幸的是它受到
保护
,并且没有访问方法

我将更改您的控制器以输出json响应,以简化此操作:

public function registration()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('reg_username', 'Username', 'trim|required|min_length[4]|max_length[15]|xss_clean|is_unique[users.username]');
    $this->form_validation->set_rules('reg_email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
    if ($this->form_validation->run())
    {
       $response['status'] = TRUE;
    }
    else
    {
        $errors = array();
        // Loop through $_POST and get the keys
        foreach ($this->input->post() as $key => $value)
        {
            // Add the error message for this field
            $errors[$key] = form_error($key);
        }
        $response['errors'] = array_filter($errors); // Some might be empty
        $response['status'] = FALSE;
    }
    // You can use the Output class here too
    header('Content-type: application/json');
    exit(json_encode($response));
}
现在,ajax成功回调可以读取响应的
状态
错误
键。您可以循环查看
数据。错误
,并将每个错误添加到输入字段:

$("#reg_form").submit(function() {
    var form = $(this);
    $.post(baseurl+'pages/registration', form.serialize(), function(data) {
        if (data.status == true) {
            //show success message
        }else{
            $.each(data.errors, function(key, val) {
                $('[name="'+ key +'"]', form).after(val);
            });​
        }
    }, "json");
});

另一个简单的方法是将表单发布到表单本身,并让ajax响应重新加载整个表单,这样服务器端就可以处理消息和验证过滤器。

这对我来说太棒了。。非常感谢@Wesley Murch先生:)