Skip to main content
Version: Fegg 5.x

Validation::length

Check the exact number of characters

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.

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\Tool\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.