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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算