Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Validation/ValidationException.php
Строк: 137
<?php

namespace IlluminateValidation;

use 
Exception;
use 
IlluminateSupportArr;
use 
IlluminateSupportFacadesValidator as ValidatorFacade;

class 
ValidationException extends Exception
{
    
/**
     * The validator instance.
     *
     * @var IlluminateContractsValidationValidator
     */
    
public $validator;

    
/**
     * The recommended response to send to the client.
     *
     * @var SymfonyComponentHttpFoundationResponse|null
     */
    
public $response;

    
/**
     * The status code to use for the response.
     *
     * @var int
     */
    
public $status 422;

    
/**
     * The name of the error bag.
     *
     * @var string
     */
    
public $errorBag;

    
/**
     * The path the client should be redirected to.
     *
     * @var string
     */
    
public $redirectTo;

    
/**
     * Create a new exception instance.
     *
     * @param  IlluminateContractsValidationValidator  $validator
     * @param  SymfonyComponentHttpFoundationResponse|null  $response
     * @param  string  $errorBag
     * @return void
     */
    
public function __construct($validator$response null$errorBag 'default')
    {
        
parent::__construct(static::summarize($validator));

        
$this->response $response;
        
$this->errorBag $errorBag;
        
$this->validator $validator;
    }

    
/**
     * Create a new validation exception from a plain array of messages.
     *
     * @param  array  $messages
     * @return static
     */
    
public static function withMessages(array $messages)
    {
        return new static(
tap(ValidatorFacade::make([], []), function ($validator) use ($messages) {
            foreach (
$messages as $key => $value) {
                foreach (
Arr::wrap($value) as $message) {
                    
$validator->errors()->add($key$message);
                }
            }
        }));
    }

    
/**
     * Create an error message summary from the validation errors.
     *
     * @param  IlluminateContractsValidationValidator  $validator
     * @return string
     */
    
protected static function summarize($validator)
    {
        
$messages $validator->errors()->all();

        if (! 
count($messages) || ! is_string($messages[0])) {
            return 
$validator->getTranslator()->get('The given data was invalid.');
        }

        
$message array_shift($messages);

        if (
$count count($messages)) {
            
$pluralized $count === 'error' 'errors';

            
$message .= ' '.$validator->getTranslator()->get("(and :count more $pluralized)"compact('count'));
        }

        return 
$message;
    }

    
/**
     * Get all of the validation error messages.
     *
     * @return array
     */
    
public function errors()
    {
        return 
$this->validator->errors()->messages();
    }

    
/**
     * Set the HTTP status code to be used for the response.
     *
     * @param  int  $status
     * @return $this
     */
    
public function status($status)
    {
        
$this->status $status;

        return 
$this;
    }

    
/**
     * Set the error bag on the exception.
     *
     * @param  string  $errorBag
     * @return $this
     */
    
public function errorBag($errorBag)
    {
        
$this->errorBag $errorBag;

        return 
$this;
    }

    
/**
     * Set the URL to redirect to on a validation error.
     *
     * @param  string  $url
     * @return $this
     */
    
public function redirectTo($url)
    {
        
$this->redirectTo $url;

        return 
$this;
    }

    
/**
     * Get the underlying response instance.
     *
     * @return SymfonyComponentHttpFoundationResponse|null
     */
    
public function getResponse()
    {
        return 
$this->response;
    }
}
Онлайн: 0
Реклама