framework icon indicating copy to clipboard operation
framework copied to clipboard

[Proposal] Get the MessageBag message's keys along with the errors

Open bworwa opened this issue 12 years ago • 2 comments

When you deal with MessageBags they'll only return the actual message to be used on you views. E.g.:

$messageBag->first('username');

// The username must be ...

Often and specially while testing it's needed to know what actually triggered the failure, was it the required rule? the regex rule? etc.

The current solution is to assert that the $messageBag messages match those in lang > es > validation.php. However, you end up comparing messages instead of standard keys. E.g:

$this->assertEquals(
    Session::get('errors')->first('client_group'),
    Lang::get('validation.required', array(

        'attribute' => Lang::get('validation.attributes')['client_group'],

    ))
);

Please note that the use of sessionHasErrors is way generic and doesn't let you know what the actual error messages are.

Now imagine that client_group has multiple error messages, you'll have to something like this:

$this->assertTrue(
    array_in(
        Lang::get('validation.required', array(

            'attribute' => Lang::get('validation.attributes')['client_group'],

        )),
        Session::get('errors')->get('client_group')
    )
);

Proposal

Return the validation key along with the message. Instead of returning:

array('message 1', 'message 2', ...)

return:

array('required' => 'message 1', 'regex' => 'message 2', ...)

Which would translate to:

$this->assertTrue(isset(Session::get('errors')->get('username')['required']));

bworwa avatar Jul 04 '14 16:07 bworwa

This would be a pretty big breaking change.

taylorotwell avatar Jul 11 '14 20:07 taylorotwell

#laravel5 :P

GrahamCampbell avatar Jul 11 '14 21:07 GrahamCampbell