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 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误