andersao / andersao/laravel-validator

Custom validation message not coming

Aperta
#10 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
PHP
Stelle
406
Fork
88
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I am using repository pattern using [L5-repository validation](https://github.com/andersao/l5-repository#validators). This is my
## controller

```
validator->with( $request )->passesOrFail(ValidatorInterface::RULE_CREATE);

} catch (ValidatorException $e) {
throw new Dingo\Api\Exception\StoreResourceFailedException('Unable to add bank account', $e->getMessageBag());
// Wiki of the project says to use getMessage(), but it throws error

}
}
```
## Validation class

```
[
'bank_name' => 'required'
],
ValidatorInterface::RULE_UPDATE => [
]
];

protected $messages = [
'bank_name.required' => 'My custom error'
];

}
```

But I am getting this error when I use **$e->getMessage()** .

```
"message": "Call to a member function isEmpty() on string"
```

If I am using **$e->getMessageBag()** I am getting proper error response. But my custom messages are not displayed. A generic message is displayed.

```
"errors": {
"bank_name": [
"The bank name field is required."
]
}
```

It actually should have been

```
"errors": {
"bank_name": [
"My customer error"
]
}
```

dd($e)

```
ValidatorException {#505
#messageBag: MessageBag {#507
#messages: array:1 [
"bank_name" => array:1 [
0 => "The bank name field is required."
]
]
#format: ":message"
}
#message: ""
#code: 0
#file: "/home/vagrant/dev-develop/vendor/prettus/laravel-validation/src/Prettus/Validator/AbstractValidator.php"
#line: 109
-trace: array:58 [
0 => array:3 [
"call" => "Prettus\Validator\AbstractValidator->passesOrFail()"
"file" => "/home/vagrant/dev-develop/app/Api/V1/Controllers/BankAccountController.php:61"
"args" => array:1 [
0 => "create"
]
]
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.