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.
Related
I have a controller with edit method and my edit URL like
http://localhost/pothdekhun/road/edit/3
When any server side error occurred then with following code i am showing same form with errors
if ($this->form_validation->run() == FALSE) {
//this is loading edit view form
$this->nuts_lib->view_loader('user', 'add', $data, TRUE, 'latest_routes', 'rightbar');
return;
}
Now my problem is in URL and the URL showing like (When error in edit form)
http://localhost/pothdekhun/road/edit
As we know redirect not possible here with validation error messages. Now how can I can resolve this issue??
So I want URL like http://localhost/pothdekhun/road/edit/3 with validation messages. Will I change my edit form action like site_url('road/edit/').$route_id OR any better idea??
As per discussion you was facing issue with missing values for hidden fields on submit form and if there are form errors. please follow my answer for this to get values after form submit.
How to prevent forms from emptying inputs by submiting the form in html?
I have a page that takes form input, it is written using HTML and PHP(codeignter framework), the issue I am facing is when there is a form validation error, the page refresh is not reloading the page like brand new, I mean its not clearing the input from fields and the error message still coming up on page refresh and even after I correct the input and hit submit, when I click browser back button the it is still loading the page with validation error..
Any idea why it happens or how to resolve it. I just need the page to reload when I refresh it.
Here is my view (view1.php)
<html>
<?php
echo validation_errors();
form_open('search_controller/search_fn');
form_input('input1', set_value('input1'));
form_input('input2', set_value('input2'));
form_input('input3', set_value('input3'));
form_submit('submit', 'Submit');
?>
</html>
Controller Functions
function index() {
$this->load->view('view1');
}
function search_fn {
if($this->input->post('submit') {
$this->form_validation->set_rules('input1', 'XXXX',
'trim|some_validation');
$this->form_validation->set_rules('input2', 'XXXX',
'trim|some_validation');
$this->form_validation->set_rules('input3', 'XXXX',
'trim|some_validation');
if($this->form_validation->run() == FALSE) {
//when validation fails
$this->load->view('view1');
} else {
//when all input is validated sucessfully
do some processlisng,
//load some other view
$this->load->view('view2');
}
} else {
//When page is refreshed, this is supposed to redirect
redirect('search_controller/index');
}
}
when the input validation fails, the url comes back as www.something.com/search/search_fn and when I refresh, it basically loading the same page with same error message. I, somehow, need to reset
the $this->input->post('search'), so that it will be true only when the search button is clicked.
Thanks.
Are you using CodeIgniter's redirect() function after processing form? you should always do that
I mean after you are done handling submitted data, instead of return TRUE or simply closing function, to a simple redirect('CONTROLLER_NAME'); even if you want them to be on the same page
As for the fields, i would suggest using autocomplete="off" in your inputs (if you are not using an <input type="text"> above an <input type="password"> because Chrome and next version of Firefox will ignore automplete="off" in that situation)
Basically, I have my controller function for adding in my case a page but let’s focus on the function:
public function add() {
$this->session->unset_userdata('postID');
if ($this->form_validation->run() == TRUE) {
// Form Validation
}
$this->data['subview'] = 'blah blah';
$this->load->view('blah blah.php', $this->data);
}
Basically when the form is submitted it will still unset the postID in this case, however i want to ensure that if the form is submitted and there are errors that this is missed and it doesn’t redo some of my functions and variables. This is happening for a lot of my content when the form is submitted it re-initiates variables that i want to be ignored.
I also tried the following but it didn’t work either:
if (!$this->form_validation->run()) {
$this->session->unset_userdata('postID');
}
How do i avoid the entire page being redone when the form is validating as it re-performs all the page load content?
Thanks
You can check if the submit button was pushed by using
if($this->input->post('the-name-of-the-submit-button')) {
$this->session->unset_userdata('postID');
}
Basically, $this->form_validation->run() checks if the form was submitted and if it passed validation rules, whereas the above simply checks if the form was submitted at all.
You need to do an ajax submission if you don't want the page to refresh:
http://phpsblog.agustinvillalba.com/sending-forms-ajax-codeigniter/
Alternatively, you can fill in the field values so they don't "RESET", ie <input value="<?php echo $_POST['value'];?>" />, but ajax is a much better solution.
You can return errors in JSON from your codeigniter controller and display them on your page.
If you do any type of request with your browser, the page is always going to refresh, unless you use ajax/javascript.
I have 2 forms on a page eg: 'create_user' and 'update_user' i want to show validation errors specific to each form just above the form. i mean, i want to show validation errors for 'create_user' form just above it and same for 'update_user'.
Right now if i submit update, form errors are showing above both of the forms.
is there something like
<? echo validation_errors("create_user"); ?>
You could do something like:
//pass validation_errors as data to your view from your controller, like
//in your controller
if ($this->form_validation->run()) { //form create_user form
//process
}
else {
$data['create_user_form_errors'] = validation_errors();
}
//and for second form update_user
if ($this->form_validation->run()) { //form create_user form
//process
}
else {
$data['update_user_form_errors'] = validation_errors();
}
//then in your view
if (isset($create_user_form_errors)) echo $create_user_form_errors; //show above first form
//and
if (isset($update_user_form_errors)) echo $update_user_form_errors; //show above second form
Something like that should work for showing errors based on multiple forms. Hope it helps
Simplest way is this:
Pass some sort of form-identifying hidden field with each form
Pass that same hidden field to the view error state, and use a basic IF condition to run validation_errors() in the correct location
You could probably extend the validation class somehow to do what you're asking, but it would be a very specific implementation, and hardly useful in a framework (which is why CI doesn't "know how" to do this by default). The added complexity of working with the extended class would likely counteract any benefit of not simply using basic logic in the view.
Whats the best way to handle the following situation in codeigniter:
Home controller has an index action and a submit action.
The submit action is used for a form submission. I want to load the page through the index controller though - including after form submission i.e. with form errors data to repopulate form inputs on error etc.
Whats the best way to do this - without having the index controller handle the main page loading and the form submission.
If you redirect(), you won't be able to use set_value() for re-populating your form fields.
What's easiest is having your index controller handle both the default load behavior, and the submission.
function index()
{
if($this->input->post('foo'))
{ // something was POSTed
$this->load->library('form_validation');
//validation rules
} else
{ // normal view
//
}
$this->load->view('home');
}
Alternatively, you can just set up your index and submit controller, and have them point at the same view, which detects if validation_errors() are set and re-populates form fields accordingly.
Third option (hackish): you could use flashdata to keep submission errors and the submitted form values across a redirect back to index. Something like this would work:
$this->session->set_flashdata('errors', $validation_errors());
Simply point your form at your submit action:
<form method="post" action="/home/submit">
Then, use the submit action to validate your input before redirecting back to the index action.