Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andyz's avatar
Level 9

Livewire attributes live validation with translation

Is there a way to keep live validation in Livewire, while having the error messages translated?

I have this

#[Validate('required', message: 'The name is required')]
 public $name;

but if I use

__('vallidation.name')

I get errors, while using

public function rules() 
{
}

is not working for live validation

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To achieve live validation in Livewire with translated error messages, you can use the rules method in combination with the messages method to define your validation rules and custom messages. Here's how you can do it:

  1. Define your validation rules in the rules method.
  2. Define your custom error messages in the messages method, using the __ helper function for translations.

Here's an example of how you can set this up:

use Livewire\Component;

class YourComponent extends Component
{
    public $name;

    protected $rules = [
        'name' => 'required',
    ];

    protected function messages()
    {
        return [
            'name.required' => __('validation.name'),
        ];
    }

    public function updated($propertyName)
    {
        $this->validateOnly($propertyName);
    }

    public function save()
    {
        $this->validate();

        // Save logic here
    }

    public function render()
    {
        return view('livewire.your-component');
    }
}

In this example:

  • The rules property defines the validation rules for the name attribute.
  • The messages method returns an array of custom error messages, where the __('validation.name') function is used to fetch the translated message.
  • The updated method is used to perform live validation whenever a property is updated.
  • The save method validates all the properties before performing the save logic.

Make sure you have the appropriate translation strings defined in your resources/lang/{locale}/validation.php file, like so:

return [
    'name' => 'The name is required',
];

This setup ensures that your Livewire component performs live validation with translated error messages.

2 likes

Please or to participate in this conversation.