Skip to content

Latest commit

 

History

History
executable file
·
65 lines (48 loc) · 1.21 KB

custom-messages.md

File metadata and controls

executable file
·
65 lines (48 loc) · 1.21 KB

Custom messages, placeholders and templates

###Custom Messages

use rock\validate\Validate as v;

$v = v::length(10, 20, true)
            ->setMessages(['length' => 'custom error']);
$v->validate('O’Reilly'); // output: false

$v->getErrors();
/*
output:

[
  'length' => 'custom error',
]
*/

###Custom Placeholders

use rock\validate\Validate as v;

$v = v::length(10, 20, true)
            ->regex('/^[a-z]+$/i')
            ->setPlaceholders(['name' => 'username']);
$v->validate('O’Reilly'); // output: false

$v->getErrors();
/*
output:

[
  'length' => 'username must have a length between 10 and 20',
  'regex' => 'username contains invalid characters',
]
*/

###Custom Templates

use rock\validate\Validate as v;

$v = v::length(10, 20, true)
            ->setTemplates(['length' => Length::LOWER]);
$v->validate('O’Reilly'); // output: false

$v->getErrors();
/*
output:

[
  'length' => 'value must have a length greater than 10',
]
*/

Typically, no need to change the template, because this is comes automatically, depending on the arguments specified. As an example see Length.