Codice PHP:
class userController {
public function add() {
$fv = new formValidator();
$fv->validateEmpty( 'nome-del-campo', 'messaggio di errore' );
$fv->validateRange( 'nome-del-campo', 'messaggio di errore' );
$fv->validateEmpty( 'name', 'Il campo [name] è obbligatorio' );
if ( $fv->hasError() ) {
//mostro i messaggi di errore
} else {
// faccio lavorare il controller
}
}
}
class formValidator {
private $message;
public function __construct() {
$this->message = new ArrayObject();
}
public function validateEmpty( $field, $message ) {
if ( empty( $_POST[$field] ) ) {
$this->message->append( $message );
}
}
public function hasMessage() {
if ( $this->message->count() > 0 ) {
return TRUE;
}
return FALSE;
}
public function getMessage() {
return $this.>message;
}
}
io faccio così