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.

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

报告相同问题?

悬赏问题

  • ¥50 Qt在release捕获异常并跟踪堆栈
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop
  • ¥15 共享文件夹会话中为什么会有WORKGROUP