laravel-validator
laravel-validator copied to clipboard
$e->getMessage()
I am using the package and run $ e- > getMessage ( ) , I come across " " If you run $ e- > getMessageBag ( ) , run into return without customization
I have the same problem
@gugasobrinho @tookit I had the same issue, Taking a look into the package in the file LaravelValidator,php there is a function Passes which never receives an array of custom messages, I just have overriden it in my entity validator (in my case SkillValidator) with the difference this time i am giving messages array.
<?php
namespace App\Modules\Validators;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;
class SkillValidator extends LaravelValidator
{
protected $rules = [
ValidatorInterface::RULE_CREATE => ['name' => 'required'],
ValidatorInterface::RULE_UPDATE => [],
];
protected $messages = [
'name.required' => 'El campo titulo es un campo requerido'
];
/**
* Pass the data and the rules to the validator
*
* @param string $action
* @return bool
*/
public function passes($action = null)
{
$rules = $this->getRules($action);
$validator = $this->validator->make($this->data, $rules,$this->messages);
if( $validator->fails() )
{
$this->errors = $validator->messages();
return false;
}
return true;
}
}
I hope it may help you. :)
This still does not work, when i use $e->getMessage() returns {"error":true,"message":""}
Use getMessageBag method instead getMessage:
return response()->json(['error' => $e->getMessageBag()], 500);
MessageBag returns => MessageBag {#558 #messages: array:1 [ "fk_employee" => array:1 [ 0 => "validation.unique" ] ] #format: ":message"}