Jure Špik
Jure Špik

Reputation: 331

How do I add my message to the $errors variable in Laravel 4.2

I tried extending the Laravel 4.2 validation to check if all form data is valid (not just some fields) according to some business rules.

I could not extend the custom validator in any normal way (either too hacky or not DRY enough) so I decided to add the business rules check after validator succeeds, and then add my own error messages and redirect.

It is still ugly and it does not work but for now I would be happy if someone tells me how to add my message to the session. If you feel like telling me how I should use Laravel's custom validators I've included some more code so you can be more specific..:

$validator = Validator::make(Input::all(), $this->validationRules, $this->validationMessages);
if ($validator->fails()) {
    if (0 == $id) {
        return Redirect::route('x.create')->withErrors($validator)->withInput();
    } else {
        return Redirect::route('x.edit', $id)->withErrors($validator)->withInput();
    }
} else {
    //TESTs
    if ($this->AlreadyExists($id)) {
        $messages = new MessageBag();
        if ($request->getSession()->get('errors')){
            $messages = $request->getSession()->get('errors')->getBag('default');
        }
        $messages->add('form', 'same data already exists');

        if (0 == $id) {
            return Redirect::route('x.create')->withErrors($messages)->withInput();
        } else {
            return Redirect::route('x.edit', $id)->withErrors($messages)->withInput();
        }
    }
}

//all is ok. Save/update entity
...

The code for setting the session is first 5 lines after check for AlreadyExists. I got it from some forum but it doesn't seem to work ok (I get some "non-object" exceptions if I include the code so it seems it corrupts the session object)

I don't think I have time to upgrade to L5.

Upvotes: 2

Views: 1194

Answers (2)

Hassan Gilak
Hassan Gilak

Reputation: 699

for anyone who uses form request I suggest to take a look at this implementations. formatErrors in laravel 5.1 documentations mentioned for a way to manipulate the way errors display. I just simply jump in it and added some codes to extend errors.

use Illuminate\Contracts\Validation\Validator;

class ProfileChangePasswordRequest extends Request
{
    public function authorize()
    {
        if (auth()->check())
            return true;

        return false;
    }

    public function rules()
    {
        return [
            'password' => "required|confirmed|min:6"
        ];
    }

    protected function formatErrors(Validator $validator)
    {
        if ($this->isCurrentPasswordValidate()) {
            return $validator->errors()->all();
        } else {
            $validator->errors()->add('current_password', 'current password is wrong');
            return parent::formatErrors($validator);
        }
    }

    public function isCurrentPasswordValidate()
    {
        return auth()->validate(['email' => auth()->user()->email,
            'password' => $this->current_password]);
    }
}

Upvotes: 0

Jure Špik
Jure Špik

Reputation: 331

I solved this by creating a new error message bag and adding it to redirect:

$mb = new Illuminate\Support\MessageBag();
$mb->add("form", "same data already exists");
...
return Redirect::route('x.create')->withErrors($mb)->withInput();

Seems I needed to add full namespace for bag, but most of all I needed rest :)

The validation logic is still bad but no time to tinker with that now.

Upvotes: 2

Related Questions