TALL-forms
  • TALL-forms
  • Installation
    • Quickstart: Artisan cmd
    • Manual installation
      • Package
      • Translations
      • Css & Theme
      • Javascript
      • Tailwind
      • Laravel Mix
      • Wrapper view
      • Laravel Jetstream/Breeze
  • Upgrade v7 to v8
    • Configuration changes
    • Component & Field changes
    • Blade view changes
    • Notification changes
    • Styling changes
  • Concept
    • Configuration
    • Validation
    • Styling
    • Icons
    • Javascript
    • Layout vs Wrapper
    • Model binding
    • Manually saving data
    • Extend Blade Components
  • Create Forms
    • Form Data
    • Form Types
      • Standard form
      • As modal
      • Without buttons
      • Fields only
      • Headless
    • Form attributes
    • Form buttons
    • Form slots
    • Form methods
    • Lifecycle hooks
    • Render a form
    • Mass generate
  • Fields
    • Declaration
    • Labels
    • Field Slots
    • Field methods
    • Custom field attributes
    • Conditional field
    • Custom view
    • Custom field
    • Custom Livewire component
    • Relations
    • Fields
      • Checkbox
      • Checkboxes
      • FileUpload
      • Honeypot
      • ImageCropper
      • Input
      • Password
      • InputArray
      • KeyVal (array)
      • Radio
      • Range
      • Repeater (array)
      • Search
      • Select & Multiselect
      • Tags
      • TagsSearch
      • Textarea
      • Trix
      • SpatieTags
    • Sponsor Fields
      • More Inputs
      • CKEditor
      • DatePicker
      • Heading
      • Markdown
      • Panels
      • SearchList
      • SelectOptGroup
      • Tabs
      • Trix, file-uploads
  • Blade components
    • Notifications
    • Label wrapper
    • Button
    • Modal blade component
    • Modal form blade component
  • Examples
    • Input examples
    • Array fields example
Powered by GitBook
On this page
  • Override auto generated field rules
  • Override auto generated field validation attributes
  • Form validation settings
  • Field validation methods
  • Field real-time validation methods
  • Examples
  • Validation rules precedence
  • Field error message example
  • Field validation attribute example
  • Conditional rule example
  • Component $rules combined with field->rules(), example

Was this helpful?

  1. Concept

Validation

Override auto generated field rules

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

Field error message example

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

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

Conditional rule example

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.

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

PreviousConfigurationNextStyling

Last updated 3 years ago

Was this helpful?

Read more about updatedFooValidate() on the .

lifecycle hooks page