TransWikia.com

Custom validation rule with request class not working laravel 7

Stack Overflow Asked by Vishal Vaishnav on November 14, 2020

Below is my code;

FruitRequest.php

class FruitRequest extends Request
{

public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'name' => 'required|alpha',
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ];
}

public function messages()
{
    return ['name.required' => response("Name should be mandatory", 404),
        'name.alpha' => response("Name should be contains only letters", 404),
        'image.required' => response("Foto should be mandatory", 404),
        'image.mimes' => response('Foto should be jpeg,png,jpg,gif,svg', 404),
        'image.max' => response('Foto size should be blow 2 MB', 404),
    ];
}

}

FruitController.php

use AppHttpControllersController;
use AppHttpRequestsFruitRequest;

class FruitController extends Controller
{

public function store(FruitRequest $request)
{
    echo $request->input('name');

    //above line gives nothing to me
}

}

If I use extends Request instead of extends FruitRequest then this gives me value which is passed by user in postman. I don’t know why this custom Request class not working.I attached screenshot. Please help….

enter image description here

2 Answers

extend your request class with FormRequest

use IlluminateFoundationHttpFormRequest;

class FruitRequest extends FormRequest

for more details visit official doc of laravel: https://laravel.com/docs/7.x/validation#creating-form-requests

Answered by SJR on November 14, 2020

long time not using postman, i'm testing with my code

I'm using FormRequest like this:

use IlluminateFoundationHttpFormRequest;
use IlluminateContractsValidationValidator;
use IlluminateHttpExceptionsHttpResponseException;

class YourRequest extends FormRequest
{
  //this function called if Validator::make()->fails();
  //here where you can modifying your message
  protected function failedValidation(Validator $validator)
  {
    //note this only for API, for formData use IlluminateValidationValidationException($validator)
    throw new HttpResponseException(response()->json($validator->errors()->all(), 422));
    //this will get parameter attribute set from FormRequest
    //attributes() along with the error message, 
    //or $validator->errors()->all() to get messages only like my screenshot
    //or modify message with your logic
  }

  public function authorize() { return true; }
  public function rules() { return []; }
  public function attributes() { return []; }
  public function messages() { return []; }
}

in controller :

use YourRequest;

public function store(YourRequest $req)
{
  return response($req->all())->setStatusCode(200); 
}

in your FormRequest replace response(), just text:

public function messages()
{
    return ['name.required' => "Name should be mandatory"],
}

2nd, validation alpha only accepts alphabet, which your name is numeric, result from my code(i use default validator message which in array of messages) : postman

Answered by Alzafan Christian on November 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP