Welcome Guest, Not a member yet? Register   Sign In
getPost() not working as expected
#1

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'];

Reply
#2

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 )
Reply
#3

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

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

(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 :-)
Reply
#6

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'];

Reply
#7

(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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB