i have a data validation class method where i check the user input before inserting the records into the database. i would like to apply many rules per field method for example
field email will have to validate the following
a) validate email using FILTER_VALIDATE_EMAIL
b) check if duplicate mail exist in database(this does not apply everytime)
field name should have to validate the following.
a) only a-z or A-Z with space are 开发者_运维问答allowed
b) it should be minimum 5 and maximum 40 characters
and so on i would want to make sure that i should be able to apply many rules per field and it should be optional as well.
here is the original code i was using.
public function validate() {
if(!empty($this->name)) {
if(!preg_match('/^[a-zA-z ]{3,50}$/',$this->name)) {
$this->error['name'] = 'Name should be valid letters and should be between 3 and 25 characters';
}
}
if(!empty($this->email)) {
if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)) {
$this->error['invalidEmail'] = 'Invalid email address';
}
if($this->emailCount($this->email)) {
$this->error['emailExist'] = 'Email already exist';
}
}
if(!empty($this->password)) {
$this->password = trim($this->password);
if(strlen($this->password) < 5 || strlen($this->password > 40)) {
$this->error['password'] = 'Password length should be between 5 and 40 characters';
}
}
if(!empty($this->pPhone)) {
if(!preg_match('/^[0-9]{5,10}$/',$this->pPhone)) {
$this->error['invalidpPhone'] = 'Invalid primary phone number';
}
}
if(!empty($this->sPhone)) {
if(!preg_match('/^[0-9]{5,10}$/',$this->sPhone)) {
$this->error['invalidsPhone'] = 'Invalid secondary phone number';
}
}
return (empty($this->error)) ? true : false;
}
instead of using lots of if condition i want to use switch case with multi dimensional array with something like this.
var $validate = array(
'name' => array(
'notEmpty'=> array(
'rule' => 'notEmpty',
'message' => 'Name can not be blank.'
),
'allowedCharacters'=> array(
'rule' => '|^[a-zA-Z ]*$|',
'message' => 'Name can only be letters.'
),
'minLength'=> array(
'rule' => array('minLength', 3),
'message' => 'Name must be at least 3 characters long.'
),
'maxLength'=> array(
'rule' => array('maxLength', 255),
'message' => 'Name can not be longer that 255 characters.'
)
),
'email' => array(
'email' => array(
'rule' => 'email',
'message' => 'Please provide a valid email address.'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This E-mail used by another user.'
)
)
);
i am getting confused on how to implement mt code to be compatible with the later one. with regard to my original i will be thankful if someone demonstrate me with an example about implementing validation with the later one.
thank you.
I would put this into some functions:
// Validation methods
function checkMail($mail) {
// perform mail checks
if(!valid)
throw Exception("not valid mail");
}
function checkMinLength($string) {
// perform length
if(!valid)
throw Exception("not valid length");
}
// mapping fields - methods
$mappingArray = array('mailfield' => array('checkMail'), 'lengthfield' => array('checkMinLength');
// perform checking
try {
foreach($arrayContaintingYourFields as $field) {
foreach($mappingArray[$field['name']] as $check) {
call_user_func($check, $field['value']);
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
You can influence the error handling by defining own exception types and reacting in different ways.
} catch (MailException $e) {
echo $e->getMessage();
} catch (LengthException $e) {
// do some other stuff
}
精彩评论