Welcome Guest, Not a member yet? Register   Sign In
trim|valid_email not working in validation
#1

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?
Reply
#2

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 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 01-07-2018, 12:49 PM by 040mag.)

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

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

https://codeigniter.com/user_guide/libra...pping-data
Reply
#5

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(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.
Reply
#7

On which CI version is this?
Reply
#8

(This post was last modified: 01-08-2018, 02:32 AM by 040mag.)

(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.
Reply
#9

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.
Reply
#10

(This post was last modified: 01-08-2018, 05:06 AM by 040mag.)

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'].
Reply




Theme © iAndrew 2016 - Forum software by © MyBB