1

I am developing an app using Laravel and VueJs. I have used the below form validation rules:

public function rules()
{
  $rules = [
    'id.*'=>'required|integer',
    'job_id.*'=>'required|integer',
    'satisfied.*'=>'nullable',
    'client_comments.*'=>'nullable',
    'improvements.*'=>'nullable',
    'rating.*'=>'integer|max:10|min:0'
  ]; 
  return $rules;
}

Further, I am sending the below axios request to the Laravel controller. However, the validation is not working. Could somebody can help?

The request comes to the Laravel controller as below:

array:2 [
  0 => array:7 [
    "client_comments" => null
    "id" => 34
    "improvements" => null
    "job_id" => 1
    "quality_cycle_id" => 14
    "rating" => "10"
    "satisfied" => null
  ]
  1 => array:7 [
    "client_comments" => null
    "id" => 35
    "improvements" => null
    "job_id" => 3
    "quality_cycle_id" => 14
    "rating" => "9"
    "satisfied" => null
  ]
]

I just used so many efforts to resolve this problem, but none of these worked. In the same application, I have put so many validations for single request. But this request is coming as an arrays inside array as above.

Appreciate, if somebody help.

Thanks

3 Answers 3

1

You can try to validate like this:

 $request->validate($request, [
          '*.id'=>'required|integer',
          '*.job_id'=>'required|integer',
          '*.satisfied'=>'nullable',
          '*.client_comments'=>'nullable',
          '*.improvements'=>'nullable',
          '*.rating'=>'integer|max:10|min:0']);

It works fine for me

Sign up to request clarification or add additional context in comments.

Comments

0

Your field is inside of array not an array. It work for array of inputs id[], job_id[], etc..

Try this,

public function rules()
{

 $rules = [

          '*.id'=>'required|integer',
          '*.job_id'=>'required|integer',
          '*.satisfied'=>'nullable',
          '*.client_comments'=>'nullable',
          '*.improvements'=>'nullable',
          '*.rating'=>'integer|max:10|min:0'

        ]; 

        return $rules;

    }

Comments

0

The request data should be in a specified filed like items, which you dump it out using

dd($request->input('items'));

That prints an array. So to validate that array you could use these rules:

public function rules()
{
  return [
    'items.*.id'=>'required|integer',
    'items.*.job_id'=>'required|integer',
    'items.*.satisfied'=>'nullable',
    'items.*.client_comments'=>'nullable',
    'items.*.improvements'=>'nullable',
    'items.*.rating'=>'integer|max:10|min:0',
  ];
}

Comments

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.