Welcome Guest, Not a member yet? Register   Sign In
Form validation returning FALSE
#4

(This post was last modified: 10-15-2017, 10:27 AM by dwlamb. Edit Reason: clarity )

I discovered why FALSE is being returned but am at a loss to explain CI's state at the time my application seeks to validate the email address.

In core/Form_validation.php @ line 167 we have `public function set_rules`
PHP Code:
    public function set_rules($field$label ''$rules = array(), $errors = array())
    {
        
// No reason to set rules if we have no POST data
        // or a validation array has not been specified
        
if ($this->CI->input->method() !== 'post' && empty($this->validation_data))
        {
            return 
$this;
        } 


For that first if, $this->CI->input->method() must equal 'post'.  The value is derived from $_SERVER['REQUEST_METHOD'].  For some reason in my application the value is 'GET' [Case is irrelevant. method() does a case conversion on the value].  That state is despite having values in $_POST placed there by a form submission.  I also know the data is in $_POST at the the time form validation runs after this set_rules function. Yes, PaulD name does equal 'email' in the $_POST and validation rule.

In core/Form_validation.php public function run() line 422:

PHP Code:
    public function run($group '')
    {
        
$validation_array = empty($this->validation_data)
            ? 
$_POST
            
$this->validation_data
$validation_array gets its data from $_POST.

From research as to how CI sets $_SERVER['REQUEST_METHOD']='POST' CI does so with the form helper using the form_open() function to create a semantic form opening tag with a method equal to 'post'.

For now, this is my code: 

PHP Code:
    if (isset($_POST['email']) && !empty($_POST['email'])) {
        
$_SERVER['REQUEST_METHOD']='POST'//added this as band-aid
        
$this->load->helper('form');
        
$this->load->library('form_validation');
        
$this->form_validation->set_rules('email''E-mail''trim|valid_email|required');
        if (
$this->form_validation->run()==FALSE) {
            
$data['errors'] = 'E-mail address you provided is not valid.';
            
$data['inset_content']    = $this->load->view('login/inset_obtain_email_password_reset'$dataTRUE);
            
$this->load->view('view_login_template'$data);
        } else {
            
$email        trim($this->input->post('email'));
            
$user        $this->model_reset_password->email_exists($email);
            if (
$user) {
                
$this->send_reset_password_email($email,$user['first_name'],$user['salt']);
            }
            
$data['inset_content']    = $this->load->view('login/inset_reset_password_email_sent'$dataTRUE);
            
$this->load->view('view_login_template'$data);
        }
//if
    
}//if 

I added $_SERVER['REQUEST_METHOD']='POST';.  The validation rule sets, validation can run and return TRUE.

If someone can think of the reason $_SERVER['REQUEST_METHOD'] would equal GET, please enlighten me.

I have another CI application which uses 3.0.6.  That application also has a password reset system but the code is very different from the one above.  Stepping through that code for form validation of a submitted e-mail address, $_SERVER['REQUEST_METHOD'] equals 'POST' as set by CI.  
Reply


Messages In This Thread
Form validation returning FALSE - by dwlamb - 10-14-2017, 12:05 PM
RE: Form validation returning FALSE - by PaulD - 10-15-2017, 04:58 AM
RE: Form validation returning FALSE - by dwlamb - 10-15-2017, 10:23 AM
RE: Form validation returning FALSE - by dwlamb - 10-15-2017, 01:06 PM
RE: Form validation returning FALSE - by dwlamb - 10-15-2017, 06:45 PM
RE: Form validation returning FALSE - by gmgj - 04-17-2019, 11:38 AM
RE: Form validation returning FALSE - by gmgj - 04-30-2019, 03:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB