master

laravel/framework

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

Validator.php

TLDR

This file defines the Validator interface which represents a contract for validating data and retrieving validation results.

Methods

validate

The validate method runs the validator's rules against its data and returns an array of validation errors. It may throw a ValidationException if the validation fails.

validated

The validated method returns an array containing the attributes and values that were successfully validated. It may throw a ValidationException if the validation fails.

fails

The fails method determines if the data fails the validation rules. It returns true if the validation fails, otherwise it returns false.

failed

The failed method returns an array of the validation rules that failed.

sometimes

The sometimes method adds conditions to a given field based on a closure. It takes an attribute, rules, and a callback as arguments and returns the validator instance.

after

The after method adds an after validation callback. It takes either a callable or a string representing a class method as an argument and returns the validator instance.

errors

The errors method returns an instance of Illuminate\Support\MessageBag containing all of the validation error messages.

<?php

namespace Illuminate\Contracts\Validation;

use Illuminate\Contracts\Support\MessageProvider;

interface Validator extends MessageProvider
{
    /**
     * Run the validator's rules against its data.
     *
     * @return array
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function validate();

    /**
     * Get the attributes and values that were validated.
     *
     * @return array
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function validated();

    /**
     * Determine if the data fails the validation rules.
     *
     * @return bool
     */
    public function fails();

    /**
     * Get the failed validation rules.
     *
     * @return array
     */
    public function failed();

    /**
     * Add conditions to a given field based on a Closure.
     *
     * @param  string|array  $attribute
     * @param  string|array  $rules
     * @param  callable  $callback
     * @return $this
     */
    public function sometimes($attribute, $rules, callable $callback);

    /**
     * Add an after validation callback.
     *
     * @param  callable|string  $callback
     * @return $this
     */
    public function after($callback);

    /**
     * Get all of the validation error messages.
     *
     * @return \Illuminate\Support\MessageBag
     */
    public function errors();
}