Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jaev's avatar
Level 1

Laravel custom validation not working as expected

So I've been having a problem when trying my custom validation. If I submit the form with empty fields, the error message is showing but only in commodity dropdown.

        <div class="col-md-3 mb-2">
            <label for="commodity" class="form-label label-style">Commodity</label>
            <select id="commodity" name="commodity[]" class="form-select">
                <option value="" disabled selected>Select Commodity</option>
                @foreach ($commodityValues as $id => $value)
                    <option value="{{ $id }}">{{ $value }}</option>
                @endforeach
            </select>
            @error('commodity')
                <span class="fs-6 text-danger">{{ $message }}</span>
            @enderror
        </div>

        <div class="col-md-2 mb-2">
            <label for="variety" class="form-label label-style">Variety</label>
            <input type="text" class="form-control" name="variety[]" id="variety" autocomplete="off">
            @error('variety')
                <span class="fs-6 text-danger">{{ $message }}</span>
            @enderror
        </div>

        <div class="col-md-2 mb-2">
            <label for="volume" class="form-label label-style">Volume (Kg)</label>
            <input type="text" class="form-control" name="volume[]" id="volume" autocomplete="off">
            @error('volume')
                <span class="fs-6 text-danger">{{ $message }}</span>
            @enderror
        </div>

        <div class="row">
            <div class="col-md-3 mb-2">
                <label for="startDate" class="form-label label-style">Estimated
                    Time of Harvest <br> (Start Date)</label>
                <input type="date" class="form-control" name="startDate[]" id="startDate">
                @error('startDate')
                    <span class="fs-6 text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="col-md-3 mb-2">
                <label for="endDate" class="form-label label-style">Estimated
                    Time of Harvest <br> (End Date)</label>
                <input type="Date" class="form-control" name="endDate[]" id="endDate">
                @error('endDate')
                    <span class="fs-6 text-danger">{{ $message }}</span>
                @enderror
            </div>
        </div>
        <div x-show="index > 0">
            <div class="col-md-2 mb-2 d-flex mt-auto">
                <button x-on:click="removeRow(index)" type="button" class="btn btn-danger">Remove</button>
            </div>
        </div>
        <hr>
    </div>
</template>

<div class="col-md-1 mb-2 d-flex mt-auto">
    <button x-on:click="addRow()" type="button" class="btn btn-success">Add more</button>
</div>

This is the form request code:

public function rules(): array { return [ 'commodity' => 'required', 'variety' => 'required', 'volume' => 'required', 'startDate' => 'required', 'endDate' => 'required', ]; }

public function messages()
{
    return [
        'commodity' => 'Please select a commodity.',
        'variety' => 'Please enter the variety.',
        'volume' => 'Please enter the volume.',
        'startDate' => 'Please select the start date.',
        'endDate' => 'Please select the end date.',
    ];
}

This is the controller.

public function store(StoreELinkageRequest $request) { $user = $request->user();

    $request->validated();

    foreach ($request->input('associationName') as $key => $associationName) {
        $data = [
            'association' => $associationName,
            'commodity' => $request->input('commodity')[$key],
            'variety' => $request->input('variety')[$key],
            'volume' => $request->input('volume')[$key],
            'startDate' => $request->input('startDate')[$key],
            'endDate' => $request->input('endDate')[$key],
            'userId' => $user->id,
        ];
        ELinkage::create($data);
    }
    return redirect()->route('e-linkage')->with('success', 'Data saved successfully!');
}
0 likes
4 replies
Ben Taylor's avatar

Can you check the response in the browser developer tools network tab and see if the validation error messages you expect to see are there?

Jaev's avatar
Level 1

@Ben Taylor This is what I get when I submit the form for validation

_token: 7cT6vMnKVkGCDwAIfCpgmRpteLLVtzuCFswi3t9d associationName[]: National Basketball Association variety[]: volume[]: startDate[]: endDate[]:

Jaev's avatar
Level 1

@Ben Taylor I can't see the error messages

Please or to participate in this conversation.