phalcon Validation Built in Validators


Example

PresenceOf - Validates that a value is not null or empty string

$validator->add('name', new \Phalcon\Validation\Validator\PresenceOf([
   'message' => 'The name is required'
]));

Email - Checks if a value has a correct e-mail format

$validator->add('email', new \Phalcon\Validation\Validator\Email([
   'message' => 'The e-mail is not valid'
]));

Identical - Checks if a value is identical to other

$validator->add('terms', new \Phalcon\Validation\Validator\Identical([
   'accepted' => 'yes',
   'message' => 'Terms and conditions must be accepted'
]));

Url - Checks if a value has a url format

$validator->add('url', new \Phalcon\Validation\Validator\Url([
   'message' => ':field must be a url'
]));

Confirmation - Checks that two values have the same value

$validator->add('password', new \Phalcon\Validation\Validator\Confirmation([
   'message' => 'Password doesn\'t match confirmation',
   'with' => 'confirmPassword'
]));

StringLength - Validates that a string has the specified maximum and minimum constraints The test is passed if for a string’s length L, min<=L<=max, i.e. L must be at least min, and at most max.

$validation->add('name_last', new \Phalcon\Validation\Validator\StringLength([
      'max' => 50,
      'min' => 2,
      'messageMaximum' => 'We don\'t like really long names',
      'messageMinimum' => 'We want more than just their initials'
]));

Regex - Allows validate if the value of a field matches a regular expression

$validator->add('created_at', new \Phalcon\Validation\Validator\Regex([
   'pattern' => '/^[0-9]{4}[-\/](0[1-9]|1[12])[-\/](0[1-9]|[12][0-9]|3[01])$/',
   'message' => 'The creation date is invalid'
]));

CreditCard - Checks if a value has a valid creditcard number

$validator->add('creditcard', new \Phalcon\Validation\Validator\CreditCard([
   'message' => 'The credit card number is not valid'
]));

Between - Validates that a value is between an inclusive range of two values. For a value x, the test is passed if minimum<=x<=maximum.

$validator->add('name', new \Phalcon\Validation\Validator\Between([
   'minimum' => 0,
   'maximum' => 100,
   'message' => 'The price must be between 0 and 100'
]));

ExclusionIn - Check if a value is not included into a list of values

$validator->add('status', new \Phalcon\Validation\Validator\ExclusionIn([
   'message' => 'The status must not be A or B',
   'domain' => ['A', 'B']
]));

InclusionIn - Check if a value is included into a list of values

$validator->add('status', new \Phalcon\Validation\Validator\InclusionIn([
   'message' => 'The status must be A or B',
   'domain' => ['A', 'B']
]));

Uniqueness - Check if a value is uniqueness

$validator->add('login', new \Phalcon\Validation\Validator\Uniqueness([
    'message' => 'The login must be unique',
    'model' => new Users()
]));