CodeIgniter`validate_errors()`无效

时间:2014-03-11 21:27:13

标签: php codeigniter validation codeigniter-2

我使用的是 CodeIgniter 2.1.4 版本。我在显示表单验证错误时遇到问题。表单验证返回false,但validation_errors()未显示任何错误。我曾在echocontroller中尝试view,但没有结果。我通过ajax.no提出请求错误被抛出。

控制器:

<?php
class Dashboard extends Admin_Controller {

   public function ajax_new_dist_center($id=NULL)
   {
       $this->load->model('dist_centre_m');
       $this->load->helper(array('form', 'url'));
       $this->load->library('Form_validation');

       $validation = $this->dist_centre_m->rules;
       $this->form_validation->set_error_delimiters('<li>', '</li>');
       $this->form_validation->set_rules($validation);
       if ($this->form_validation->run() == TRUE)
       {
            if($this->dist_centre_m->create($id))
                echo 'New centre created';
            else
            {
                $this->output->set_status_header('404');
                echo 'Given center not found';
            }
       }
       else
       {
           $this->output->set_status_header('400');
           echo 'validation Failed';
           $this->load->view('alert_error');
       }
   }
}

查看:

<?php echo validation_errors();?>
<p>Testing Error</p>

型号:

class dist_centre_m extends MY_Model {
    protected $_table_name = 'distribution_centre';
    protected $_primary_key = 'dis_id';
    protected $_order_by = 'dis_id';
    public $rules = array(
        'name' => array( 
            'field'=>'name',
            'label'=>'Center name',
            'rules'=>'trim|required|xss_cleaned|min_length[3]|max_length[45]'
        ),
        'street' => array(
            'field'=>'street',
            'label'=>'Street',
            'rules'=>'trim|required|xss_cleaned|min_length[3]|max_length[45]'
        ),
        'town' => array(
            'field'=>'town',
            'label'=>'Town',
            'rules'=>'trim|required|min_length[3]|max_length[45]|required|xss_cleaned'
        ),
        'postcode' => array(
            'field'=>'postcode',
            'label'=>'Postcode',
            'rules'=>'trim|required|max_length[10]|required|xss_cleaned'
        ),
        'tel' => array(
            'field'=>'tel',
            'label'=>'Telephone number',
            'rules'=>'trim|valid|exact_length[11]|required|xss_cleaned'
        ),
    );

    public function __construct() {
        parent::__construct();
        $this->load->helper('security'); 
    }

    public function create($id=NULL){
        $data = array(
                'name'      =>$this->input->post('name',true),
                'street'    =>$this->input->post('street',true),
                'town'      =>$this->input->post('town',true),
                'postcode'  =>$this->input->post('postcode',true),
                'tel'       =>$this->input->post('tel',true),
        );
        return $this->save($data,$id);
    }  
} 

输出:

validation Failed

Testing Error

相关问题均无效,请勿标记重复。

更新 我的Ajax请求页面:

 <form id="new-center-form" method="post">
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group">
                            <label for="name">Name</label>
                            <input type="text" name="name" placeholder="Name"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for='street'>Street</label>
                            <input type="text" name="street" placeholder="Street"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="town">Town</label>
                            <input type="text" name="town" placeholder="Town"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="postcode">Postcode</label>
                            <input type="text" name="postcode" placeholder="Postcode"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="tel">Phone</label>
                            <input type="text" name="tel" placeholder="Telephone"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group">
                            <button id="centre-submit" class="btn btn-primary pull-left">Save</button>
                        </div>
                    </div>
                </div>
            </form>


<script type="text/javascript">
    $(document).ready(function(){

        var url_new_center = '<?php echo page_url('new-center') ?>';

        $('#centre-submit').click(function(e){
            e.preventDefault();
            $.ajax({
                url:url_new_center,
                type:'post',
                data: $('new-center-form').serialize()
            }).done(function(response){
                $('#infos').html(response);
                $('#infos').slideDown();
                $('.new-centre').slideUp();
            }).fail(function(response){
                $('#errors').html(response);
                $('#errors').slideDown();
            });
        })
    });
</script>

2 个答案:

答案 0 :(得分:3)

经过长时间的睡眠后,我发现了。$this->form_validation->run()在未找到false数据的情况下返回$_POST,并且不会设置任何错误进行打印。在我的ajax请求中,由于某些错误,它无法发送任何数据。感谢 curl 我能够测试它。

<强>更新

 data: $('new-center-form').serialize() // this was the error. 

我在这里写了一个错字。这应该是这个data: $('#new-center-form').serialize()

答案 1 :(得分:1)

让我更清楚地说明你的答案。

这不是因为没有发现$ _POST数据,就像你说的那样, 实际上,因为您没有在模型中为它​​们设置规则的 RELATED $ _POST数据。

可能已为您为其设置规则的人排除了任何其他$ _POST数据:

&#39;名称&#39; ,&#39; street&#39; ,&#39; town&#39; ,&#39;邮政编码&#39; ,&#39;电话&#39;

然后CI验证器找不到任何已设置规则的元素,因此form_validation->run()肯定会返回 false

当然validation_errors()不会显示任何内容,因为没有任何内容可以应用指定的规则。