Skip to content
Advertisement

My custom message Request validator not work Laravel

I use the Request validation but I don’t get message from request. Look my code and see my mistake. Only important is store function which work good if all fields is fullfiled but if any field not filled i don’t get my custom message from request. For not filled field I don’t give error just laravel home page.

This is my request file

<?php

namespace AppHttpRequests;

use IlluminateFoundationHttpFormRequest;

class CarRequest 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()
    {
        return [
            'car_type' => 'required',
            'mark' => 'required',
            'model' => 'required',
            'fuel' => 'required',
            'circuit' => 'required',
            'chassis' => 'required|numeric',
            'bill_type' => 'required',
            'first_registration' => 'required|date',
            'km' => 'required|numeric',
            'structure' => 'required',
            'motor_vehicle_check' => 'required|boolean',
            'warranty' => 'required|boolean',
            'year' => 'required|date',
            'import_vehicle' => 'required|boolean',
            'know_damage' => 'required',
            'car_accessories' => 'required',
            'email' => 'required|email'
        ];
    }

    public function messages()
    {
        return [
            'car_type.required' => 'A car_type is required',
            'mark.required'  => 'A mark is required',
            'model.required' => 'A model is required',
            'fuel.required' => 'A fuel is required',
            'circuit.required' => 'A circuit is required',
            'chassis.required' => 'A chassis is required',
            'bill_type.required' => 'A bill_type is required',
            'first_registration.required' => 'A first_registration is required',
            'km.required' => 'A km is required',
            'structure.required' => 'A structure is required',
            'motor_vehicle_check.required' => 'A motor_vehicle_check is required',
            'warranty.required' => 'A warranty is required',
            'year.required' => 'A year is required',
            'import_vehicle.required' => 'A import_vehicle is required',
            'know_damage.required' => 'A know_damage is required',
            'car_accessories.required' => 'A car_accessories is required',
            'email.required' => 'A email is required'
        ];
    }
}

And this is my controller code

<?php

namespace AppHttpControllers;

use AppCar;
use AppCarImages;
use AppHttpRequestsCarRequest;
use IlluminateHttpRequest;
use CarbonCarbon;
use IlluminateSupportFacadesConfig;



class CarController extends Controller
{


    public function index()
    {
        $cars = Car::with(['images'])
            ->orderByDesc('car.created')
            ->get();

        return response()->json($cars, 200);
    }


    public function search($name){
        $cars = Car::where('mark', '=' , $name)->get();
        return $this->response($cars);
    }


    public function create()
    {
        //
    }


    public function show($id)
    {
        $car = School::with(['images'])->find($id);
        if (!$car) {
            return response()->json(['message' => 'No Car found'], 404);
        }
        return response()->json($car, 200);
    }


    public function store(CarRequest $request)
    {
            $car = Car::create([
                'car_type' => $request->input('car_type'),
                'mark' => $request->input('mark'),
                'model' => $request->input('model'),
                'fuel' => $request->input('fuel'),
                'circuit' => $request->input('circuit'),
                'chassis' => $request->input('chassis'),
                'bill_type' => $request->input('bill_type'),
                'first_registration' => $request->input('first_registration'),
                'km' => $request->input('km'),
                'structure' => $request->input('structure'),
                'motor_vehicle_check' => $request->input('motor_vehicle_check'),
                'warranty' => $request->input('warranty'),
                'year' => $request->input('year'),
                'import_vehicle' => $request->input('import_vehicle'),
                'know_damage' => $request->input('know_damage'),
                'car_accessories' => $request->input('car_accessories'),
                'email' => $request->input('email')
            ]);

            return response()->json([
                'message' => 'Your car has been successfully added',
                'car' => $car
            ],201);
        }


    public function destroy($id)
    {
        $car = Car::destroy($id);
        return response()->json($id);
    }
}

Advertisement

Answer

I use the Request validation but I don’t get message from request.

When expecting a json response, don’t forget to add this header when making your requests:

   Accept: Application/json // <--

If I want set in my custom message example km must be numberic , how do that in messages function?

You need to specify your message for every rule like this. Let’s do it for the km validation:

MyCustomRequest.php

public function rules()
{
    return [
     // ...
     // 'first_registration' => 'required|date',
        'km' => 'required|numeric',  // <---
     // 'structure' => 'required',
     // ...
    ];
}

Given that km has two validations, just add it one element more in the messages() function:

MyCustomRequest.php

public function messages()
{
    return [
        // ...
        'km.required' => 'A km is required',
        'km.numeric' => 'The km needs to be numeric, dude!', // <---
        // ...
    ];
}

Regarding this last subject, check the documentation.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement