![]() |
Continuous Loop in form validation - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Continuous Loop in form validation (/showthread.php?tid=37362) |
Continuous Loop in form validation - El Forum - 01-06-2011 [eluser]yorvik[/eluser] Hello I have this litte code where I update value of the database with jquery AJAX. Code: class Art_controller extends Controller { Now i load this view: (but when I typ $artwork less than 5 caracters I get in an continous loop) I want my errors to be showed but that is not the case. Can someone help? Code: <h1>Edit artwork!</h1> Edit: I fixed the problem with the following code. Continuous Loop in form validation - El Forum - 01-06-2011 [eluser]Nick_MyShuitings[/eluser] I don't have to much time to read or test all of that... but you have an error here: Code: if ($kunstwerk = $this->input->post('kunstwerk')) You would want this I think: Code: if ($kunstwerk == $this->input->post('kunstwerk')) Continuous Loop in form validation - El Forum - 01-06-2011 [eluser]yorvik[/eluser] I edited the error, but it's not that what's causing trouble, my function index() keeps getting viewed in an continues loop. Continuous Loop in form validation - El Forum - 01-06-2011 [eluser]Nick_MyShuitings[/eluser] this is the other section that seems odd: Code: if($this->form_validation->run() == FALSE) What is the functionality of $this->input(); ? Continuous Loop in form validation - El Forum - 01-06-2011 [eluser]cideveloper[/eluser] I always have a hard time explaining form validation but I will attempt to help and kinda figure out what is going on here. Code: $this->form_validation->run() == FALSE runs initially when the form has not been submitted to be validated or when validation fails. So when you say Code: $artwork= $this->form_validation->set_rules('kunstwerk', 'Kunstwerk', 'trim|required|min_length[5]'); and $artwork is <5 then you will just rerun the input function. Since you are not redirecting or anything else like that Code: $kunstwerk == $this->input->post('kunstwerk') will be true again, and validation will fail again and you will go back to input function. voila infinite loop. what I dont understand is why you are setting the variables Code: $artwork= $this->form_validation->set_rules('kunstwerk', 'Kunstwerk', 'trim|required|min_length[5]'); why wouldnt you just do this Code: $this->form_validation->set_rules('kunstwerk', 'Kunstwerk', 'trim|required|min_length[5]'); also in your ajax call Code: url: [removed] I assume you are sending a 3rd segment in the uri? Continuous Loop in form validation - El Forum - 01-07-2011 [eluser]Twisted1919[/eluser] [quote author="Nick_MyShuitings" date="1294381432"]I don't have to much time to read or test all of that... but you have an error here: Code: if ($kunstwerk = $this->input->post('kunstwerk')) You would want this I think: Code: if ($kunstwerk == $this->input->post('kunstwerk')) Code: if ($kunstwerk = $this->input->post('kunstwerk')) Code: $kunstwerk = $this->input->post('kunstwerk'); Just as a sidenote Code: if ($kunstwerk == $this->input->post('kunstwerk')) Continuous Loop in form validation - El Forum - 01-07-2011 [eluser]yorvik[/eluser] Ok I edited the suggestions, now I am using Code: if($this->form_validation->run() == FALSE) This works but I am still not able the show the message errors on my view. Is there any way I could realise this My ajax url is: url: ...art_controller/input/22 My 3rd segment is the ID of the Current record I am updating in the database. Continuous Loop in form validation - El Forum - 01-07-2011 [eluser]Twisted1919[/eluser] [quote author="yorvik" date="1294419677"]Ok I edited the suggestions, now I am using Code: if($this->form_validation->run() == FALSE) This works but I am still not able the show the message errors on my view. Is there any way I could realise this My ajax url is: url: ...art_controller/input/22 My 3rd segment is the ID of the Current record I am updating in the database.[/quote] As you are doing, you won't get the errors because you refresh the webpage. You can, however, do something like this: [code] if($this->form_validation->run() == FALSE) { $this->session->set_flashdata('form_error',validation_errors());//set here before redirect redirect('art_controller/index', 'refresh'); } // AND in your views, simply do a : echo $this->session->flashdata('form_error'); [code] This will work for sure. Your initial idea was the bad one, because you were refreshing the page therefore resetting everything . Continuous Loop in form validation - El Forum - 01-07-2011 [eluser]yorvik[/eluser] I found a good tutorial on CI , form_validation and AJAX. I rebuild my code and now it's working. You can view my sollution in the first post. I think this problem is solved or could my code be more best practice? Continuous Loop in form validation - El Forum - 01-07-2011 [eluser]cideveloper[/eluser] A couple things I would change. 1) your javascript Code: $('#myForm').submit(function(e){ This way you can take this file out of your views and put it into an external js where it belongs. Also in your code if Javascript is disabled and the form is submitted the user will be sent to art_controller/submit/22 and if validation fails they will just see the error message and NOT be sent back to the form with the error. Not good in my opinion I will update soon with a controller I think will be better |