CodeIgniter Forums
trim|valid_email not working in validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: trim|valid_email not working in validation (/showthread.php?tid=69698)

Pages: 1 2


trim|valid_email not working in validation - 040mag - 01-07-2018

PHP Code:
$this->form_validation->set_rules('xname','','trim|required');
$this->form_validation->set_rules('xemail','','trim|valid_email'); 

Hi, in the above code the first line is trimming 'xname' as expected. But the second line to not remove spaces in the beginning of 'xemail'. I get validation success and I insert the data in a database. When looking in the database the spaces are still there in the beginning of the 'xemail' but gone in 'xname'.  Any idea what is causing this?


RE: trim|valid_email not working in validation - InsiteFX - 01-07-2018

Use php's trim method before inserting into the database.

You are doing the set_rules wrong, there  is no trim in set_rules.

PHP Code:
set_rules($field[, $label ''[, $rules ''[, $errors = array()]]])

Parameters:    

$field (string) – Field name
$label 
(string) – Field label
$rules 
(mixed – Validation rules, as a string list separated by a pipe “|, or as an array or rules
$errors 
(array) – A list of custom error messages 



RE: trim|valid_email not working in validation - 040mag - 01-07-2018

(01-07-2018, 12:21 PM)InsiteFX Wrote: Use php's trim method before inserting into the database.

You are doing the set_rules wrong, there  is no trim in set_rules.

PHP Code:
set_rules($field[, $label ''[, $rules ''[, $errors = array()]]])

Parameters:    

$field (string) – Field name
$label 
(string) – Field label
$rules 
(mixed – Validation rules, as a string list separated by a pipe “|, or as an array or rules
$errors 
(array) – A list of custom error messages 



RE: trim|valid_email not working in validation - 040mag - 01-07-2018

But trim is used with set_rules(). See manual here:

https://codeigniter.com/user_guide/libraries/form_validation.html?highlight=form_validation#prepping-data


RE: trim|valid_email not working in validation - InsiteFX - 01-07-2018

Yes, but you are using quotes for the set_rules label field

PHP Code:
$this->form_validation->set_rules('xname','Xname','trim|required');
$this->form_validation->set_rules('xemail','Xemail','trim|valid_email'); 

But like I mentioned if it will not work then you will need to trim the fields before inserting them into the database.


RE: trim|valid_email not working in validation - 040mag - 01-08-2018

(01-07-2018, 07:44 PM)InsiteFX Wrote: Yes, but you are using quotes for the set_rules label field

PHP Code:
$this->form_validation->set_rules('xname','Xname','trim|required');
$this->form_validation->set_rules('xemail','Xemail','trim|valid_email'); 

But like I mentioned if it will not work then you will need to trim the fields before inserting them into the database.

Actually the problem was this: I $this->form_validation->use set_data($array) since the data I want to validate is not the one in $_POST. However there is a bug in CI that prevent native php functions to execute on other data than $_POST. This will be fixed in CI 3.2 see issue #2436. Meanwhile I do as you suggest, I trim before inserting. Thanks.


RE: trim|valid_email not working in validation - Narf - 01-08-2018

On which CI version is this?


RE: trim|valid_email not working in validation - 040mag - 01-08-2018

(01-08-2018, 02:03 AM)Narf Wrote: On which CI version is this?

I am on 3.1.6
My understanding is that the fix should come out with 3.2.

And I was wrong in my first post. Trim didn't work in either of the two cases.


RE: trim|valid_email not working in validation - Narf - 01-08-2018

If it doesn't work in either case, then the question becomes: where's the rest of the code? It is unknown what variables you expect to be trimmed.


RE: trim|valid_email not working in validation - 040mag - 01-08-2018

First I copy some input variables from a submitted form:

PHP Code:
$this->data['item']['xname'] = $this->input->post('xname');
$this->data['item']['xemail'] = $this->input->post('xemail'); 

Then I set the validation rules
PHP Code:
$this->form_validation->set_rules('xname','','trim|required');
$this->form_validation->set_rules('xemail','','trim|valid_email'); 

Then I set what to validate:


PHP Code:
$this->form_validation->set_data($this->data['item']); 

Then I validate
PHP Code:
if($this->form_validation->run()) 

No trim is done on $this->data['item'].