Validation

The Validator

Radiate's valiator provides a simple API for validating input.

<?php

use Radiate\Validation\ValidationException;

$data= [
    'name' => 'Radiate',
    'url'  => 'https://radiate-framework.github.io/',
]

$rules = [
    'name' => 'required',
    'url'  => 'required|url',
]

try {
    Validator::validate($data, $rules);

} catch(ValidationException $e) {
    var_dump($e->errors());
}

Validation On The Request

Radiate's valiator provides a simple API for basic form validation.

The validate method is made available on the Request class.

public function create(Request $request)
{
    $request->validate([
        'name'     => 'required',
        'email'    => 'requred|email',
        'password' => 'required|min:8',
        'terms'    => 'accepted',
    ]);

    // do stuff
}

Custom Rules

You can create custom validation rules with the make:rule command. Rules are stored in the app/Rules directory.

wp radiate make:rule Uppercase

This will generate:

<?php

namespace Theme\Rules;

use Radiate\Validation\Rules\Rule;

class Uppercase implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed $value
     * @return bool
     */
    public function passes(string $attribute, $value): bool
    {
        return strtoupper($value) === $value;
    }

    /**
     * Get the validation error message.
     *
     * @return string|array
     */
    public function message()
    {
        return ':Attribute must be uppercase';
    }
}

Form Request Validation

Creating Form Requests

For more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that encapsulate their own validation and authorization logic. To create a form request class, you may use the make:request Radiate CLI command:

php artisan make:request RegisterRequest

The generated form request class will be placed in the app/Http/Requests directory. If this directory does not exist, it will be created when you run the make:request command. Each form request generated by Radiate has a method: rules.

As you might have guessed, the rules method returns the validation rules that should apply to the request's data:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'name'     => 'required|max:255',
        'email'    => 'required|email',
        'password' => 'required|min:8',
        'terms'    => 'accepted',
    ];
}

You may type-hint any dependencies you require within the rules method's signature. They will automatically be resolved via the Radiate service container.

So, how are the validation rules evaluated? All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:

/**
 * Store a new blog post.
 *
 * @param \Theme\Http\Requests\StorePostRequest $request
 * @return mixed
 */
public function store(StorePostRequest $request)
{
    // The incoming request is valid...

    // Retrieve the validated input data...
    $validated = $request->validated();
}

If validation fails, an HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.