Validation

Often while making websites, we need to validate certain things before processing data further. CakePHP provides validation package, to build validators that can validate data with ease.

Validation Methods

CakePHP provides various validation methods in the Validation Class. Some of the most popular of them are listed below.

SyntaxAdd(string $field, array|string $name, array|Cake\Validation\ValidationRule $rule [] )
ParametersThe name of the field from which the rule will be added.The alias for a single rule or multiple rules array.The rule to add
Returns$this
DescriptionAdds a new rule to a field’s rule set. If second argument is an array, then rules list for the field will be replaced with second argument and third argument will be ignored.
SyntaxallowEmpty(string $field, boolean|string|callable $whentrue, string|null $messagenull)
ParametersThe name of the field.Indicates when the field is allowed to be empty. Valid values are true (always), ‘create’, ‘update’. If a callable is passed, then the field will be left empty only when the callback returns true.The message to show if the field is not.
Returns$this
DescriptionAllows a field to be empty.
Syntaxalphanumeric (string $field, string|null $messagenull, string|callable|null $whennull)
ParametersThe field you want to apply the rule to.The error message when the rule fails.Either ‘create’ or ‘update’ or a callable that returns true when the validation rule should be applied.
Returns$this
DescriptionAdd an alphanumeric rule to a field.
SyntaxcreditCard(string $field , string $type‘all’, string|null $messagenull, string|callable|null $whennull)
ParametersThe field you want to apply the rule to.The type of cards you want to allow. Defaults to ‘all’. You can also supply an array of accepted card types, for example, [‘mastercard’, ‘visa’, ‘amex’].The error message when the rule fails.Either ‘create’ or ‘update’ or a callable that returns true, when the validation rule should be applied.
Returns$this
DescriptionAdd a credit card rule to a field.
SyntaxEmail(string $field , boolean $checkMXfalse, string|null $messagenull, string|callable|null, $whennull)
ParametersThe field you want to apply the rule to.Whether or not to check the MX records.The error message when the rule fails.Either ‘create’ or ‘update’ or a callable that returns true, when the validation rule should be applied.
Returns$this
DescriptionAdd an email validation rule to a field.
SyntaxmaxLength(string $field, integer $max, string|null $messagenull, string|callable|null $whennull)
ParametersThe field you want to apply the rule to.The maximum length allowed.The error message when the rule fails.Either ‘create’ or ‘update’ or a callable that returns true when the validation rule should be applied.
Returns$this
DescriptionAdd a string length validation rule to a field.
SyntaxminLength(string $field, integer $min, string|null $messagenull, string|callable|null $whennull)
ParametersThe field you want to apply the rule to.The maximum length allowed.The error message when the rule fails.Either ‘create’ or ‘update’ or a callable, that returns true when the validation rule should be applied.
Returns$this
DescriptionAdd a string length validation rule to a field.
SyntaxnotBlank(string $field, string|null $messagenull, string|callable|null $whennull)
ParametersThe field you want to apply the rule to.The error message when the rule fails.Either ‘create’ or ‘update’ or a callable that returns true when the validation rule should be applied.
Returns$this
DescriptionAdd a notBlank rule to a field.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *