Skip to main content
Version: Fegg 5.x (next)

Validation::length

Check exact character length

Description​

public boolean Validation::length( string $name, mixed $value, numeric $length, [ mixed $code = '' ] )

Valid when the number of characters (mb_strlen() in UTF-8) is exactly $length.

An empty value ('', null, '0', whitespace only) is not checked and returns true: combine with Validation::required when the field is required. When $name already has an error, the check is skipped and returns false, so only the first error of each field is kept.

Arguments​

NameData typeDefaultRemarks
$namestringField name (key of the error message)
$valuemixedValue to check
$lengthnumericNumber of characters
$codemixed''Error code, array of error code and parameters, or error message (see Configuration)

Example​

Controller
use Fegg\Validation;

$validation = new Validation();

$validation->length('success', '1234', 4, '<strong>PIN</strong> は4桁で入力してください'); // -> true
$validation->length('failed', '12345', 4, '<strong>PIN</strong> は4桁で入力してください'); // -> false

var_dump($validation->getErrorMessage());
// -> array('failed' => '<strong>PIN</strong> は4桁で入力してください')
note

The default validation_error_message.php has no @length code. Pass the error message itself, or add the code to your site/config/validation_error_message.php.