master

laravel/framework

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

ValidatesWhenResolvedTrait.php

TLDR

The file ValidatesWhenResolvedTrait.php provides a default implementation of the ValidatesWhenResolved contract for validation in Laravel.

Methods

validateResolved

This method validates the class instance. It performs the following actions:

  • Calls the prepareForValidation method.
  • Checks if the authorization passes. If not, it throws a failed authorization exception.
  • Gets the validator instance.
  • Adds an after validation hook if the validation is precognitive.
  • Checks if the validation fails. If so, it throws a validation exception.
  • Calls the passedValidation method.

prepareForValidation

This method prepares the data for validation. It does not perform any actions by default.

getValidatorInstance

This method returns the validator instance for the request.

passedValidation

This method is called when the validation attempt passes. It does not perform any actions by default.

failedValidation

This method is called when the validation attempt fails. It takes a Validator instance as a parameter and throws a validation exception.

passesAuthorization

This method determines if the request passes the authorization check. If the class has an authorize method, it calls that method. Otherwise, it returns true.

failedAuthorization

This method is called when the authorization attempt fails. It throws an unauthorized exception.

<?php

namespace Illuminate\Validation;

use Illuminate\Foundation\Precognition;

/**
 * Provides default implementation of ValidatesWhenResolved contract.
 */
trait ValidatesWhenResolvedTrait
{
    /**
     * Validate the class instance.
     *
     * @return void
     */
    public function validateResolved()
    {
        $this->prepareForValidation();

        if (! $this->passesAuthorization()) {
            $this->failedAuthorization();
        }

        $instance = $this->getValidatorInstance();

        if ($this->isPrecognitive()) {
            $instance->after(Precognition::afterValidationHook($this));
        }

        if ($instance->fails()) {
            $this->failedValidation($instance);
        }

        $this->passedValidation();
    }

    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        //
    }

    /**
     * Get the validator instance for the request.
     *
     * @return \Illuminate\Validation\Validator
     */
    protected function getValidatorInstance()
    {
        return $this->validator();
    }

    /**
     * Handle a passed validation attempt.
     *
     * @return void
     */
    protected function passedValidation()
    {
        //
    }

    /**
     * Handle a failed validation attempt.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        $exception = $validator->getException();

        throw new $exception($validator);
    }

    /**
     * Determine if the request passes the authorization check.
     *
     * @return bool
     */
    protected function passesAuthorization()
    {
        if (method_exists($this, 'authorize')) {
            return $this->authorize();
        }

        return true;
    }

    /**
     * Handle a failed authorization attempt.
     *
     * @return void
     *
     * @throws \Illuminate\Validation\UnauthorizedException
     */
    protected function failedAuthorization()
    {
        throw new UnauthorizedException;
    }
}