Inheritance vs composition in form validation
Posted: Sat Mar 04, 2006 2:32 pm
Recently we had a very interesting thread about form validation.
Many gave their ideas about strategies for form validation. I had learned myself about the strategy pattern as explained on PHPpatterns. As a response Arborint gave some code, based on a Rules bases Validator class approach.
Some parts of the code:
Inheritance:
Composition:
I have read about inheritance and composition. But I don't understand yet what advantages or disadvantages they have in this situation. Could anyone shed some light on this? Or point me in the right direction?
From a practical point of view, I would prefer the usage of the second method over the first
as the Rules method allows me to set the error messages in the View instead of in the class.
Thanks!
Many gave their ideas about strategies for form validation. I had learned myself about the strategy pattern as explained on PHPpatterns. As a response Arborint gave some code, based on a Rules bases Validator class approach.
To me Feyd seemed positive about such an approach:arborint wrote:It is an interesting study in the change that has happened in the last few years from using inheritence (like the article uses) to composition
Now I have experimented with both approaches: the Strategy pattern of phppatterns using inheritance and the Rules based aproach of Arborint.feyd wrote:Personally, I prefer a class for each rule. With a unified interface, rules are entirely interchangable. Which has amazing power potential and VERY easy to extend/customize
Some parts of the code:
Inheritance:
Code: Select all
class ValidateEmail extends Validator {
/**
* Private
* $email the email address to validate
*/
var $email;
//! A constructor.
/**
* Constucts a new ValidateEmail object subclass or Validator
* @param $email the string to validate
*/
function ValidateEmail ($email){
$this->email=$email;
Validator::Validator();
}
//! A manipulator
/**
* Validates an email address
* @return void
*/
function validate() {
$pattern=
"/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
if(!preg_match($pattern,$this->email)){
$this->setError('Invalid email address');
}
if (strlen($this->email)>100){
$this->setError('Address is too long');
}
}
}Code: Select all
class RuleEmail extends Rule {
function RuleEmail($field, $errorMsg) {
$this->field = $field;
$this->errorMsg = $errorMsg;
}
function isValid($request) {
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\.|\{\}~\']+';
$doIsValid = '(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.?)+';
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
return (preg_match("/^$user@($doIsValid|(\[($ipv4|$ipv6)\]))$/", $request[$this->field]));
}
} // end class RuleEmailFrom a practical point of view, I would prefer the usage of the second method
Code: Select all
$validator->addRule(new RuleEmail('email', 'This is not a valid email address'));Code: Select all
$v['e']=new ValidateEmail($_POST['email']);Thanks!