text: Q:
  
  Laravel request rules method validation switch case issue HTTP method
  
  I am developing a website with Laravel 5 and Angularjs, I send the request to save data through this Angular Ajax method:
  save : function(screen_route, id=null) {
  
              var default_screen_route = screen_route;
              //var self = this;
              //params = $('#store_form').serializeArray();
              //console.log(params);
  
              var store_form = document.getElementById('store_form');
              form_data = new FormData(store_form);
  
              if (document.getElementById("id")== null)  
              {
                  form_data.append('_method', "POST");
                  var method = 'POST';
              } else {
  
                  //---------- this line because $.ajax doesn't support PATCH method.
                  form_data.append('_method', "PATCH");
                  var method = 'POST';
  
                  id = document.getElementById("id").value;
                  screen_route = screen_route + '/' + id;
              }
  
              /*
              return $http({
                  method: method,
                  url: screen_route,
                  headers: { 'Content-Type' : undefined },
                  data: form_data,
              });
              */
  
              return $.ajax({
                  type: method,
                  url: screen_route,
                  async: false,
                  data: form_data,
                  processData: false,
                  contentType: false,
                  success: function (response) {
                      if(response.success) {
  
                          //----- sweet alert plugin -----
                          swal({
                                title: '',
                                text: response.success,
                                type: "success",
                                timer: 3000,
                                showConfirmButton: false
                          });
  
                      }
                      if(response.error) {
                          swal({
                                title: '',
                                text: response.error,
                                type: "error",
                                showConfirmButton: false
                          });
                      }
  
                      var current_id = response.id;
                      $http.get(default_screen_route +'/' + current_id ).success(function(response){
                          $('#dv_content').html(response);
                          $("#dv_save_btns").hide();
                          $("#dv_show_btns").show();
                          $("#dv_pagination").hide();
  
                          $( "#lnk_show_btns_edit" ).attr( "row_id", current_id );
                      });
  
                  },
                  error: function (response) {
                      var obj = response.responseJSON;
                      if(obj.errors) {
                          var errors = obj.errors;
                      } else {
                          var errors = obj;
                      }
                      var errorsHtml = '<ul>';
                      $.each(errors, function (index, value) {
                          errorsHtml += '<li>' + value + '</li>';
                      });
                      errorsHtml += '</ul>';
  
                      swal({
                        title: '',
                        text: errorsHtml,
                        type: "error",
                        html: true
                      });
  
                   },
              });
  
          },
  
  to validate the inputs, I always make a request class and rules function, but because I use the same Angular function to save, I make switch case to check the current request HTTP method, when it = POST this mean I insert record, when it = PATCH, this mean I update the record.
  but in this case there is a strange mistake, this a rules function in Request class:
  public function rules()
      {
          $rules = [];
  
          switch($this->method())
          {           
              case 'GET':
              case 'DELETE':
                  {
                      print_r("1 \n");
                      $rules = [];
                  }
              case 'POST':
                  {
                      print_r("2 \n");
                      $rules = [
                          'email' => 'required|email|unique:technicians',
                          'mobile' => 'required|unique:technicians',
                          'name' => 'required',
                          'password' => 'required',
                          'id_number' => 'required|unique:technicians',
                          'image'       => 'required|max:2048|mimes:jpeg,bmp,png,jpg,gif',
                          'lat' => 'required',
                          'long' => 'required',
                      ];
  
                      if ($this->attributes->has('country_id') && $this->attributes['country_id']==9) {
                           $rules['id_number'] = 'required|unique:technicians|number|max:10|min:10';
                      }
                  }
              case 'PUT':
              case 'PATCH':
                  {
                      print_r("3 \n");
                      $rules = [
                          'email' => 'required|email',
                          'mobile' => 'required',
                          'name' => 'required',
                          //'password' => 'required',
                          'id_number' => 'required',
                          'lat' => 'required',
                          'long' => 'required',
                      ];
                  }
              default:break;
          }
  
          print_r($this->method()." \n");
          print_r($rules);
          return $rules;
  
      }
  
  it is supposed to print 2 and go to execute the POST case, but it is the output of the code:
  2 
  3 
  POST 
  Array
  (
      [email] => required|email
      [mobile] => required
      [name] => required
      [id_number] => required
      [lat] => required
      [long] => required
  )
  
  it print 2, 3 really I don't know how? and of course it print the method POST and it is true, but it set $rules array with values when method = PATCH
  What is the wrong here?
  it is the request url:
  
  http://localhost/utf/fixbees/public/admin/technicians
  
  and the request header:
  Request URL:http://localhost/utf/fixbees/public/admin/technicians
  Request Method:POST
  Status Code:422 Unprocessable Entity
  Response Headers
  view source
  Cache-Control:no-cache
  Connection:Keep-Alive
  Content-Length:367
  Content-Type:application/json
  Date:Mon, 03 Apr 2017 12:09:21 GMT
  Keep-Alive:timeout=5, max=100
  phpdebugbar-id:a6b7cf5aa8f5117ed187b9fae77bedb2
  Server:Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
  Set-Cookie:laravel_session=eyJpdiI6ImFpdnJWM3Z2WmQyM3BlREJpVitXOUE9PSIsInZhbHVlIjoicXJJSmcxVWZLeDhiT2Q4b1g1UStOWUpGbTQyQno1cHQwTmlsb05iWG9GUDlzU3FMcHZrVmg5bXpBaUQ3Zmkra3V1cENlXC9HZXhzSlloWjlGc2lraXJnPT0iLCJtYWMiOiI3ODlkNzE0MGFjZjEzZmI2ZjNmYjg4ZWJlOTk4ZWNiNzIzZjdmY2MzN2UzZGNkZWM0NWJjYjVkNDU5NjQzNzI0In0%3D; path=/; httponly
  Set-Cookie:XSRF-TOKEN=eyJpdiI6Ik55SUFyY0huTGJBXC82T2o1dHh5NXNBPT0iLCJ2YWx1ZSI6InpLdkJXTVdoenFrbWVXMGtcL1plQ1czWGhmNnBIcDhGdDRKam0wMU53MXpvMmt0Z1A4QThlRkZXU2dUXC9Uc01CMGIxVDRuRjdzQWF6TDQ3RHVLeGh2aUE9PSIsIm1hYyI6IjAwMWVkMmM4ZjgzYWZjYTYzODM4ZjQzOGZiZWZjN2NhODhhNzBmMTcwYWI1NjhhNzU3OTIyOWIxZmE4MWRlMzYifQ%3D%3D; expires=Mon, 03-Apr-2017 14:09:21 GMT; Max-Age=7200; path=/
  X-Content-Type-Options:nosniff
  X-Powered-By:PHP/5.6.15
  X-UA-Compatible:IE=edge
  Request Headers
  view source
  Accept:*/*
  Accept-Encoding:gzip, deflate, br
  Accept-Language:en-US,en;q=0.8
  Connection:keep-alive
  Content-Length:1357
  Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryzbqA326BcIu3Ciep
  Cookie:intend=eyJpdiI6ImZRM2NSUmE5eXV3b0d2TUVzcW1LcWc9PSIsInZhbHVlIjoiR0VjTmk2MUt2S29qTWV3c1dMSUZrdDJiOTgrZmdMRDdGaVwvSXpkYzNLbGdKV0hnSndmN2c2dzA5WUZsYjN4b1FoSkp0U2JYbUN6UGFLb1wveGdUS04xQT09IiwibWFjIjoiNDhkNDAxOTI4ZDY5MWEyMTE4OWE1MzIzOWNlMTIwNGQxYzU0NzQyNmJmYjUxODViNmQ5MDBmODVhZWJhZTQyMSJ9; XSRF-TOKEN=eyJpdiI6IkY0QWRUc041QkhQaHpQRk5HWVwvb0pBPT0iLCJ2YWx1ZSI6InV6UWZlczdBN0ZNazR3TUlvUkQ1MHFVc0M0d1ZaZ0pyTWI2V2txNzU2blZCaGowR1wvS25mUTJrNllIc1M5ajhsN0x1OGpHM1Z1UkpnQjVtcUZuYlIrQT09IiwibWFjIjoiMDkzNWRlZjAzYTllMmI1YzYzM2EzMGU4MDg5OTYxMTYzYjRhYjhhNzE4N2E0MjJhOWZmZGFjYjZhODczYmI4NyJ9; laravel_session=eyJpdiI6Ijk0azNCaDRFSHFaRTF5K2VBYlhtMkE9PSIsInZhbHVlIjoiUExENHh0eVF0MTdoeHJNNVFIVUlGXC9RcjBTekUyZkRWeVF0MDN5S2F0eTI3QnJrODVvYzk3Sngrd1hEVU52RUNEV0dDWlpNWTlnblFldXJcLzZoM0hhUT09IiwibWFjIjoiODkzMzMzYzJkZjUwYmQ1MTJkOWM1YTEzM2ZjMzIxZmUzM2UxMGVmYzg2MDVmODA2YmQwY2Y3NDc1M2VhN2M5NyJ9
  DNT:1
  Host:localhost
  Origin:http://localhost
  Referer:http://localhost/utf/fixbees/public/admin/technicians
  User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
  X-CSRF-TOKEN:ovfFSeLUbkrQvSX1DYTN9HLuoNWhEEL2X2kJ69bo
  X-Requested-With:XMLHttpRequest
  Request Payload
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="_token"
  
  ovfFSeLUbkrQvSX1DYTN9HLuoNWhEEL2X2kJ69bo
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="name"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="email"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="password"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="mobile"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="id_number"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="country_id"
  
  9
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="city_id"
  
  12
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="image"; filename=""
  Content-Type: application/octet-stream
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="lat"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="long"
  
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="active"
  
  on
  ------WebKitFormBoundaryzbqA326BcIu3Ciep
  Content-Disposition: form-data; name="_method"
  
  POST
  ------WebKitFormBoundaryzbqA326BcIu3Ciep--
  
  A:
  
  you are missing the break statement , so your switch case will do -in pseudo-code- :-  
  check your first case GET ? not get , go to next case
  DELETE ? not delete , go to the next case
  POST ? yes, continue executing until your next break statement.
  consider the following example:
  $c = 'c';
  
  switch ($c) {
      case 'a': echo 'a' . "\n";
      case 'b': echo 'b' . "\n";
      case 'c': echo 'c' . "\n";
      case 'd': echo 'd' . "\n";
      case 'e': echo 'e' . "\n";
  }
  
  the output will be as follows:
  c
  d
  e
  
  while this :
  $c = 'c';
  
  switch ($c) {
      case 'a': echo 'a' . "\n"; break;
      case 'b': echo 'b' . "\n"; break;
      case 'c': echo 'c' . "\n"; break;
      case 'd': echo 'd' . "\n"; break;
      case 'e': echo 'e' . "\n"; break;
  }
  
  will only print c ,
  so to solve your issue you need to break your switch cases :
  switch($this->method())
  {
      case 'GET':
      case 'DELETE':
          {
              print_r("1 \n");
              $rules = [];
          }
          break;
      case 'POST':
          {
              print_r("2 \n");
              $rules = [
                  'email' => 'required|email|unique:technicians',
                  'mobile' => 'required|unique:technicians',
                  'name' => 'required',
                  'password' => 'required',
                  'id_number' => 'required|unique:technicians',
                  'image'       => 'required|max:2048|mimes:jpeg,bmp,png,jpg,gif',
                  'lat' => 'required',
                  'long' => 'required',
              ];
  
              if ($this->attributes->has('country_id') && $this->attributes['country_id']==9) {
                  $rules['id_number'] = 'required|unique:technicians|number|max:10|min:10';
              }
          }
          break;
      case 'PUT': break;
      case 'PATCH':
          {
              print_r("3 \n");
              $rules = [
                  'email' => 'required|email',
                  'mobile' => 'required',
                  'name' => 'required',
                  //'password' => 'required',
                  'id_number' => 'required',
                  'lat' => 'required',
                  'long' => 'required',
              ];
          }
          break;
      default:
          break;
  }
  
  