duanfu3390 2015-12-20 08:29
浏览 292

Laravel自定义验证消息无效

I am using repository pattern using L5-repository validation. This is my

controller

<?php

namespace App\Api\V1\Controllers;

use Request;
use Dingo;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use App\Api\V1\Transformers\BankAccountTransformer;
use Prettus\Validator\Exceptions\ValidatorException;
use \Prettus\Validator\Contracts\ValidatorInterface;
use App\Api\V1\Validations\BankAccountValidator;

   public function store()
    {
        $request = Request::all();
        try {
                //Validate request body
                $this->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

<?php

    namespace App\Api\V1\Validations;

    use \Prettus\Validator\Contracts\ValidatorInterface;
    use \Prettus\Validator\LaravelValidator;

    class BankAccountValidator extends LaravelValidator {

        protected $rules = [
            ValidatorInterface::RULE_CREATE => [
            '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"
      ]
    ]
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?