dpd7195 2015-09-27 16:19
浏览 44

使用Laravel Validator模块时没有错误消息文本

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?

  • 写回答

1条回答 默认 最新

  • duanboniao5903 2015-09-28 23:41
    关注

    Perhaps Translator requires locale of 'en_US'

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度