PHP class for form validation

PHP class for form validation

PHP plays great role in the form values sensitization. PHP provide filters with the help of that you plays with form validation & sensitization of form values. PHP provides three types of filter which is below.

  •  Validate filters
  •  Sanitize filters
  •  Other filters

Validate filters
PHP provide in built function for some values validate like email, url, IP address, int , float & regx validation.
for more information

Sanitize filters
Sanitize filters are used for making valid input so that in future it will create the hole in application. With maintian validation user can be input such a values that can break the functionlity like not inserted in the database or can input the code that can be used in the future. So Sanitize filters can be filter the user inputs.

Other filters
PHP faciliate to developer he can be add our custom function. That function can be call at time of validation.

So with the usage of these function ii try to create  a class that can be used for validation form. The class that i have written in which covered the below validation.

  • Email validation
  • Required validation
  • Numeric validation
  • positive numeric validation
  • positive integer validation
  • URL
  • Alpha
  • Alphanumeric
  • Unique value in database
  • two parameter field value comparison
  • Max & Min character limit
  • Photos
  • upload validation

PHP Form validation Library

So below i will show you structure of the class how to be processed. We cretae the seprate function each above validation. which is shown to you in below code.

You can get here code of PHP Form Validation Library

Use of PHP Form validation Library

Now show to how to be use this class. So, First need to initialize the Class with config array, from the config array it will get the data & process it. For different validation how to call. In below code first need to initialize the array. Then provide the one dimensional array to validate for array variable [array_to_validate]. Here just need to pass the name of the element the class will process there values & return the validation status. For example [$config[’email’]] assign account_email, alternative_email textbox name so that it can be validate.

Now let me show you with code.

$config = array();
$config['array_to_validate'] = $_POST or array_need_to_validate;
$config['email'] 	     = "email,forgot_email";
$config['required']	     = "date_birth,phone,mobile";
$config['numeric']	     = "mobile";
$config['positive_numeric']  = "positive_number";
$config['positive_integer']  = "positive_integer";
$config['url']		     = "url";
$config['alpha']	     = "name";
$config['alphanumeric']	     = "user_password";

Now its some thing different because this validation the database. field_name means provide the textbox name or element that need to validation.

Implimentation of unique value in database like user name:

$config['unique_from_table']= array(
				array(
				'field_name' =>'',
				'table_name' =>'user',
				'table_field'=>'username'
				),
				array(
				'field_name' =>'',
				'table_name' =>'users',
				'table_field'=>'username'
				)
			);

Comparasion


$config['compare']= array(
			array(
				'field_name' =>'username',
				'compare_field_name' =>'firstname',
			),
			array(
			  'field_name' =>'lastname',
			  'compare_field_name' =>'firstname'
			)
		);

Character Limit Validation


$config['min_character_limit']= array(
				    array(
					'field_name' =>'username',
					'no_of_character' =>'20',
				    ),
				    array(
					'field_name' =>'lastname',
					'no_of_character' =>'30'
				    )
				);

$config['max_character_limit'] = array(
								array(
									'field_name' =>'username',
									'no_of_character' =>'20',
								),
								array(
									'field_name' =>'lastname',
									'no_of_character' =>'30'
								)
							);

Finally call the class & pass all things which we have built in above. Then class will process your inputs & return the result. If there is validation error then return array of error otherwise pass it.


$my_validator = new validator($config)
$error = $my_validator->process_validation();

1 thought on “PHP class for form validation

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons