Search code examples
phplaravellaravel-validation

Get input values in laravel formRequests


This is my FormRequest class

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use URL;
class CaseStudyUpdateRequest extends FormRequest
{
    /**
     * 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()
    {
      $id = $this->request->get('cspid');
      dump($this);
      dump($id);
      $prev_url = URL::previous();
      $url_arr = explode('/', $prev_url);
      $data = $this->request->all();
      dump($data);
        $rules = [
            // 'post_title' => 'required|min:15|max:255|unique:case_study_posts,title,'.$id,
            'summary' => "required|array|min:1",
            'total_blocks' => "not_in:0",
            'category' => 'required',
            'specialities' => 'required',
        ];
        if($url_arr[4] === "edit-for-request") {
          // $rules['edit_comment'] = 'required';
                if(!empty($data)) {
                  $rules['edit_comment'] = 'required';
                    $totalBlocks = $data['total_blocks'];
                    foreach(range(1, $totalBlocks) as $i){
                        if(array_key_exists("description-$i", $data)) {
                          // $rules['edit_comment'] = 'required';
                            $rules["description-$i"] = 'required|min:15';
                        } elseif(array_key_exists("image-$i", $data) || array_key_exists("image_title-$i", $data) || array_key_exists("image_description-$i", $data)) {
                          // $rules['edit_comment'] = 'required';
                            $rules["image-$i"] = (array_key_exists("old-img-$i", $data) && !array_key_exists("image-$i", $data)?"":"required|mimes:jpeg,bmp,jpg,png|max:2048");
                            $rules["image_title-$i"] = "required_with:image-$i|required|min:5|max:30";
                            $rules["image_description-$i"] = "required_with:image_title-$i|required|min:15|max:120";
                        }
                    }
                }
        }

        return $rules;
    }

    public function messages() {
        return [
           'total_blocks'=> 'Please select text/images block to case studys',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes() {
        return [
            'post_title' => 'Case Study Title',
        ];
    }


}
?>

View page

In my view page I've this,

@section('scripts')
@parent
   {{ Html::script('vendor/jsvalidation/js/jsvalidation.js') }}
   {!! JsValidator::formRequest('App\Http\Requests\CaseStudyUpdateRequest', '#edit_post_form'); !!}  
   ....  
   ....   

Controller

In Controller, I've added

use App\Http\Requests\CaseStudyUpdateRequest;   

and in the called function

public function editCaseStudy(CaseStudyUpdateRequest $request) {

Here, i have image blocks and description blocks , say, image1, image2,image3,description1, description2,..etc.. I want to validate those fields, so i need to get those input values in formRequest file. But it's showing an empty array when i print dump($this->request->all());

How do i get input value in side formRequest? Thanks in advance


Solution

  • The Illuminate\Foundation\Http\FormRequest class extends the Illuminate\Http\Request class. So all() or any of the usual methods will give you access to the request parameters from GET or POST.

    e.g. instead of this:

    $id = $this->request->get('cspid');
    

    Use one of these:

    $id = $this->cspid;
    $id = $this->get("cspid");