samedi 29 juin 2019

Laravel Form Request - Validation issues

I'm attempting to create a single error response message for array input:

This is my form:

    <div class="form-group  col-md-12">
        <div class="row">
            <div class="col-md-6">
                <label for="client-type">Client Type</label>
                <input type="text" name="client_type[]" class="form-control" value="Panelist" readonly>
            </div>
            <div class="col-md-6">
                <label for="commission-percentage">Commission Percentage</label>
                <input type="number" name="commission_percentage[]" class="form-control">
            </div>
        </div><br>
        <div class="row">
            <div class="col-md-6">
                <label for="client-type">Client Type</label>
                <input type="text" name="client_type[]" class="form-control" value="Non Panelist" readonly>
            </div>
            <div class="col-md-6">
                <label for="commission-percentage">Commission Percentage</label>
                <input type="number" name="commission_percentage[]" class="form-control">
            </div>
        </div>

Here's my controller:

public function store(StoreCommissionsList $request)
{
    $attributes = $request->validated();

    dd($attributes);
}

And finally my StoreCommissionsList form request:

public function rules()
{
    $commission = request('commission_percentage');

    $rules = [];

    $rules['role_id'] = 'required';
    $rules['client_type'] = 'required';

    if ( $commission[0] == null && $commission[1] == null ) 
    {
        $rules['commission_percentage'] = 'required';
    }

    return $rules;
}

public function messages()
{
    return [
        'role_id.required' => 'Please select a user role',
        'client_type.required' => 'Please input a client type',
        'commission_percentage.required' => 'Please fill in a percentage commission for each client type',
    ];
}

What is happening is: If all the fields in my form are blank, only the role_id seems to be passing the validation check in terms of returning the required validation message. The commission_percentage is not validated at all and after inputting role_id, the form submits meaning the commission_percentage validation is overlooked for some reason.

Kindly assist.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2XdX1pc
via IFTTT

Aucun commentaire:

Enregistrer un commentaire