Welcome Guest, Not a member yet? Register   Sign In
Form Validation validating blank values with min_length and valid_email rules
#8

Yeah. I found a logic for working with blank fields.

For now I'll add an isset rule if the blank field is set but with an empty string. The logic will be like this:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * Class Issue
 */
class Issue extends CI_Controller
{

    public function 
index()
    {
        
$this->load->library('form_validation');

        
// Test: If you add one character in username or email the validation will run and show the errors...
        //$userdata = ['username' => '', 'email' => ''];
        
$userdata $this->input->get();

        
$this->form_validation->set_data($userdata);
        
$this->form_validation->set_rules([
            [
                
'field' => 'username',
                
'label' => 'Username',
                
'rules' => 'min_length[5]|max_length[32]'
                    
. (isset($userdata['username']) && $userdata['username'] === '' '|isset' ''),
            ],
            [
                
'field' => 'email',
                
'label' => 'Email',
                
'rules' => 'valid_email'
                    
. (isset($userdata['email']) && $userdata['email'] === '' '|isset' ''),
            ],
        ]);

        if ( ! 
$this->form_validation->run())
        {
            
var_dump($this->form_validation->error_array());
            exit;
        }

        foreach (
$userdata as $key => $val)
        {
            if ( ! 
in_array($key, ['username''email']))
            {
                unset(
$userdata[$key]);
            }
        }


        if (empty(
$userdata))
        {
            
var_dump('Nothing to update. Stop here.');
            exit;
        }

        echo 
'Validated! Data to be updated: ';
        
var_dump($userdata);
    }



I did some research and I saw that there was a issue like this a long time ago.
Reply


Messages In This Thread
RE: Form Validation validating blank values with min_length and valid_email rules - by natanfelles - 10-16-2017, 10:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB