Validate even faster!

v1.12 2015-11-17 01:51 UTC

This package is not auto-updated.

Last update: 2021-05-04 17:31:16 UTC


Validate even faster with FastValidate!

FastValidate requires Laravel >= 5.0


To install, simply composer require lawrence/fast-validate


Below is an example of a class that extends the BaseModel.


use FastValidate\BaseModel;

class User extends BaseModel {

    protected $fillable = [

    public function getRulesAttribute(){
        return [
            'name' => 'required',
            'email' => 'required|email|unique:users,email,' . $this->id,
            'password' => 'required|min:8'

    public function setPasswordAttribute($value){
        $this->attributes['password'] = bcrypt($value);


Below is an example of how we can use the User class.


// Creating a new model
$user = new User;

// Updating a model
$user = User::where('name', '=', 'Super User')->firstOrFail();

The magic comes from hooking into the saving event hook. When a model is begin saved, it will be grab all relevant input from the Request, validate the input, then populate the model with the input. If the input is not valid, a ValidationException will be thrown, which contains a MessageBag full of the validation errors.

To provide this magic, there is a requirement on the structure of the data. This requirement follows the format model_name.attribute_name. So for the above User example, we will need to send data in the following format:

        '' => 'Johnnie',
        '' => '',
        'user.password' => 'correcthorsebatterystaple'

In the above User example, we have added some complexities. The getRulesAttribute() function means we can dynamically change our validation rules. In this case, we want to make sure that an email is unique, but if we are updating a model with the same email address, then "unique:users,email,$this->id" will avoid this issue.

The setPasswordAttribute($value) function is used, so that we can validate against the password that we are provided, then save the bcrypted value of the password.

Sometimes you might want to create multiple models from the input. We can do this with a form like shown below:

<form action="create-users" method="post">
    <input type="text" name="[]" value="Johnnie">
    <input type="text" name="[]" value="">
    <input type="text" name="user.password[]" value="correcthorsebatterystaple">
    <input type="text" name="[]" value="Tommie">
    <input type="text" name="[]" value="">
    <input type="text" name="user.password[]" value="Tr0ub4dor&3">

Which produces the following data:

    'name' : ['Johnnie', 'Tommie'],
    'email': ['', ''],
    'password': ['correcthorsebatterystaple', 'Tr0ub4dor&3']

And then let the magic happen!

``php <?php $users = User::createFromInput();