In Part One, we explored some securing form logic. In That Article, we extend form validation class by writing validation method like doesn't allow to send empty input value, restrict (minimum or maximum) characters before sending a form or check input value is an email address or not.
Lists of method and properties going to declare in class.
Everyone gets a form validation class from Github with an example.
Lists of method and properties going to declare in class.
- Declare class properties.
- Rework in previous declared methods.
- Include a method for getting the form field value.
- Include method check form post request.
- Include validations method.
Declaring three class properties
public $isform;
public $errors = [];
private $formData = [];
The $isform property, later on, will use in the validation method. $errors property will use for validation messages and last $formData will hold the form field values.Rework in previous declared methods
public function __construct(){
switch($_SERVER['REQUEST_METHOD']):
case 'GET':
$this->get();
break;
case 'POST':
$this->post();
break;
endswitch;
}
private function post($arg = ''){
$post = $_POST;
$this->userInput($post);
$this->formData = (!empty($arg) && !empty($post)) ? $post[$arg] : $post;
}
private function get($arg = ''){
$get = $_GET;
$this->userInput($get);
$this->formData = (!empty($arg) && !empty($get)) ? $get[$arg] : $get;
}
private function userInput($data){
$arg = [];
// clean user input logic here
foreach($data as $k => $val):
$val = trim($val);
$val = stripslashes($val);
$val = htmlspecialchars($val);
$arg[$k] = $val;
endforeach;
return $arg;
}
Main Reason for rework in the declared method is pushing form data in the $formData class property.Getting the Form field value
public function input($fieldname = ''){
if(empty($fieldname)):
return $this->formData;
else:
if(array_key_exists($fieldname,$this->formData)):
return $this->formData[$fieldname];
else:
return '';
endif;
endif;
}
Method input() takes one parameter. it will return all the form field value if not send any form field name in the first argument.Required input field Validation
private function required($fieldName){
$field = $this->input($fieldName);
if(empty($field)):
$this->errors[$fieldName][] = "The $fieldName is required";
$this->isform = true;
endif;
}
The method required() is a private not accessible outside the class. It takes form field name in the first parameter. If the posted form field is empty that method set $isform to true and set validation message in $errors property.Email input field Validation
private function validEmail($fieldName){
$field = $this->input($fieldName);
if(!filter_var($field, FILTER_VALIDATE_EMAIL)):
$this->errors[$fieldName][] = 'Email is invalid';
$this->isform = true;
endif;
}
The method validEmail() also have similar required() method logic. It takes one argument, if not valid email then set $isform to true and push validation error message in $errors property.Set validation rules method
public function addRule($fieldNm,$ruleNm){
if($_SERVER['REQUEST_METHOD'] = 'post' && empty($this->formData) ){
$this->isform = true;
return $this;
}
if(!empty($ruleNm)):
$validationRules = explode('|',$ruleNm);
foreach ($validationRules as $validationRule):
switch ($validationRule):
case 'required':
$this->required($fieldNm);
break;
case 'validEmail':
$this->validEmail($fieldNm);
break;
endswitch;
endforeach;
endif;
}
These validations method is not accessible outside the class because it's private. So we make an addRule() method which is allowed to set validation rules.//set single validtion rule
$formValidation->addRule('fieldname','required');
//set multiple validation rule
$formValidation->addRule('fieldname','required|validEmail');
The method addRule() takes two arguments. The first form field name and second rules name '|' pipe sign separated.public function isFailed(){
$isFail = $this->isform;
return $isFail;
}
The method isFailed() is checked form validation Failed or successful. Hopefully that article helpful.Everyone gets a form validation class from Github with an example.
0 comments:
Post a Comment