Answer the question
In order to leave comments, you need to log in
How to add a validator?
Hello. I am writing my bike i.e. OOP, MVC framework. In the process, I decided not to write validations in the Account model, but to write a separate form validation class (in the end, I stole it from github =)).
The idea was that several validation methods could be applied to a field, let's say:
<?php
namespace components\libs;
class Validator
{
private $field_name;
private $message;
private $type_of_rule;
private $param;
private static $rules = [];
private static $fields = [];
private static $errors = [];
public function __construct($field_name, $message, $type_of_rule, $param)
{
$this->field_name = $field_name;
$this->message = $message;
$this->type_of_rule = $type_of_rule;
$this->param = $param;
}
public static function addRule($field_name, $message, $type_of_rule, $param = null)
{
self::$rules[] = new Validator($field_name, $message, $type_of_rule, $param);
}
public static function addEntries($fields) {
foreach ($fields as $fieldname => $value) {
self::$fields[$fieldname] = self::sanitize($value);
}
}
public static function validate() {
foreach (self::$rules as $rule) {
self::testRule($rule);
}
}
public static function sanitize($text)
{
$text = trim(strip_tags($text));
if (get_magic_quotes_gpc()) {
$text = stripslashes($text);
}
return $text;
}
public static function getErrors()
{
if (count(self::$errors)) {
return self::$errors;
}
return false;
}
public static function longerThan($value, $min)
{
if (strlen($value) >= $min) {
return true;
}
return false;
}
public static function shorterThan($value, $max)
{
if (strlen($value) <= $max) {
return true;
}
return false;
}
public static function asEmail($value)
{
if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
return true;
}
return false;
}
public static function asPhoneNumber($value)
{
if (preg_match("/^\(?[0-9]{3}\)? *-? *[0-9]{3} *-? *[0-9]{4}$/", $value)) {
return true;
}
return false;
}
private static function testRule($rule)
{
if (isset(self::$errors[$rule->field_name])) {
return;
}
if (isset(self::$fields[$rule->field_name])) {
$value = self::$fields[$rule->field_name];
}
else {
$value = null;
}
switch ($rule->type_of_rule) {
case 'required' :
if (empty($value)) {
self::$errors[$rule->field_name] = $rule->message;
return;
}
break;
case 'minlength' :
if (!(self::longerThan($value, $rule->param))) {
self::$errors[$rule->field_name] = $rule->message;
return;
}
break;
case 'maxlength' :
if (!(self::shorterThan($value, $rule->param))) {
self::$errors[$rule->field_name] = $rule->message;
return;
}
break;
case 'email' :
if (!(self::asEmail($value))) {
self::$errors[$rule->field_name] = $rule->message;
return;
}
break;
case 'phonenumber' :
if (!(self::asPhoneNumber($value))) {
self::$errors[$rule->field_name] = $rule->message;
return;
}
break;
}
}
}
public function indexAction()
{
if (isset($_POST['submit'])){
$name = $_POST['name'];
Validator::addRule(
'name',
'Поле name обязательное',
'required');
Validator::addRule(
'name',
'Имя должно состоять не менне чем из 2-х символов',
'minlength',
3);
Validator::addEntries($_POST);
Validator::validate();
$errors = Validator::getErrors();
}
}
Answer the question
In order to leave comments, you need to log in
To get data from the database, you must use a model, in this case, the User model.
Then you simply connect your model to any class you need and take what you need from the database using model methods
There are a lot of adequate validation components, see symphony, for example. Why drag some kind of muddy code. The same, by the way, about "I'm writing my bike, i.e. OOP, MVC framework". Frameworks are written differently, use ready-made components to the maximum.
Yeah, you can't, you have a model for that. You must call the appropriate model method from your validator.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question