dou91808
2018-07-19 07:31
浏览 229
已采纳

Laravel 5.6。 无法在构造函数中获取错误消息

I am trying to get validator error message after it failed, but unsuccessfully. I have this code:

<?php

namespace App\Model\Validators\Common;

use App\Model\Contracts\AbstractClasses\Validator as BaseValidator;
use App\Model\Contracts\Interfaces\Validators\Common\ConversationMessageValidatorInterface;
use Illuminate\Support\Facades\Validator;

class ConversationMessageValidator extends BaseValidator implements ConversationMessageValidatorInterface
{

    /**
     * Validates message sending.
     * @param mixed $data
     * @return boolean
     */
    public function validateCreate($data)
    {

        $this->validator = Validator::make($data, [
            'message' => 'required'
        ]);     
        $this->validator->fails();
        dd($this->validator->errors()->first());

    }  

}

<?php 

namespace App\Model\Contracts\AbstractClasses;

use App\Model\Contracts\Interfaces\Validators\Common\ValidatorInterface;

class Validator implements ValidatorInterface
{

    protected $validator;

    /**
     * Returns validator object.
     * @return mixed
     */ 
    public function getValidator()
    {

        return $this->validator;

    }

    /**
     * Returns validation errors.
     * @return mixed
     */ 
    public function getErrors()
    {

        return $this->getValidator()->errors();

    }   

}   

it dumps this message: "validation.required", even tough I have pretty much the same code in my previous project which works as I expect:

<?php

namespace App\Model\Validators\PresentationalSubsystem;

use Illuminate\Support\Facades\Validator;
use App\Model\Contracts\AbstractClasses\Validator as AbstractValidator;
use App\Model\Contracts\Interfaces\Validators\PresentationalSubsystem\ForumThreadValidatorInterface;

class ForumThreadValidator extends AbstractValidator implements ForumThreadValidatorInterface
{

    public function validateThread($data)
    {

        $this->validator = Validator::make($data, [
            'content' => 'required|string|min:1',
            'description' => 'required|string|min:1',
            'title' => 'required|string|min:1',
            'forum_id' => 'required|exists:forums,id'
        ]);

        $this->validator->fails();
        print_r($this->validator->errors()->first());

    }

}

And this prints out: The content field is required.

My question would be: why my first code does not print error message? instead it prints validation rule that fails. How to change that?

EDIT: validateCreate is called here:

   public function sendMessage($account, $data)
    {

        if(!$this->conversationMessageValidator->validateCreate($data)) {
            return $this->conversationMessageFormatter->prepareCreateResponseWithErrors($this->conversationMessageValidator->getErrors());
        }
}

and this function is called from constructor:

public function sendMessage(Request $request, $conversationId)
{

    $request->merge(['conversation_id' => $conversationId]);
    return $this->conversationService->sendMessage(Auth::user(), $request->all());

}   
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题