download1214 2019-04-21 23:59
浏览 102
已采纳

如何使用CodeIgniter表单验证库验证空复选框并通过AJAX显示消息?

Displaying form error message when checkbox is not checked using CodeIgniter form validation library.

Currently, form error message is displayed only when checkbox is checked.

Here is AJAX call:

$('#add_user').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
        url : $(this).attr('action'),
        method : "post",
        data: $(this).serialize(),
        dataType: "json",
        success: function(result) {
            if(result.success == true) {
                alert('Success');
            } else {
                $.each(result.errors, function(key, value) {
                    var error = $('#' + key);
                    error.closest('div.form-group')
                    .find('.error').remove();
                    error.after(value);
                });
            }
        }
    });
});

And here is PHP controller:

$this->form_validation->set_rules('agreement', 'Agreement', 'callback_agreement_checkbox');

if($this->form_validation->run()) {
            $data['success'] = true;
            $this->load->view('Success');
        } else {
            foreach($this->input->post() as $key => $value) {
                $data['errors'][$key] = form_error($key);
            }
            echo json_encode($data);
        }

public function agreement_checkbox() {
        if (empty($this->input->post('agreement'))) {
            return FALSE;
        } else {
            $error = 'Please accept TOS';
            $this->form_validation->set_message('agreement_checkbox', $error);
            return TRUE;
        }
    }

When the form is submitted without checkbox checked, error message is not displayed (but it should). It's displayed only when checkbox is checked and it's wrong.

EDIT:

I've done some modifications for support of mixing different input data:

$this->form_validation->set_rules('first_name', 'First name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last name', 'trim|required');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required');
$this->form_validation->set_rules('agreement', 'Agreement', 'required');

if($this->form_validation->run()) {
            $data['success'] = true;
            $this->load->view('Success');
        } else {                
            foreach($this->input->post() as $key => $value) {
                $data['errors'][$key] = form_error($key);
            }
            if (empty($this->input->post('agreement'))) {
                $data['errors']['agreement'] = form_error('agreement', '<div id="agreement_error">', '</div>');
            }           
            echo json_encode($data);
        }
  • 写回答

1条回答 默认 最新

  • douxi7219 2019-04-22 01:03
    关注

    Callback function of Codeigniter does not defined $this->input->post() . I suggest you to use required. So it will be like this.

    $this->form_validation->set_rules('agreement', 'Agreement', 'required');
    

    it will defined if the check box was checked before it submitted. then change this code too.

    if($this->form_validation->run()) {
        if(! empty($this->input->post('agreement))){
              $data['success'] = "It was checked";
            }else{
              $data['error'] = validation_errors();
        }
        echo json_encode($data);
    }
    

    Your ajax:

            success: function(result) {
            if(result.success) {
                console.log(result.success);
            } else {
                console.log(result.error);
            }
        }
    

    Let me know the result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题