I'm using Form Request Validation
to validate a form, but when I leave a field of type required
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();
}
}