Eloquent & Data Access v4

Filter Validation

Laravel Model Filter · A Laravel package for filtering and searching models from database with ease.

Version and pages
Commit 2332a8a114bf This page on GitHub

Some filters include validation. DateFilters for example will validate the inserted values for the given format 'Y-m-d'. The used format can be overwritten with the config value 'date_format'. If a given value does not fit the validation, an ValidationException will be thrown.

NumericFilters validates the input for numerical values. You can also add minimum and maximum check for the numeric filters by setting the properties $min and/or $max on the NumericFilter.

<?php

namespace App\Models\Filters;

use Lacodix\LaravelModelFilter\Filters\NumericFilter;

class TestNumericFilter extends NumericFilter
{
    protected string $field = 'fieldname';

    protected int $min = 100;
    protected int $max = 1000;
}

SelectFilter validate against the available options.

Usually invalid values are just ignored. If invalid values are set, the filter is just not applied. You can switch the validation mode to a hard validation, to throw a ValidationException with invalid data. Just set the Validation Mode of your Filter like in the following example.

    ...
    public ValidationMode $validationMode = ValidationMode::THROW;
    ...

You can add your own validation rules to all filters by overwriting the rules() function. You can use all possibilities of Laravels Validator.

use Illuminate\Validation\Rule;

    public function rules(): array
    {
        return [
            'fieldname' => Rule::in($this->options()),
        ];
    }

Prefer the Rule::in() object over an 'in:' . implode(',', ...) string whenever the values are not under your control. Laravel parses the string form with str_getcsv(): an option value with a comma (Trompete, Flügelhorn) is split into two, a leading quote is read as an enclosure that swallows the following values, and a leading space is trimmed away — such a value then fails validation silently and the filter is skipped. Rule::in() quotes every value, so the exact option values are matched. The shipped SelectFilter and BelongsToManyTimeframeFilter rules use it since 4.10.1.

Depending on the ValidationMode of your filter, the filter will not be applied (default) or throw an exception.

The validation mode also covers malformed query parameter structures. See the filter input contract for the supported scalar, list, option-map, and timeframe shapes.

If you wish to customize the validation messages that are thrown with the ValidationException, you can overwrite the $messages property of the Filter.

If you want to keep the default Laravel validation messages, but just customize the :attribute portion of the message, you can specify custom attribute names using the $validationAttributes property.

    protected $messages = [
        'fieldname.numeric' => 'The Field must be a number.',
    ];

and/or

    protected $validationAttributes = [
        'fieldname' => 'Field'
    ];

if you don't know the field name before runtime, you can also overwrite the corresponding methods messages() and validationAttributes()

    protected function messages() {
        return [
            $this->field . '.numeric' => 'The Field must be a number.',
        ];
    }

and/or

    protected function validationAttributes() {
        return [
            $this->field => 'Field',
        ];
    }

Technical consultation

A package as a building block for your application.

If architecture, integration or operations are critical, let’s discuss your application as a whole.

Discuss your project