1. 程式人生 > >phalcon的驗證機制

phalcon的驗證機制

Phalcon的Validation有二個層級的,Form, Model ,下面列出各個層面的validation的class

Form:

Name Explanation Example
PresenceOf Validates that a field’s value is not null or empty string.
Identical Validates that a field’s value is the same as a specified value
Email Validates that field contains a valid email format
ExclusionIn Validates that a value is not within a list of possible values
InclusionIn Validates that a value is within a list of possible values
Regex Validates that the value of a field matches a regular expression
StringLength Validates the length of a string
Between Validates that a value is between two values
Confirmation Validates that a value is the same as another present in the data
Url Validates that field contains a valid URL

Model:

Name Explanation Example
PresenceOf Validates that a field’s value isn’t null or empty string. This validator is automatically added based on the attributes marked as not null on the mapped table
Email Validates that field contains a valid email format
ExclusionIn Validates that a value is not within a list of possible values
InclusionIn Validates that a value is within a list of possible values
Numericality Validates that a field has a numeric format
Regex Validates that the value of a field matches a regular expression
Uniqueness Validates that a field or a combination of a set of fields are not present more than once in the existing records of the related table
StringLength Validates the length of a string
Url Validates that a value has a valid URL format

自定義form層級的validator:

<?php

use Phalcon\Validation\Validator,
    Phalcon\Validation\ValidatorInterface,
    Phalcon\Validation\Message;

class IpValidator extends Validator implements ValidatorInterface
{

    /**
     * Executes the validation
     *
     * @param Phalcon\Validation $validator
     * @param string $attribute
     * @return boolean
     */
    public function validate($validator, $attribute)
    {
        $value = $validator->getValue($attribute);

        if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {

            $message = $this->getOption('message');
            if (!$message) {
                $message = 'The IP is not valid';
            }

            $validator->appendMessage(new Message($message, $attribute, 'Ip'));

            return false;
        }

        return true;
    }

}


自定義model層級的validator:

<?php

use Phalcon\Mvc\Model\Validator,
    Phalcon\Mvc\Model\ValidatorInterface;

class MaxMinValidator extends Validator implements ValidatorInterface
{

    public function validate($model)
    {
        $field = $this->getOption('field');

        $min = $this->getOption('min');
        $max = $this->getOption('max');

        $value = $model->$field;

        if ($min <= $value && $value <= $max) {
            $this->appendMessage(
                "The field doesn't have the right range of values",
                $field,
                "MaxMinValidator"
            );
            return false;
        }
        return true;
    }

}