# Validation

### Override auto generated field rules

```php
protected function rules(): array
{
    return array_merge(
        $this->fieldRules(), 
        [ //your custom rules here ]
    );
}
```

### Override auto generated field validation attributes

```php
protected function validationAttributes(): array
{
    return array_merge(
        $this->fieldValidationAttributes(), 
        [ //your custom field validation attributes here ]
    );
}
```

### Form validation settings

```php
// 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.)

```php
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`

```php
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

```php
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
    }
}
```

Read  more about `updatedFooValidate()` on the [lifecycle hooks page](https://tina-hammar.gitbook.io/tall-forms/form/lifecycle-hooks).&#x20;

### Field error message example

```php
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.')
```

### Field validation attribute example

```php
Input::make('Foo', 'bar')
    ->rules('string|between:3,60')
    ->validationAttr('baz')
```

### Conditional rule example

```php
Input::make('Email')
    ->type('email')
    ->fieldWidth('w-full sm:max-w-sm')
    ->prefix('@')
    ->rules($this->model->exists 
        ? [ 'required', 'email', Rule::unique('users', 'email')->ignore($this->model->id)] 
        : 'required|email|unique:users,email')
```

### 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.

```php
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
   ];
}
```

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tina-hammar.gitbook.io/tall-forms/concept/validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
