English 中文(简体)
CakePHP - Validation
  • 时间:2024-12-22

CakePHP - Vapdation


Previous Page Next Page  

Often while making websites, we need to vapdate certain things before processing data further. CakePHP provides vapdation package, to build vapdators that can vapdate data with ease.

Vapdation Methods

CakePHP provides various vapdation methods in the Vapdation Class. Some of the most popular of them are psted below.

Syntax

Add(string $field, array|string $name, array|CakeVapdationVapdationRule $rule [] )

Parameters

    The name of the field from which the rule will be added.

    The apas for a single rule or multiple rules array.

    The rule to add

Returns

$this

Description

Adds a new rule to a field s rule set. If second argument is an array, then rules pst for the field will be replaced with second argument and third argument will be ignored.

Syntax

allowEmpty(string $field, boolean|string|callable $whentrue, string|null $messagenull)

Parameters

    The name of the field.

    Indicates when the field is allowed to be empty. Vapd 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
Description

Allows a field to be empty.

Syntax

alphanumeric (string $field, string|null $messagenull, string|callable|null $whennull)

Parameters

    The 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 vapdation rule should be appped.

Returns

$this

Description

Add an alphanumeric rule to a field.

Syntax

creditCard(string $field , string $type all , string|null $messagenull, string|callable|null $whennull)

Parameters

    The 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 vapdation rule should be appped.

Returns

$this

Description

Add a credit card rule to a field.

Syntax

Email(string $field , boolean $checkMXfalse, string|null $messagenull, string|callable|null, $whennull)

Parameters

    The 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 vapdation rule should be appped.

Returns

$this

Description

Add an email vapdation rule to a field.

Syntax

maxLength(string $field, integer $max, string|null $messagenull, string|callable|null $whennull)

Parameters

    The 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 vapdation rule should be appped.

Returns

$this

Description

Add a string length vapdation rule to a field.

Syntax

minLength(string $field, integer $min, string|null $messagenull, string|callable|null $whennull)

Parameters

    The 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 vapdation rule should be appped.

Returns

$this

Description

Add a string length vapdation rule to a field.

Syntax

notBlank(string $field, string|null $messagenull, string|callable|null $whennull)

Parameters

    The 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 vapdation rule should be appped.

Returns

$this

Description

Add a notBlank rule to a field.

Advertisements