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

时间:2018-07-19 07:31:04

标签: php laravel

失败后,我尝试获取验证器错误消息,但未成功。我有以下代码:

/**
 * 
 */
package test;

import java.util.ArrayList;

/**
 * @author 00990
 *
 */
public class TestDTO extends JsonObject {
    private String code;
    Data data;
    private String message;
    private String token;
    private float status;

    // Getter Methods

    public String getCode() {
        return code;
    }

    public Data getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public String getToken() {
        return token;
    }

    public float getStatus() {
        return status;
    }

    // Setter Methods

    public void setCode(String code) {
        this.code = code;
    }

    public void setData(Data dataObject) {
        this.data = dataObject;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setStatus(float status) {
        this.status = status;
    }


    public static void main(String[] args) {
        String jsonObj = "{\r\n" + "    \"code\": \"2075-4673\",\r\n" + "    \"data\": {\r\n"
                + "        \"requestId\": 4673,\r\n" + "        \"requestCode\": \"2075-4673\",\r\n"
                + "        \"fiscalYear\": {\r\n" + "            \"fiscalYearId\": 2075,\r\n"
                + "            \"fiscalYearCode\": \"2075/76\"\r\n" + "        },\r\n"
                + "        \"requestDate\": 1531851300000,\r\n" + "        \"requestNdate\": \"2075/04/02\",\r\n"
                + "        \"rcAgency\": {\r\n" + "            \"id\": 2373,\r\n"
                + "            \"code\": \"210003501\",\r\n" + "            \"rcAgencyEDesc\": \"ABC\",\r\n"
                + "            \"rcAgencyNDesc\": \" सेवा \",\r\n" + "            \"nepaliName\": \" सेवा\",\r\n"
                + "            \"engishName\": null\r\n" + "        },\r\n" + "        \"status\": 1,\r\n"
                + "        \"pan\": \"500127108\",\r\n" + "        \"payerCode\": null,\r\n"
                + "        \"payerEdesc\": \"ABC Enterprises\",\r\n" + "        \"payerNdesc\": null,\r\n"
                + "        \"payerAddress\": null,\r\n" + "        \"payerPhone\": null,\r\n"
                + "        \"totalAmount\": 14000,\r\n" + "        \"createdBy\": \"psctest\",\r\n"
                + "        \"createdOn\": 1531851300000,\r\n" + "        \"collectedBank\": null,\r\n"
                + "        \"collectedDate\": null,\r\n" + "        \"collectedBy\": null,\r\n"
                + "        \"token\": \"xxxxxxxxxxxxxxxx\",\r\n" + "        \"details\": [\r\n" + "            {\r\n"
                + "                \"ebpNo\": \"4977\",\r\n" + "                \"sno\": 1,\r\n"
                + "                \"depositSlipNo\": null,\r\n" + "                \"purpose\": null,\r\n"
                + "                \"revenueHead\": {\r\n" + "                    \"id\": 14224,\r\n"
                + "                    \"code\": \"14224\",\r\n" + "                    \"oldCode\": \"14224\",\r\n"
                + "                    \"nepaliName\": \"शुल्क\",\r\n"
                + "                    \"englishName\": \"शुल्क\",\r\n"
                + "                    \"description\": \"शुल्क\",\r\n"
                + "                    \"nepaliDescription\": \"शुल्क\",\r\n"
                + "                    \"preRevenueHeadId\": 0,\r\n" + "                    \"status\": true,\r\n"
                + "                    \"federal\": true,\r\n" + "                    \"state\": true,\r\n"
                + "                    \"local\": true,\r\n" + "                    \"remarks\": \"xxxxx\"\r\n"
                + "                },\r\n" + "                \"remarks\": \"remarks\",\r\n"
                + "                \"description\": \"Production\",\r\n" + "                \"currency\": {\r\n"
                + "                    \"currencyId\": 524,\r\n" + "                    \"currencyCode\": \"524\",\r\n"
                + "                    \"descEnglish\": \"NRS\",\r\n"
                + "                    \"descNepali\": \"NRS\"\r\n" + "                },\r\n"
                + "                \"amount\": 14000,\r\n" + "                \"taxAdv\": false,\r\n"
                + "                \"taxyearId\": 2074,\r\n" + "                \"dueAmount\": 14000,\r\n"
                + "                \"createdBy\": \"psctest\",\r\n" + "                \"createdOn\": 1531894162000\r\n"
                + "            }\r\n" + "        ]\r\n" + "    },\r\n"
                + "    \"message\": \"Voucher has saved sucessfully.\",\r\n"
                + "    \"token\": \"xxxxxxxxxxxxxxxxxxxx\",\r\n" + "    \"status\": 0\r\n" + "}";
        System.out.println("Token :" +((TestDTO) new TestDTO().toObject(jsonObj)).getToken());
        System.out.println(" PAN  :" +((TestDTO) new TestDTO().toObject(jsonObj)).getData().getPan());
        System.out.println("EBPNo :" +((TestDTO) new TestDTO().toObject(jsonObj)).getData().getDetails().get(0).getEbpNo());
    }
}

class Data extends JsonObject {
    private float requestId;
    private String requestCode;
    FiscalYear FiscalYearObject;
    private float requestDate;
    private String requestNdate;
    RcAgency RcAgencyObject;
    private float status;
    private String pan;
    private String payerCode = null;
    private String payerEdesc;
    private String payerNdesc = null;
    private String payerAddress = null;
    private String payerPhone = null;
    private float totalAmount;
    private String createdBy;
    private float createdOn;
    private String collectedBank = null;
    private String collectedDate = null;
    private String collectedBy = null;
    private String token;
    ArrayList<Detail> details = new ArrayList<Detail>();

    // Getter Methods

    public float getRequestId() {
        return requestId;
    }

    public ArrayList<Detail> getDetails() {
        return details;
    }

    public void addToDetails(Detail detail) {
        if (details == null) {
            details = new ArrayList<Detail>();
        }
        this.details.add(detail);
    }

    public String getRequestCode() {
        return requestCode;
    }

    public FiscalYear getFiscalYear() {
        return FiscalYearObject;
    }

    public float getRequestDate() {
        return requestDate;
    }

    public String getRequestNdate() {
        return requestNdate;
    }

    public RcAgency getRcAgency() {
        return RcAgencyObject;
    }

    public float getStatus() {
        return status;
    }

    public String getPan() {
        return pan;
    }

    public String getPayerCode() {
        return payerCode;
    }

    public String getPayerEdesc() {
        return payerEdesc;
    }

    public String getPayerNdesc() {
        return payerNdesc;
    }

    public String getPayerAddress() {
        return payerAddress;
    }

    public String getPayerPhone() {
        return payerPhone;
    }

    public float getTotalAmount() {
        return totalAmount;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public float getCreatedOn() {
        return createdOn;
    }

    public String getCollectedBank() {
        return collectedBank;
    }

    public String getCollectedDate() {
        return collectedDate;
    }

    public String getCollectedBy() {
        return collectedBy;
    }

    public String getToken() {
        return token;
    }

    // Setter Methods

    public void setRequestId(float requestId) {
        this.requestId = requestId;
    }

    public void setRequestCode(String requestCode) {
        this.requestCode = requestCode;
    }

    public void setFiscalYear(FiscalYear fiscalYearObject) {
        this.FiscalYearObject = fiscalYearObject;
    }

    public void setRequestDate(float requestDate) {
        this.requestDate = requestDate;
    }

    public void setRequestNdate(String requestNdate) {
        this.requestNdate = requestNdate;
    }

    public void setRcAgency(RcAgency rcAgencyObject) {
        this.RcAgencyObject = rcAgencyObject;
    }

    public void setStatus(float status) {
        this.status = status;
    }

    public void setPan(String pan) {
        this.pan = pan;
    }

    public void setPayerCode(String payerCode) {
        this.payerCode = payerCode;
    }

    public void setPayerEdesc(String payerEdesc) {
        this.payerEdesc = payerEdesc;
    }

    public void setPayerNdesc(String payerNdesc) {
        this.payerNdesc = payerNdesc;
    }

    public void setPayerAddress(String payerAddress) {
        this.payerAddress = payerAddress;
    }

    public void setPayerPhone(String payerPhone) {
        this.payerPhone = payerPhone;
    }

    public void setTotalAmount(float totalAmount) {
        this.totalAmount = totalAmount;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public void setCreatedOn(float createdOn) {
        this.createdOn = createdOn;
    }

    public void setCollectedBank(String collectedBank) {
        this.collectedBank = collectedBank;
    }

    public void setCollectedDate(String collectedDate) {
        this.collectedDate = collectedDate;
    }

    public void setCollectedBy(String collectedBy) {
        this.collectedBy = collectedBy;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

class RcAgency extends JsonObject {
    private float id;
    private String code;
    private String rcAgencyEDesc;
    private String rcAgencyNDesc;
    private String nepaliName;
    private String engishName = null;

    // Getter Methods

    public float getId() {
        return id;
    }

    public String getCode() {
        return code;
    }

    public String getRcAgencyEDesc() {
        return rcAgencyEDesc;
    }

    public String getRcAgencyNDesc() {
        return rcAgencyNDesc;
    }

    public String getNepaliName() {
        return nepaliName;
    }

    public String getEngishName() {
        return engishName;
    }

    // Setter Methods

    public void setId(float id) {
        this.id = id;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setRcAgencyEDesc(String rcAgencyEDesc) {
        this.rcAgencyEDesc = rcAgencyEDesc;
    }

    public void setRcAgencyNDesc(String rcAgencyNDesc) {
        this.rcAgencyNDesc = rcAgencyNDesc;
    }

    public void setNepaliName(String nepaliName) {
        this.nepaliName = nepaliName;
    }

    public void setEngishName(String engishName) {
        this.engishName = engishName;
    }
}

class FiscalYear extends JsonObject {
    private float fiscalYearId;
    private String fiscalYearCode;

    // Getter Methods

    public float getFiscalYearId() {
        return fiscalYearId;
    }

    public String getFiscalYearCode() {
        return fiscalYearCode;
    }

    // Setter Methods

    public void setFiscalYearId(float fiscalYearId) {
        this.fiscalYearId = fiscalYearId;
    }

    public void setFiscalYearCode(String fiscalYearCode) {
        this.fiscalYearCode = fiscalYearCode;
    }


}

它转储此消息:“ validation.required”,即使很难,我在以前的项目中也有几乎相同的代码,可以按我的预期工作:

<?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();

    }   

}   

这会打印出来:必填内容字段。

我的问题是:为什么我的第一个代码不显示错误消息?而是打印失败的验证规则。如何更改?

编辑: validateCreate在这里被调用:

<?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());

    }

}

此函数从构造函数中调用:

   public function sendMessage($account, $data)
    {

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

2 个答案:

答案 0 :(得分:0)

if ($validator->fails()) {
    $messages = [];
    foreach ($validator->messages()->getMessages() as $field_name => $message){
         $messages[] = $message[0];
     }
    dd($messages);

}

答案 1 :(得分:0)

我找到了解决方案。之前一段时间不小心删除了resources / lang / validation.php文件,这就是为什么它返回消息的实际路径而不是消息的原因。我一直以为这是一个错误代码,但是我错了。