Validation

Validation rules will add an extra layer of security and can used to steer user input.

Class: Validate

In this table you'll find information about the different methods you can use and what kind of data can be passed as an argument.

Method Arg Type Expect
rules() 1 array rules
input() 1 string html input name
as() 1 string html input name alias

Class: Errors

In this table you'll find information about the different methods you can use and what kind of data can be passed as an argument.

Method Arg Type Expect
get() 1 array validation rules
get() 2 string html input name

Type of rules

In this table you'll find information about what kind of validation rules can be applied.

Rule Type Expect Description
require bool true To require an input value.
min int number of mininum amount To determine the minimum amount of characters.
max int number of maximum amount To limit the amount of input value characters.
csrf string csrf token input value To validate the csrf token.
special bool true To not allow certain special characters.
alphanumeric bool true To not allow special characters.
unique string input value To ensure the value is unique.
match string input value to match To ensure the input value matches another input value.
min-one-admin array records of admin ids To ensure there always is one type of admin user existing.

Creating validation rules

Add a function method inside the Rules class and create an instance of the Validate class to call the input function method. Inside the as function method, the argument is some sort of alias for the html input name. When the validation rules inside a view are displayed, the alias will be shown instead of the input name itself. Finally, apply the validation rules inside the rules function method.


<?php
                
  namespace validation;
                
  use core\validation\Validate;
                
  class Rules {
                
    public $errors;
                 
    public function exampleRules($request) {    
                
      $validation = new Validate();
                
      $validation->input(['name' => $request['name']])->as('Alias')->rules(['required' => true, 'min' => 5, 'max' => 99, 'special' => true]);
      $validation->input(['name' => $request['name']])->as('Alias')->rules(['required' => true, 'min' => 8]);
      $validation->input(['name' => $request['name']])->as('Alias')->rules(['required' => true, 'max' => 20]);

      $this->errors = $validation->errors;
      return $this;
    }
                
    public function validated() {    
                
      if(empty($this->errors) ) {
                
        return true;
      } 
    }       
  }     

Validating rules

Call an existing function method from the Rules class and use the validated function method to validate the validation rules.

<?php

  namespace app\controllers;
                
  use validation\Rules;
                
  class ExampleController extends Controller {
                  
    public function example($request) {    
                        
      $rules = new Rules();

      if($rules->exampleRules($request)->validated() === true) {
                  
        /**
        * Input values are valid    
        * No validation errors   
        *   
        */
                
      } else {
                  
        $data['rules'] = $rules->errors;
        return $this->view('example')->data($data);
      }
    }       
  }       

Display validation rules

Call the get function method from the Rules class to display the valiation rules. The second argument should match the html input name.

<form action="" method="POST">                
  <div class="container">                
    <label for="inputId">LabelName:</label>                
    <input type="text" name="inputName"/>                
    <div class="validation-rules">                
      <?php echo validation\Errors::get($rules, 'name'); ?>                
    </div>                
  </div>                          
  <div class="container">                
    <input type="hidden" name="token" value="<?php core\Csrf::token(); ?>"/>
    <button type="submit" name="submit">Submit</button>                
  </div>                
</form>