Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php namespace App\Http\Requests;
  2.  
  3. use App\Http\Requests\Request;
  4.  
  5. class StoreBidRequest extends Request {
  6.  
  7.     /**
  8.      * Determine if the user is authorized to make this request.
  9.      *
  10.      * @return bool
  11.      */
  12.     public function authorize()
  13.     {
  14.         return true;
  15.     }
  16.  
  17.     /**
  18.      * Get the validation rules that apply to the request.
  19.      *
  20.      * @return array
  21.      */
  22.     public function rules()
  23.     {
  24.         //first list the rules that apply in any case
  25.  
  26.         $input = $this->input();
  27.  
  28.         $rules = [
  29.             'trip_id' => 'required',
  30.             'method_of_transport' => 'required',
  31.             'partial_bids_accepted' => 'required',
  32.             'collection' => 'required',
  33.             'duty_applicable' => 'numeric',
  34.             'import_tax_vat' => 'numeric',
  35.             'additional_charges' => 'numeric'
  36.         ];
  37.  
  38.         //then add some fancy validation logic based on input
  39.         if($input['method_of_transport'] == 'Rail' && $input['partial_bids_accepted']){
  40.             $rules += [
  41.                 'price_per_tonne' => 'required|numeric',
  42.                 'transport_min_tonnes' => 'required|numeric',
  43.                 'transport_max_tonnes' => 'required|numeric'
  44.             ];
  45.         }
  46.  
  47.         //now we've dealt with validation the proper way, keeping the controller logic clean
  48.         return $rules;
  49.     }
  50.     //set custom message for each field / rule
  51.     public function messages()
  52.     {
  53.         return [
  54.             'price_per_tonne.required' => 'You must set a price per tonne',
  55.             'price_per_tonne.numeric' => 'Price per tonne must be a number',
  56.         ];
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement