title: Laravel validation rules for JSON
body: <p>I'm trying to use a custom FormRequest with validation rules for JSON formatted data. If I use same code in controller instead of the FormRequest class then it works fine, see below:</p>
  
  <p>array data (from ajax request):</p>
  
  <pre><code>[
    {
      "name": "id",
      "value": "1"
    },
    {
      "name": "title",
      "value": "My fun project"
    }  
  ]
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public function update(Request $request, $id) {
      //convert it to readable json
      $jsonReq = $request-&gt;json()-&gt;all();
      $jsonData = array();
      foreach ($jsonReq as $json) {
          $jsonData[$json["name"]] = $json["value"];
      }
  
      $rules = [
          'id' =&gt; 'required|numeric:1',
          'title' =&gt; 'required|max:255',
      ];
  
      $validation = Validator::make($jsonData, $rules);
  
      if ($validation-&gt;fails()) {
        return $validation-&gt;errors();
      }
  }
  </code></pre>
  
  <p>Above works fine when used in the controller. However, I want to separate my validation in a separate class, extending the FormRequest. This generates some errors, most likely due to the array format.</p>
  
  <pre><code>class UpdateProjectValidationRequest extends FormRequest {
  
        public function rules() {
  
          $jsonReq = $this-&gt;json()-&gt;all();
          $jsonData = array();
          foreach ($jsonReq as $json) {
              $jsonData[$json["name"]] = $json["value"];
          }
  
          return [
              'id' =&gt; 'required|max:1', //does not work
              $jsonData['title'] =&gt; 'required|max:255', //does not work
          ];
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>  public function update(UpdateProjectValidationRequest $request, $id) {
  
      // validate against rules
      $request-&gt;rules();
  </code></pre>
  
  <p>The error message:</p>
  
  <pre><code>{
    "message": "The given data was invalid.",
    "errors": {
      "My fun project": [
        "My fun project field is required."
      ],
      "id": [
        "The id field is required."
      ],
  </code></pre>
  
  <p>Clearly this has to do with the format. Any ideas how to solve this? Note that after the foreach loop the data is formatted to below:</p>
  
  <pre><code>{
    "id": "1",
    "title": "My Fun project",
  }
  </code></pre>