How to add the validator?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
12 views
Hi. Writing your bike ie, OOP, MVC framework. In the process decided not to write checks in this Account and to write a separate validation class forms (in the end, I pulled from github =)).

The idea was that the field it is possible to apply several methods of validation, for example:
  1. The username field is required.
  2. Login must consist only of russkis letters.
  3. Login must consist of at least 3 characters.


That is, to every wrong action spilling out on the 1st message, not a stack or even in one line.

The code that I got working 100%, but in the end, faced with the fact that I can't with this class to check, say on the existence of the email in the database...

In a situation when a database already has a password, email or username need to show the message that any, but I don't doganyaesh what to do. To climb in a DB in this class makes no sense, because it is the task of the model account.

In this class it is possible to do such checks? Can tell another class about the same complexity.

In General I will be glad to any good advice.

Code validator:
<?phpnamespace 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; } } }


The controller propisyvaetsya all of the following:
public function indexAction() { if (isset($_POST['submit'])){ $name = $_POST['name']; Validator::addRule( 'name', 'The name field is required', 'required'); Validator::addRule( 'name', The name must consist of not less than 2 characters', 'minlength', 3); Validator::addEntries($_POST); Validator::validate(); $errors = Validator::getErrors(); } }
by | 12 views

2 Answers

0 like 0 dislike
To retrieve data from the database you need to use the model, in this case the model User.
Then you just plug in your model you need in any class and take methods from the base model is what you need
by
0 like 0 dislike
There is a fully adequate component validation, see the Symphony, for example. Why drag some murky code. The same way about "Write to your bike ie, OOP, MVC framework". Writing frameworks is not so, use prefabricated components to the maximum.

I'm not in this class to write queries to the database to check the existence of email or the username, or password.

Yeah, you can't, you have to model. You should refer to the corresponding method of the model of your validator.
by
110,608 questions
257,186 answers
0 comments
35,390 users