0

In my Laravel app, I am creating a dynamic search functionality using Eloquent and I have a customer search form which looks like this:

customers.search.blade.php Contrived Example

<form method="post" action="{{ route('customers.search') }}">
    @csrf
    <div class="form-group">
        <label for="first_name" class="font-weight-bold">First Name</label>
        <div class="input-group">
            <div class="input-group-prepend">
                <select name="first_name['operator']" class="custom-select">
                    <option value="%">%</option>
                    <option value="=">=</option>
                    <option value="!=">!=</option>
                </select>
            </div>
            <input id="first_name" name="first_name['query']" type="text" class="form-control">
        </div>
    </div>
</form>

I have a SearchCustomerRequest (FormRequest) class which builds out the validation rules dynamically. I've dumped it, so you can see what the generated rules looks like:

enter image description here

In my CustomersController under search method, I did the following to see what the validated request array looks like:

class CustomersController extends Controller
{

    // ...

    public function search(SearchCustomerRequest $searchCustomerRequest)
    {
        dd($searchCustomerRequest->validated());
    }

    // ...

}

In order to test this, in the search form, I've selected the firstname['operator'] to % and typed the first_name['query'] to test and submitted the form.

I got the following response (i.e. empty array):

[]

So, I dumped the whole request object $searchCustomerRequest to see what was in the parameter bag and this is what I see:

enter image description here

As you can see, my request is valid and my rules also looks correct, yet the validation doesn't seem to be working as expected.

Any ideas what might be wrong here?

1 Answer 1

3

In your ParameterBag, the first_name properties operator and query are enclosed in single quotes. In your HTML, the name attribute should exclude the single quotes. Ex: name="first_name[query]"

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

1 Comment

Thanks @pablo. Nice catch.

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.