protected function rules(): array
{
return array_merge(
$this->fieldRules(),
[ //your custom rules here ]
);
}
Override auto generated field validation attributes
protected function validationAttributes(): array
{
return array_merge(
$this->fieldValidationAttributes(),
[ //your custom field validation attributes here ]
);
}
Form validation settings
// config/tall-forms
'form' => [
'labelsAsAttributes' => true, //use field labels as validation :attribute
'notifyErrors' => true, //display notification errors as popups, not just text
]
// override in component
protected function formAttr(): array
{
return [
'labelsAsAttributes' => true,
'notifyErrors' => true,
]
}
Field validation methods
Fields are automatically validated when the form is submitted. (If you extend the TALL-form component.)
errorMsg(string $string) //custom validation message
keyAsAttribute() //use the field key as validation :attribute, overrides $form->labelsAsAttributes
validationAttr() //Set a custom validation :attribute
rules(mixed $rules) //defaults to 'nullable', Use any Laravel rule
Field real-time validation methods
You can disable/enable automatic real-time validation with different field methods, depending on the field type. Defaults are set in config/tall-forms field-attributes
deferEntangle(true)//realtime validation off
deferEntangle(false)//realtime validation on
wire('lazy')//realtime validation on, validates when the field looses focus
wire('defer')//update field value on next request, realtime validation off
realtimeValidationOff() //consider using wire('defer) instead
Examples
Validation rules precedence
class FooForm extends TallFormComponent
{
//rules() takes precedence
protected function rules(): array
{
return [
'email' => ['required', 'email', 'not_in:' . auth()->user()->email],
];
}
//overridden by rules() method
protected array $rules = [
'email' => 'nullable',
];
//overridden by both rules() and $rules
protected function fields(): array
{
return [
Email::make('Email')->rules('required|string');
];
}
//real-time validation hook where you can override all other defined rules
protected function updatedEmailValidate($value)
{
//you need to call $this->validateOnly(...) manually here.
//see Livewire documentation https://laravel-livewire.com/docs/2.x/input-validation
}
}
Tags::make('Tags')
->rules('string|between:3,60')
->errorMsg('The tag must be between 3 to 60 chars long and only consist of alpha-numeric characters.')
Component $rules combined with field->rules(), example
Some fields of array type apply field rules to each item in the array. If you want a rule that applies to the array instance you have to add it manually using Livewire default $rules property or rules() method.
protected array $rules = [
'topics' => 'required|array|min:1',
];
protected function fields()
{
return [
InputArray::make('Topics')
->type('text')
->minItems(1)
->help('Please list at least one topic')
->rules('required|string|between:3,5'); //the rules are applied to each input field
];
}