1

Trying to validate arrays in laravel via FormRequest validation

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class VendorStoreRoom 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 [
            'roomtype_id' => 'required',
            'price' => 'required|digits_between:1,8',
            'amenities' => 'required',

            //'max_children' => 'required',
            'max_adult' => 'required',
            //'capacity' => 'required',
            'floor.*' => 'required',
            'room_count' => 'min:1|max:50',
            'room_number.*' => 'required',
            //'room_number' => 'required|max:5',
            'image' => 'required',
        ];
    }


    public function messages()
    {
        return [
            'roomtype_id.required'      => 'Please select a room type',

            'price.required'            => 'Price cannot be empty',
            'price.digits_between'      => 'Price cannot exceed 8 digits',

            'amenities.required'        => 'Atleast select one amenities',

            //'max_children.required'     => 'Vendor name cannot be empty',

            'max_adult.required'     => 'Maximum adult can not be empty',

            //'capacity.required'     => 'Vendor name cannot be empty',

            'floor.*.required'     => 'Please select a floor',

            'room_count.min'     => 'Room count cannot be empty',
            'room_count.max'     => 'Room count cannot exceed 50',

            'room_number.*.required'     => 'Room number cannot be empty',
            //'room_number.max'     => 'Room number cannot exceed 5 characters',

            'image.required'     => 'Atleast select one image',
            /*'image.*.mimes'     => 'Image Must be JPEG, JPG or PNG',
            'image.*.min'     => 'Image size must be more than 10 kb',
            'image.*.max'     => 'Image size cannot exceed 300 mb', */


        ];
    }
}

This is the response i have got:

{"message":"The given data was invalid.","errors":{"roomtype_id":["Please select a room type"],"price":["Price cannot be empty"],"amenities":["Atleast select one amenities"],"image":["Atleast select one image"],"floor.0":["Please select a floor"],"room_number.0":["Room number cannot be empty"]}}

I am catching these errors in jquery and do like below:

This works fine: $("#amenities_error").text(data.responseJSON.errors.amenities);

But not this: $("#floor_error").text(data.responseJSON.errors.floor.0); How can i achieve this?

is there anything i can do like data.responseJSON.errors."floor.0" or data.responseJSON.errors.{floor.0}

1 Answer 1

1

floor.0 is the string that is key for this error. However, javascript sees a . as a property separator. Because of this, javascript searches for an object floor with a property 0.
To fix this, you will have to use the array syntax for this key.

$("#floor_error").text(data.responseJSON.errors["floor.0"]);
Sign up to request clarification or add additional context in comments.

2 Comments

this is what i have asked for, is there any way to loop through? consider "floor.0":["Please select a floor"], "floor.1":["Please select a floor"], "floor.2":["Please select a floor"]
Sure, just use "floor." as string an concatenate the number of the floor to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.