Validation rules that rely in other attributes that not itself (like confirmed that requires foo_confirmation) will always fail.
All validation attributes that are not $fillable will always fail too. In case someone wants to validate a field but not store it.
Cases
Password confirmation:
$rules = ['password' => 'confirmed'];
$fillable = ['foo', 'password'];
--
User::create(Input::only('foo', 'password', 'password_confirmation')); // Even if matches, validation fails.
Cause
Attributes passed to Validator::make() are the model attributes which some are previously removed by $fillable rules.
We also don't want to make fields like "password_confirmation" fillable as it will throw a QueryException because that collumn doesn't exist.
Fix
Use all the input received instead of $this->attributes so everything can be validated. (Like we would do in a "traditional validation workflow" if we can call it that way)
First of all... Happy new year :)
Problem
Validation rules that rely in other attributes that not itself (like
confirmed
that requiresfoo_confirmation
) will always fail. All validation attributes that are not$fillable
will always fail too. In case someone wants to validate a field but not store it.Cases
Password confirmation:
Cause
Attributes passed to
Validator::make()
are the model attributes which some are previously removed by $fillable rules. We also don't want to make fields like "password_confirmation" fillable as it will throw a QueryException because that collumn doesn't exist.Fix
Use all the input received instead of
$this->attributes
so everything can be validated. (Like we would do in a "traditional validation workflow" if we can call it that way)