Errors & Messaging
Reading and rendering validation errors, and sending messages to the user.
Error Handling
addError
Adds an error message to the error bag. One error per key — adding a second error for the same key replaces the first.
$form->addError('email', 'Invalid email format');
Parameters:
$key-string- The field name.getError()/hasError()look it up by this.$message-string|array- The message. Pass an array to supply one message per rule.$rule-string(optional) - The rule this error came from. Only needed when you want Flick's built-in message for that rule.$matches-string|array(optional) - Values to substitute into the message. A string fills:match; an array fills:match1,:match2… (an array never fills a bare:match, and vice versa).:keyis always replaced with$key.
Leave $rule off for a plain custom message. Supply it when you want Flick to pick
the message for you:
// custom message, used as-is $form->addError('email', 'Invalid email format'); // let Flick write the message for the `min` rule, filling in :key and :match $form->addError('username', '', 'min', '3'); // -> "username must be at least 3 characters"
hasError
Checks if an error exists for a specific key.
if ($form->hasError('email')) { // Handle email error }
getError
Retrieves an error message for a specific key.
$emailError = $form->getError('email');
getErrors
Retrieves all error messages.
$allErrors = $form->getErrors();
deleteError
Removes an error message for a specific key.
$form->deleteError('email');
errorsIsEmpty / errorsIsNotEmpty
Checks if the error bag is empty or not.
if ($form->errorsIsEmpty()) { // No errors }
ok
Alias for errorsIsEmpty - checks if there are no errors.
if ($form->ok()) { // No errors, proceed }
Messaging
errorMessage
Prints an error message.
if (! $form->ok()) { $form->errorMessage('There was an error'); }
successMessage
Prints a success message.
if ($form->ok()) { $form->successMessage('Form submitted!'); }
infoMessage
Prints an info message.
$form->infoMessage('Here is some helpful information');
warningMessage
Prints a warning message.
$form->warningMessage('You probably should not do that');
errors
Print an alert box containing an unordered list of every error in the error bag.
$form = new Flick(['showErrorsAlert' => true]); // ... if (! $form->ok()) { $form->errors(); // default heading $form->errors('Please fix these'); // your own heading }
It follows the echo config the same way the other message helpers do. By
default it prints the alert and returns an empty string. With 'echo' => false
— the mode you want in Blade — nothing is printed and the markup comes back for
you to output yourself:
$form = new Flick(['showErrorsAlert' => true, 'echo' => false]); // ... echo $form->errors();
Warning
errors() only produces an alert when the
showErrorsAlert config value is true.
It is false by default, in which case this method returns an empty string and
prints nothing.