Laravel validate model
I've created an Employer model in my Laravel 4 application and, in
Employer.php I have created the following function to validate user input
before saving it to the database:
public static function validate($input)
{
$validator = Validator::make($input, static::$rules);
if ($validator->fails() {
return $validator;
}
return true;
}
This works fine when I'm creating a new record in the database, because I
am passing in values for all the rules where I have specified a particular
field is required.
However, there are certain fields in the database I don't want the user to
edit after they have been created (for example, business_name). On my edit
method I create a form and omit those fields from the form. But validation
fails because business_name is by the $rules.
As a temporary work around, I tried just creating a hidden field in the
edit form and populating it with the business_name. However, this is also
required to be unique and fails when I PATCH my form to the update method!
Any advice? Is there any way I can specify which validation rules should
be applied depending on the method calling it? Or should I create a new
method in Employer.php specifically to validate on the update method?
No comments:
Post a Comment