patternphpMinor
Validating a User via Save Method
Viewed 0 times
methoduservalidatingsavevia
Problem
I'm fairly new to object oriented programming here and am trying to get my head around the best way to go about creating Users and Validating them in the system.
My first attempt is below and my main questions apart from does it all seem correct is in regards passing a validator class to the save function and pros/cons to this.
A lot of the functionality has been stripped back to enable you to read it a bit easier, including removing any getters and setters but please assume they are there:
User Model
Abstract Validator
Name Validator
Email Validator
```
class EmailValidator extends ValidatorAbstract {
protected $email;
protected $errors;
public function __construct( $email ) {
$this->email = $email;
}
public function validate() {
if( !filter_var( $this->email, FILTER_VALIDATE_EMAIL ) ) :
$this->errors[] = 'Invalid Email';
My first attempt is below and my main questions apart from does it all seem correct is in regards passing a validator class to the save function and pros/cons to this.
A lot of the functionality has been stripped back to enable you to read it a bit easier, including removing any getters and setters but please assume they are there:
User Model
class User implements Validateable {
protected $name;
protected $email;
protected $validator;
// getters and setters for name, email, validator here
public function save( Validator $validator ) {
$this->validator = $validator;
if( $this->validator->validate( $this ) )
return true;
return false;
}
public function getRules() {
return [
'name' => new NameValidator( $this->name ),
'email' => new EmailValidator( $this->email )
];
}
}Abstract Validator
abstract class ValidatorAbstract {
protected $errors;
public function getErrors() {
return $this->errors;
}
}Name Validator
class NameValidator extends ValidatorAbstract {
protected $name;
protected $errors;
public function __construct( $name ) {
$this->name = $name;
}
public function validate() {
if( empty( $this->name ) )
$this->errors[] = 'Invalid Name';
if( $this->errors )
return false;
return true;
}
}Email Validator
```
class EmailValidator extends ValidatorAbstract {
protected $email;
protected $errors;
public function __construct( $email ) {
$this->email = $email;
}
public function validate() {
if( !filter_var( $this->email, FILTER_VALIDATE_EMAIL ) ) :
$this->errors[] = 'Invalid Email';
Solution
It's better to separate the validation from the save operation and to use a constraint object to setup the rules which be applied to the entity. So you get a better separation of concern, which is one of the keys to good OOP architecture. I propose something like this:
I advise you to check the validator component from symfony2: https://github.com/symfony/Validator which is really well designed.
$user = new User;
$user->setName( 'Peter' );
$user->setEmail( 'peter@email.com' );
$validator = new Validator(new UserConstrainte));
if( $validator->validate($user)){
$databaseStuff->save($user)
} else {
$error = $validator->getErrors()
}I advise you to check the validator component from symfony2: https://github.com/symfony/Validator which is really well designed.
Code Snippets
$user = new User;
$user->setName( 'Peter' );
$user->setEmail( 'peter@email.com' );
$validator = new Validator(new UserConstrainte));
if( $validator->validate($user)){
$databaseStuff->save($user)
} else {
$error = $validator->getErrors()
}Context
StackExchange Code Review Q#85600, answer score: 2
Revisions (0)
No revisions yet.