CodeIgniter Forums
getPost() not working as expected - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: getPost() not working as expected (/showthread.php?tid=77872)



getPost() not working as expected - 68thorby68 - 10-29-2020

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')){
    $data['emp_no']=$this->request->getGet('emp_no');
    $data['employee_data']=$em->getEmployeeById($this->request->getGet('emp_no'));
    
}else{
    if($this->request->getPost('emp_no')){
        $data['emp_no']=$this->request->getPost('emp_no');
    }
}
            
if ($this->request->getPost('first_name')) {                
    $data['first_name'] =$this->request->getPost('first_name'FILTER_SANITIZE_STRING);
}else{            
    $data['first_name'] = $data['employee_data']['first_name'];




RE: getPost() not working as expected - InsiteFX - 10-29-2020

You should run validation on all input fields first.


RE: getPost() not working as expected - 68thorby68 - 10-29-2020

(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 :-) ?


RE: getPost() not working as expected - paulbalandan - 10-29-2020

If the field is optional, then you can add the permit_empty rule.


RE: getPost() not working as expected - 68thorby68 - 10-29-2020

(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 :-)


RE: getPost() not working as expected - ojmichael - 10-29-2020

You should check for a POST request, and use the default values only for a GET request.

PHP Code:
if ($this->request->getMethod() === 'post') {
    $data['first_name'] $this->request->getPost('first_name', FILTER_SANITIZE_STRING);
} else {
    $data['first_name'] = $data['employee_data']['first_name'];




RE: getPost() not working as expected - 68thorby68 - 10-30-2020

(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.

PHP Code:
if ($this->request->getMethod() === 'post') {
    $data['first_name'] $this->request->getPost('first_name', FILTER_SANITIZE_STRING);
} else {
    $data['first_name'] = $data['employee_data']['first_name'];


Many thanks,
I really appreciate your time and help.

So, I think we can conclude getPost() does not recognise empty form fields!