I need to update database table columns only if valid fields are received in the request.
If a field is received with a blank value '', the validation rules must be applicated or not?
Why is a blank value '' accepted when the min_length[5] (strlen('') equals 0, less then 5) and valid_email ('' is not a valid email address) rules are set?
If proceed the columns will be populated with blank values... But this can not happen!
How to solve this?
Please, check the code:
I see that it is intentional. But is correct and I have not understand something?
If a field is received with a blank value '', the validation rules must be applicated or not?
Why is a blank value '' accepted when the min_length[5] (strlen('') equals 0, less then 5) and valid_email ('' is not a valid email address) rules are set?
If proceed the columns will be populated with blank values... But this can not happen!
How to solve this?
Please, check the code:
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->post();
$this->form_validation->set_data($userdata);
$this->form_validation->set_rules([
[
'field' => 'username',
'label' => 'Username',
'rules' => 'min_length[5]|max_length[32]',
],
[
'field' => 'email',
'label' => 'Email',
'rules' => 'valid_email',
],
]);
if ( ! $this->form_validation->run())
{
var_dump($this->form_validation->error_array());
exit;
}
echo 'Validated! Data to be updated: ';
var_dump($userdata);
}
}
I see that it is intentional. But is correct and I have not understand something?