getPost() not working as expected |
Hi,
I have a simple process in my controller, however if the POST of form input 'first_name' is empty, the statement if($this->request->getPost('first_name') ) is evaluated FALSE and (of course) an exception is reported as $data['employee_data']['first_name'] does not exist. Is this how request->getPost() is supposed to work for empty form input fields? This is quite annoying especially if the form input firld is optional ! PHP Code: if($this->request->getGet('emp_no')){
You should run validation on all input fields first.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(10-29-2020, 03:31 AM)InsiteFX Wrote: You should run validation on all input fields first.Thanks InsiteFX, But that doesn't really make any sense. If if($this->request->getPost()) does not recognise empty POSTed form inputs, how can i run validation ($this->validation->run())?? Or am I missing something :-) ?
(10-29-2020, 09:38 AM)paulbalandan Wrote: If the field is optional, then you can add the permit_empty rule.Thanks Paul, I really appreciate you comments, but how do i validate if I can't even get hold of the value, my issue isn't validation it is with the $this->request->getPost() statement. Are we saying that when a form value is empty, ie PHP Code: <input type="text" name="first_name" value="" /> the statement PHP Code: if($this->request->getPost('first_name')) { .. is FALSE ??? Surely the value is "empty"! And, if I then validate PHP Code: $this->request->getPost('first_name') my rules should validate against an empty value (like you suggested)? Or, am I missing something??? Sorry to be a pain :-)
You should check for a POST request, and use the default values only for a GET request.
PHP Code: if ($this->request->getMethod() === 'post') {
(10-29-2020, 04:09 PM)ojmichael Wrote: You should check for a POST request, and use the default values only for a GET request. Many thanks, I really appreciate your time and help. So, I think we can conclude getPost() does not recognise empty form fields! |
Welcome Guest, Not a member yet? Register Sign In |