aerni / statamic-livewire-forms

Supercharge your Statamic forms with Livewire
https://statamic.com/addons/aerni/livewire-forms
Other
28 stars 2 forks source link

Allow for more complex field validation scenarios #33

Closed aerni closed 1 year ago

aerni commented 1 year ago

This PR moves the rules and validationAttributes into the Field class. This allows for better control of the validation of a field.

I came across this issue when working on a field model for the Grid fieldtype that deals with an array of data. The current implementation didn't allow for this scenario. Now you can simply override the default rules and validationAttributes methods if needed.

public function rules(): array
{
    return [
        "{$this->key}" => 'required|array',
        "{$this->key}.*.issue" => 'required|string',
        "{$this->key}.*.amount" => 'required|integer|min:1',
    ];
}

public function validationAttributes(): array
{
    return [
        "{$this->key}" => 'Ausgaben',
        "{$this->key}.*.issue" => 'Ausgabe',
        "{$this->key}.*.amount" => 'Anzahl',
    ];
}