Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions en/core-libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,30 @@ callable, including anonymous functions, as validation rules::

// Use a global function
$validator->add('title', 'custom', [
'rule' => 'validate_title'
'rule' => 'validate_title',
'message' => 'The title is not valid'
]);

// Use an array callable that is not in a provider
$validator->add('title', 'custom', [
'rule' => [$this, 'method']
'rule' => [$this, 'method'],
'message' => 'The title is not valid'
]);

// Use a closure
$extra = 'Some additional value needed inside the closure';
$validator->add('title', 'custom', [
'rule' => function ($value, $context) use ($extra) {
// Custom logic that returns true/false
}
},
'message' => 'The title is not valid'
]);

// Use a rule from a custom provider
$validator->add('title', 'unique', [
'rule' => 'uniqueTitle',
'provider' => 'custom'
'provider' => 'custom',
'message' => 'The title is not unique enough'
]);

Closures or callable methods will receive 2 arguments when called. The first
Expand Down