0

I manage to use the Laravel Validation module in my project like this:

<?php

namespace Models;

use Illuminate\Database\Eloquent\Model as eModel;
use Illuminate\Validation\Factory as ValidatorFactory;
use Symfony\Component\Translation\Translator;

class AbstractModel extends  eModel {



public function validate($data)
{

    $factory = new ValidatorFactory(new Translator('en'));
    $v = $factory->make($data, $this->rules);

    return $v;
}

public function save() {

    $validator = $this->validate($this->toArray());

    if(!$validator->passes())
    {
        $messages = $validator->messages();
        print_r($messages);
        die();
    }

    parent::save();
}

}

If I lunch that i get the follow output:

Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [email] => Array ( [0] => validation.required ) ) [format:protected] => :message )

"validation.required"

I dont know why the module does not get the default error strings. Im pretty sure the module is working ok because if I try to define some custom string like this:

    $message = array(
        'unique' => 'FOO',
        'email' => ':attribute WORNG!!!!!',
        'required' => ':attribute MUST WRITE EMAIL!!!!'
    );
    $factory = new ValidatorFactory(new Translator('en'));
    $v = $factory->make($data, $this->rules, $message);

I get the output:

Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [email] => Array ( [0] => email MUST WRITE EMAIL!!!! ) ) [format:protected] => :message )

What am I missing?

DomingoSL
  • 14,920
  • 24
  • 99
  • 173

1 Answers1

0

Perhaps Translator requires locale of 'en_US'

Denny K
  • 31
  • 3