trim|valid_email not working in validation |
PHP Code: $this->form_validation->set_rules('xname','','trim|required'); 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?
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()]]]) What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(01-07-2018, 12:21 PM)InsiteFX Wrote: Use php's trim method before inserting into the database.
But trim is used with set_rules(). See manual here:
https://codeigniter.com/user_guide/libra...pping-data
Yes, but you are using quotes for the set_rules label field
PHP Code: $this->form_validation->set_rules('xname','Xname','trim|required'); 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 )
(01-07-2018, 07:44 PM)InsiteFX Wrote: Yes, but you are using quotes for the set_rules label field 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.
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.
First I copy some input variables from a submitted form:
PHP Code: $this->data['item']['xname'] = $this->input->post('xname'); Then I set the validation rules PHP Code: $this->form_validation->set_rules('xname','','trim|required'); 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']. |
Welcome Guest, Not a member yet? Register Sign In |