简体   繁体   中英

CodeIgniter: form submitted with <enter> fails form validation

I have this problem with CodeIgniter: - when I click the submit button in a form, the form is submitted and validated correctly - when I don't click the submit button, just hit <enter> , the form validation always fails Any solution? Is that a flaw in CI's form validation or am I missing something?

What code I have there:

--- the form view ---

form_open("/");
...some inputs...
echo form_submit('submit', 'Přihlásit');
form_close();
...

--- the controller ---

$this->CI->load->helper('form');  
$this->CI->load->library('form_validation');

$this->CI->form_validation->set_rules('id_uziv', 'ID', 'required');   
$this->CI->form_validation->set_rules('heslo', 'Heslo', 'required');  
//... see, no rules have anyhting to do with the submit button

if ($this->CI->form_validation->run() == FALSE) {
  // validation OK
}
else {
  // validation failed
}

you're not echoing form_open('/')

your form elements will not be enclosed within a <form> element so when you submit, no post data will be sent to the server resulting in your validation failing.

I solved it by adding an empty validation rule for the submit button, like this:

$this->CI->form_validation->set_rules('submit_button', 'Submit', '');

Now it works. I don't really know what was causing the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM