lundi 19 septembre 2016

Laravel 5, custom validation rule - how to show different error messages on validation result

I've a custom validation rule added to AppServiceProvider boot() method as following:

   Validator::extend('validate_item', function($attribute, $value, $parameters, $validator) use($service) {
        try {
            $service->checkItem($value);
        }catch (ItemNotFound $e){
            return false;
        }catch(MultipleExists $e){
            return false;
        }
        return true;
    });

as you can see checkItem($value) method throws two exceptions depending on if item not exists or if multiple items exists with the same $value.

I want to show different messages according to the exception. Is there a built in way to do this? I don't want to make two validation rules becoz the $service->checkItem() method is resource expensive, contacts a remote service.

Documentation only talks about using a single message, and :attribute re-placer if needed



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire