Skip to content

08. Validators

Simon Striekwold edited this page Apr 3, 2018 · 2 revisions

The default validators:

Make a new validator

address

Calls the google maps API with the supplied data. This function assumes an address structure of:

$this->address = [
    'Street name',
    'House number',
    'City',
    'Zipcode'
];

Any of these can be left empty, the first result of the API call will be used to fill in the data. This function only returns false if there are no results from the API.

If the address is valid the supplied property will be changed to a string with the filled in data:

$this->address = "Street name, House number, Zipcode, City, Country";

date

Converts the Model property from an array of [ day, month, year ], to a string of d/m/Y:H:i:s.

image

If the image has a size of more than zero this function tries to save the image, and remove the previous image if it was set. If the image has a size of zero (so no image was uploaded) the default image, as set in the config, will be used.

The supplied property will be changed to a string with the path of the image.

in

This validator performs an in_array() check, to see if the supplied property is in the array supplied in the rule. For more information about how to use this rule see the Model wiki entry.

password

This validator must check exactly 2 properties, and checks if they are the same.

required

Checks if all the supplied properties have some value.

type_double

Checks if all the supplied properties are doubles.

type_integer

Checks if all the supplied properties are integers.

type_string

Checks if all the supplied properties are strings.

unique

Checks that another database row in this models table doesn't have the same value in the same column.

Make a new validator

The validate() function calls the validators with, the rule array and $this as arguments.

<?php // name.php

    class Name {

        public static function validate( $rule, &$model ) {

            // $rule = [ ['prop1', 'prop2', 'etc'], 'name' ];

            foreach ( $rule[0] as $prop ) { // loops through all properties of the rule

                // checks
                
            }

            return true;
            
        }

    }
Clone this wiki locally