1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Report back validation errors an array of inputs with FormRequest in Laravel

Discussão em 'Outras Linguagens' iniciado por wruckie, Novembro 7, 2024 às 13:32.

  1. wruckie

    wruckie Guest

    I have a page where I want a teacher to list the grade for each student on a project as one large form. I have the form working via a large array as shown. It is created with a foreach loop with a input component for each row. I have to name them all as grade[id] to get them as key value pairs in an array.

    <x-input type="text" name="grade[{{$scr->id}}]"
    id="{{$scr->id}}" label="Grade"
    :value="@old('grade[]', $scr->grade)" /></x-input>


    [​IMG]

    A dd($request); on the incoming data looks like this. I am able to save the values when data comes in correctly.

    [​IMG]

    I have a validation rule via a FormRequest that seems to work.

    public function rules(): array
    {
    return [
    'grade.*' => ['numeric'],
    ];
    }


    I know that an error was returned because I added this to my view and can verify that it is tripping over error.

    @error('*')
    {{ dd($errors);}}
    @enderror


    [​IMG]

    But I cannot seem to find a way to report back the which line(s) contain the error. This line in the input component works for other things on this same page, but not for inputs in this grade array.

    @error($name)
    <div class="text-red-600 text-sm">{{ $message }}</div>
    @enderror


    But the name of the field has changed. I submitted it as grade[x] and it comes back as grade.x



    UPDATE
    I was able to get it working by adapting to the name change. Do I make a custom component for this use only? Or is there something else I am missing? How do I create a generic message and skip the grade.x portion of error message.

    @error('grade.'.$id)
    <div class="text-red-600 text-sm">{{ $message }}</div>
    @enderror


    [​IMG]

    Continue reading...

Compartilhe esta Página