📜 ⬆️ ⬇️

HTML Form Validator

Good day, dear% username%.

Every programmer at least once in his life faced with the inherited code. Sometimes this code causes a reaction: "What kind of rubbish | noodles | govnokod, let's rewrite it."

It happened with the project to which I joined. 200-300 string methods, code duplication, procedural approach instead of using OOP did not cause any positive emotions. Fortunately, PM turned out to be a very adequate person and did not dismiss the request to allow time for refactoring. One of these refactorings eventually resulted in a validation plugin for HTML forms, which I want to share.

The reason for creating this plugin was the code of the form:
')
if($this->request->data['Model']['field'] > 0) { $this->Session->setFlash(«message»); $this->redirect($this->referer()); } if($this->request->data['Model']['field'] == «string») { $this->Session->setFlash(«message»); $this->redirect($this->referer()); } 


Which abounded action games. In some actions, the number of similar structures reached a dozen. As a result, the action logic was completely lost in multiple ifs. Often the “collections” of these designs were duplicated in several actions and even controllers. Those who use CakePHP know about the convenient validator of model fields. In my case, it was not always possible to use it directly, fields were passed in forms that were not associated with table fields in models.

After considering the options for refactoring for this code, it was decided to create a class-validator for the data transmitted in the forms. The ModelValidator class was used as the base for it. Unfortunately, its architecture did not allow the use of inheritance. I had to use the good old copy-paste.

The result is a class that allows you to check associative arrays of the form:

 array( 'Model' => array(field_list)) 

using all the power of the Validation class.

Examples of using

HTML form:

  echo $this->Form->create('Model'); echo $this->Form->input('first_field', array('type' => 'text')); echo $this->Form->input('second_field', array('type' => 'text)); echo $this->Form->end(); 


Form class:

 App::uses('Form', 'Forms.Lib/Form'); class ExampleForm extends Form { public $model = 'Example'; public $fields = array( 'first_field' => array( 'type' => 'text', ), 'second_field' => array( 'type' => 'text', ) ); public $validate = array( 'second_field' => array( 'isActive' => array( 'rule' => 'isActive', 'message' => "Is fields not active!" ), ), 'first_field' => array( 'aboveZero' => array( 'rule' => 'aboveZero', 'message' => "Number must be greater than 0", ), ), ); /** * @return bool */ public function isActive(){ if(isset($this->options['user_id'])) { return false; } return (bool)$this->data[$this->model]['second_field']; } /** * @return bool */ public function aboveZero() { return (is_numeric($this->data[$this->model]['first_field']) && ($this->data[$this->model]['first_field'] > 0)); } } 


Using the ExampleForm class to validate form data:

 class ExamplesController extends AppController { public $name = 'Examples'; public $uses = array( 'Example', ); public $components = array( 'Forms.FormValidator' ); public $forms = array( 'ExampleForm', ); /** * Example validation html-form data in $this->request->data */ public function example() { if ($this->ExampleForm->validates($this->request->data)) { echo 'true validations'; } else { echo 'false validation'; } } } 


Using the ExampleForm class to validate an associative array:

  public function example2() { $data = array( 'Example' => array( 'first_field' => 0, 'second_field' => true ) ); if ($this->ExampleForm->validates($data)) { echo 'true validations'; } else { echo 'false validation'; } } 


Using the ExampleForm class to validate an associative array with passing additional data for checks:

  public function example3() { $data = array( 'Example' => array( 'first_field' => 0, 'second_field' => true ) ); $this->ExampleForm->addOptions(array('user_id' => $this->Auth->user('id'))); if ($this->ExampleForm->validates($data)) { echo 'true validations'; } else { echo 'false validation'; } } 


The plans are to automatically add validation rules based on the type of fields described in the class of the form, helpers for displaying the form using different templates.

Plugin code on github .

Source: https://habr.com/ru/post/197928/


All Articles