Laravel throw Error from model if condition doesn't work
I am doing all validation in Model
My rule
public static $rules = array(
'VehicleNumber' => 'required|unique:vehicle',
'NumberSeats' => 'required',
'VehicleType' => 'required',
'MaximumAllowed' => 'numeric|max:3',
'VehicleCode' => 'required|unique:vehicle');
}
But to change the filename and check its size, I process in SetFooAttribute
public function setInsurancePhotoAttribute($InsurancePhoto)
{
if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000')
{
$this->attributes['InsurancePhoto'] = Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension();
Input::file('InsurancePhoto')->move('assets/uploads/vehicle/', Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension());
}
}
In condition
if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000')
If this fails, saving the customization attribute will fail.
But how can I throw an error to the user in this case?
+3
source to share
2 answers
You can throw an Exception with an error message.
Model
if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000') {
// process image
} else {
throw new \Exception('Error message');
}
In the controller (where you call validation) you can catch this exception and print it to the user.
0
source to share
getSize () returns an integer, so you shouldn't use a string for comparison as you wrote 1000 in single quotes. You can create your own validation in laravel. Check the links fooobar.com/questions/1245919 / ... and http://culttt.com/2014/01/20/extending-laravel-4-validator/
0
source to share