Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php namespace App\Http\Requests;
- use App\Http\Requests\Request;
- class StoreBidRequest extends Request {
- /**
- * Determine if the user is authorized to make this request.
- *
- * @return bool
- */
- public function authorize()
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- *
- * @return array
- */
- public function rules()
- {
- //first list the rules that apply in any case
- $input = $this->input();
- $rules = [
- 'trip_id' => 'required',
- 'method_of_transport' => 'required',
- 'partial_bids_accepted' => 'required',
- 'collection' => 'required',
- 'duty_applicable' => 'numeric',
- 'import_tax_vat' => 'numeric',
- 'additional_charges' => 'numeric'
- ];
- //then add some fancy validation logic based on input
- if($input['method_of_transport'] == 'Rail' && $input['partial_bids_accepted']){
- $rules += [
- 'price_per_tonne' => 'required|numeric',
- 'transport_min_tonnes' => 'required|numeric',
- 'transport_max_tonnes' => 'required|numeric'
- ];
- }
- //now we've dealt with validation the proper way, keeping the controller logic clean
- return $rules;
- }
- //set custom message for each field / rule
- public function messages()
- {
- return [
- 'price_per_tonne.required' => 'You must set a price per tonne',
- 'price_per_tonne.numeric' => 'Price per tonne must be a number',
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement