Get input values in laravel formRequests
Asked Answered
D

2

5

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

Dramamine answered 8/11, 2019 at 14:59 Comment(0)
G
14

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");
Gynandry answered 8/11, 2019 at 16:24 Comment(4)
Ah.. may be. I couldn't figure what's wrong, Since it's not working i've done ajax validationDramamine
You can’t rely on client side validation. Are you type hinting the CaseStudyUpdateRequest object in your controller method?Gynandry
Yes, I have added that in controllerDramamine
I've updated my question, Please check. I'm using formRequest in all of my forms, but couldn't get id in any of my formRequest classDramamine
I
0

In the formrequest class, rules() function:

$this->route()->parameter('myrouteparam')
Incomprehension answered 2/12, 2020 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.