Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter form validation rules - What does trim do?

What does the trim form validation rule actually do and when should I use it?

Thanks

like image 712
pixeltocode Avatar asked Aug 21 '10 09:08

pixeltocode


2 Answers

It removes whitespace from the beginning and end of an input string.

You can think of validation rules as a pipeline of routines run on the input. For example :

$this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');

When validating the email input CodeIgniter :

  1. Removes whitespace from the beginning and end of the string
  2. Checks that the resulting string is non-empty
  3. Checks that it is a valid email address

The trim routine is normally run before other routines and placed at the front of the list.

You should use it when you want to ignore the starting and trailing whitespace in the input. Perhaps the user has accidentally typed a space character after an email address in a HTML form. The space character should not be taken as part of the email address and should be removed.

like image 103
Stephen Curran Avatar answered Oct 21 '22 22:10

Stephen Curran


You can use any in-built PHP function that takes one argument and returns a value or true or false as a form validation rule. So trim isn't actually a Codeigniter specific function, it's a PHP one.

Some other in-built PHP functions you can use are; is_int (checks if a number is an integer), ltrim (trims left of a string), rtrim (trims the right of a string), sha1, md5, etc.

like image 4
Dwayne Charrington Avatar answered Oct 21 '22 22:10

Dwayne Charrington