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?