1

I have the following validation in the controller's action:

foreach ($request['qtys'] as $key => $val){
            if (!$this->_validateMinQty($key, $job, $val)){
                $customerTitle = $job->customers()->where('customer_id',$key)->first()->title;
                return redirect()->back()->withErrors(['qtys' => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
            }
        }

This check multiple form's input named qtys in the view:

@foreach($job->customers as $customer)

    <div class="form-group {{$errors->first('qtys has-error')}}">
        {!! Form::label('qtys-'.$customer->id, __('Qty').' '.$customer->title) !!}
        <div class="row">
            <div class="col-md-9">
                {!! Form::text('qtys['.$customer->id.']',$customer->pivot->e_production,['class' =>'form-control qtys', "data-sumequal"=>"qty",'required' => 'required','title' => $customer->pivot->aid,'id' => 'qtys-'.$customer->id]) !!}
                <div class="help-block with-errors"></div>
                 @php ($eleE =  $errors->first('qtys'))
                @include('layouts.form-ele-error')
            </div>
            <div class="col-md-3">
                <a href="/storage/create/{{$customer->pivot->aid}}" class="btn btn-nile"><i class="fox-add"></i>{{__('Add Storage')}}</a>
            </div>
        </div>

    </div>
    @endforeach

The above code works, but with the following limitation:

The error message is rendered under every input named qtys[x] where x is an integer and the first input only Testana has the invalid qty, like the following screen shot: enter image description here

In the controller's action return message, I have tried to use indexed name for the input like the following:

return redirect()->back()->withErrors(['qtys.10' => ....

However, it prevents rendering the error message under any qtys field. Is there any solution?

1 Answer 1

1

The solution that I have found starts from the definition of first method found in the view :

@php ($eleE =  $errors->first('qtys'))

This, in my code, should be changed to:

@php ($eleE =  $errors->first('qtys.'.$customer->id))

Because the multiple fields have gotten keys equals to the customer id. This is a technique I usually use, when I want to send double piece of data in single post or in single form element. Then in the controller, I keep the first try,

return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);

Where $key is an integer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.