orest's avatar
Level 13

errors are available in view but not in test assertion

I'm using a custom validation and if the validation fails i return a view with the errors

validator = Validator::make(
            $request->input(),
            $this->rules($request),
            $this->messages()
        );

  if ($validator->fails()) {
            return view('some-view')
                ->withErrors($validator);
        }

The issue that i have is that even though in the view i do have access to the errors

@if($errors->any())
    @foreach($errors->all() as $error)
        {{ $error }}
    @endforeach
@endif

When i try to assert that the session has errors, it fails.

$this->get('path')->assertSessionHasErrors('error-name');

but if i assert that the error message is being displayed in the view, it works. And indeed the errors are being displayed when i use the browser.

$this->get('path')->assertSee('error message');

Any thoughts on what i might be doing wrong ?

0 likes
7 replies
tykus's avatar

You make a GET and expect to see validation errors?

orest's avatar
Level 13

ok that's what i'm doing wrong :)

Is it considered wrong to validate the request parameters when you make a GET request ?

In my case, i make a search request using GET and before i start searching i want to validate the search parameters.

tykus's avatar

Not necessarily wrong but different enough to warrant the earlier question.

So you make a GET request to your search endpoint with invalid parameter(s), and you want to follow a redirect to some-view where the validation errors are displayed (and therefore in the Session)?

orest's avatar
Level 13

Yes that is what i am doing.

And the errors are displayed correctly in the view, i just couldn't assert that the session has errors in my test.

More specifically, in my search endpoint what i do is

  1. Validate the search parameters and if validation fails, then display the errors
  2. If validation passes, then search and display the results
 public function show(Search $search, SearchRequest $searchRequest)
    {
        $validator = $searchRequest
            ->validate($this->request)
            ->getValidator();

        if ($validator->fails()) {
            return view('search.show')
                ->withErrors($validator);
        }

        $results = $search->handle($this->request);
        $query = $this->request->input('q');

        return view('search.show', compact('results', 'query'));
    }

NOTE

The SearchRequest does not extend the FormRequest, it just uses the Validator facade to apply the validation rules that i have defined.

tykus's avatar

Ok. AFAIK, the withErrors method on the View is data passed to the view, it is not in the Session as would be the case for a Redirect Response (as you would expect from a real form request).

You could change the test to assertViewHas('errors') and further assert against the errors in the View data.

orest's avatar
Level 13

Ok, thanks!

By the way, if could suggest a different approach in case you think that my approach is "wrong" or unusual, please let me know.

tykus's avatar

As I mentioned before, it is not wrong just "different" enough to confirm I understood what you were attempting.

I have used (not created) APIs that validate query params e.g. Zoho, so it is not never done!

Please or to participate in this conversation.