2
I’m wearing a Form Request Validation
to validate a form, but when I leave a field of the typerequired
blank no error is generated.
How do I generate an error when validation fails ?
class SaleRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'vendor_id' => 'numeric|required',
'price' => 'numeric|required',
'name' => 'string|required',
];
}
}
Route:
Route::post('/api/list/new/sale', 'SalesController@insert');
Controller:
public function insert(SaleRequest $request)
{
$vendorInformation = $this->sale->insert($request->all());
return response()->json($vendorInformation);
}
Model:
public function insert($request)
{
$request['comission'] = $request['price'] * 0.085;
$sale = $this->create($request);
return $this->informationVendor($sale->attributes['id']);
}
public function informationVendor($idSale)
{
return $this->join('vendors', 'vendors.id', '=', 'sales.vendor_id')
->where('sales.id', $idSale)
->select('vendors.name', 'vendors.email',
'sales.name', 'sales.price', 'sales.comission')
->get();
}
}
what is the version of your language ?
– 13dev
5.4, no view, I am returning a json directly from the controller
– David Santos
edited the question
– David Santos
@Davidsantos you send this data via ajax? and want a return if you have problems with validation?
– novic
i am using Postman to make the requests, wanted to return some error if the validation failed
– David Santos