master

laravel/framework

Last updated at: 29/12/2023 09:26

ValidationException.php

TLDR

The ValidationException.php file is a part of the Laravel framework's Illuminate\Validation namespace. It extends the base Exception class and defines a custom ValidationException class. This class is responsible for handling and throwing exceptions related to validation errors during form submission.

Methods

There are several methods defined in the ValidationException class:

__construct($validator, $response = null, $errorBag = 'default')

This method constructs a new instance of the ValidationException class. It accepts three arguments: $validator which represents the validation object, $response which represents the response object, and $errorBag which represents the name of the error bag.

withMessages(array $messages)

This static method creates a new validation exception from a plain array of messages. It accepts an array of $messages and returns a new instance of the ValidationException class.

summarize($validator)

This protected method creates an error message summary from the validation errors. It accepts a $validator object and returns a string representing the error message summary.

errors()

This method returns all of the validation error messages.

status($status)

This method sets the HTTP status code to be used for the response. It accepts an integer $status and returns the current instance of the ValidationException class.

errorBag($errorBag)

This method sets the error bag on the exception. It accepts a string $errorBag and returns the current instance of the ValidationException class.

redirectTo($url)

This method sets the URL to redirect to on a validation error. It accepts a string $url and returns the current instance of the ValidationException class.

getResponse()

This method returns the underlying response instance.

Classes

Class ValidationException

The ValidationException class extends the base Exception class and handles exceptions related to validation errors during form submission. It has several properties such as $validator, $response, $status, $errorBag, and $redirectTo. It also contains various methods to construct a new instance, get error messages, set status code, error bag, and redirect URL.

<?php

namespace Illuminate\Validation;

use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator as ValidatorFacade;

class ValidationException extends Exception
{
    /**
     * The validator instance.
     *
     * @var \Illuminate\Contracts\Validation\Validator
     */
    public $validator;

    /**
     * The recommended response to send to the client.
     *
     * @var \Symfony\Component\HttpFoundation\Response|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  \Illuminate\Contracts\Validation\Validator  $validator
     * @param  \Symfony\Component\HttpFoundation\Response|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  \Illuminate\Contracts\Validation\Validator  $validator
     * @return string
     */
    protected static function summarize($validator)
    {
        $messages = $validator->errors()->all();

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

        $message = array_shift($messages);

        if ($additional = count($messages)) {
            $pluralized = $additional === 1 ? 'error' : 'errors';

            $message .= " (and {$additional} more {$pluralized})";
        }

        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 \Symfony\Component\HttpFoundation\Response|null
     */
    public function getResponse()
    {
        return $this->response;
    }
}