laravel-validator icon indicating copy to clipboard operation
laravel-validator copied to clipboard

$e->getMessage()

Open gustavosobrinho01 opened this issue 8 years ago • 5 comments

I am using the package and run $ e- > getMessage ( ) , I come across " " If you run $ e- > getMessageBag ( ) , run into return without customization

gustavosobrinho01 avatar Jul 14 '16 18:07 gustavosobrinho01

I have the same problem

tookit avatar Aug 09 '16 13:08 tookit

@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. :)

blogui91 avatar Aug 13 '16 06:08 blogui91

This still does not work, when i use $e->getMessage() returns {"error":true,"message":""}

heavyrick avatar Mar 30 '17 14:03 heavyrick

Use getMessageBag method instead getMessage:

return response()->json(['error' => $e->getMessageBag()], 500);

touchinformatica avatar Jul 22 '17 16:07 touchinformatica

MessageBag returns => MessageBag {#558 #messages: array:1 [ "fk_employee" => array:1 [ 0 => "validation.unique" ] ] #format: ":message"}

heavyrick avatar Oct 05 '17 14:10 heavyrick