1

Couldn't find anything about validating collections on the documentation. In .NET Core I can just create a Model and add some data attributes, automatically bind that list on the front end to the model and all errors will show even using jquery validation.

I want to achieve the same result with Laravel

For example in .NET Core I would have something like this

public class Person {

    [Required]
    public string Name { get; set; }

}

on a Razor Page code I would write, this gives me a variable i can use on my view

[BindProperty]
IList<Person> People { get; set; }

then on my view i would render it like

@foreach(var person in Model.People)
{
    <input type="text" asp-for="person.Name" />
}

this would output on the page like

<input type="text" id="People_0_Name">
<input type="text" id="People_1_Name">
<input type="text" id="People_2_Name">
<input type="text" id="People_3_Name">

this would allow me to check model state on the controller or page and check if there are any errors on the list automatically. Everything is done nicely and easy.

Now on Laravel, I am new but I have learned about form requests and validation rules already.

I have the form request

class CreateContractRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'price' => 'required,
            'target' => 'required',
            // Want to add a collection here to like,  'people' => 'required
        ];
    }
}

class CreatePeopleRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required'   
        ];
    }
}

in html I'm rendering the inputs like this

@for($i = 1; $i < $personTotal; $i++)
    <input type="text" id="person{{$i}}" name="person{{$i}}">
@endfor

which will basically just render

<input type="text" id="person1" name="person1">
<input type="text" id="person2" name="person2">
<input type="text" id="person3" name="person3">

Everything different. I know maybe I should create an array for the Person class and send that variable to the view, but I have no experience using models because I do not need to access a database, just communicate with API.

In sum, I just would like to know how can I make an array or collection whichever easier to work with in laravel, to have validation rules?

Jackal
  • 3,359
  • 4
  • 33
  • 78

1 Answers1

1

If anyone has the same problem as me, I found out this topic

Laravel 5 - validate array as required, but allow an empty array to be passed

However this one didn't work for me. Because I am doing an ajax request and null properties are not sent in the request and the present rule won't work.

However, I do have already a field, a checkbox saying if this collection is required or not.

I made use of the requiredif

in my request

public function rules()
{
    return [
        'morePeople' => 'required',
        'people' => 'required_if:morePeople,true|array'
    ];
}

This will only make the field required if exists in the request with content and the checkbox is checked

Works really nicely. +1 To laravel for these clean rules.

Also I changed my code a bit, It seems i do not need an additional form request for the nested array in the main request. Just add the rules to the array properties as below

public function rules()
{
    return [
        'morePeople' => 'required',
        'people' => 'required_if:morePeople,true|array',
        'people.name' => 'required_if:morePeople,true'
    ];
}
Jackal
  • 3,359
  • 4
  • 33
  • 78