The ErrorBag class is a wrapper around an array, and is standalone and has no dependencies, you can use it in your code for any reason:
Available Methods:
add(object error)
adds an error object to the internal array.all()
Gets all messages from the internal array.any()
Checks if there are any errors.clear()
Removes all items from the internal array.collect(String field)
Collects errors associated with a specific field. not passing the field name will group all errors by field name instead.count()
Gets the length of the internal array. or the number of messages.first(String field)
Gets the first error message associated with the specified field.has(String field)
Checks if there is at least one error associated with the specified field.remove(String field)
Removes all errors for a specified field from the internal array.
The validator is injected to the Vue instance as $validator
automatically.
However it is also a stand alone class and can be used separately for programmatically validating values.
The constructor can optionally take an object to map each field name to a set of validations.
validate(field, value)
which should returna boolean if all validations pass. like this:
Promise
The validator is aware of this and will only return a Promise if at least one validation yields a promise. the promise is resolved to boolean which you can later chain to check your fields.
validateAll(obj)
:
Promise
if at least one field validation rule returned a Promise
which is also resolved to a boolean. The ErrorBag will be populated with any errors encountered.
You can access the errorBag
property directly or using getErrors()
.
setLocale
method to switch the validator locale.
setDefaultLocale(string)
which sets the default language for all newly instantiated validators to that language.
Here is an example of using the validator without the directive, which means you will be responsible for monitoring input changes on your own, and calling the API methods as you see fit. This example uses a Vue instance to simplify things, but it can be used in plain JavaScript as well.