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

Chingy's avatar
Level 6

Livewire Forms and Rules and Validation Attributes

I see that you can use a Form and put inside the #[Validate], the rule , the as (validation attribute) and message , the message.

My first question is, how can I pass to message and validation attributes strings that are translatable? such as __("My translatable string") ?

If you answer is create a function validationAttributes() inside the form object, My second question will be then, if i add validationAttributes and messages to the "parent" LW component, will Livewire merge both messages of "parent" component AND Form object?

My third question is, if I do ->validate() in parent component, and if my rules are declared as function in both places (parent component and form object) will it run for all of them ?

Thanks in advance

0 likes
2 replies
LaryAI's avatar
Level 58

To address your questions regarding Livewire forms, validation rules, and translatable strings, let's break it down step by step.

Question 1: Translatable Validation Messages and Attributes

To use translatable strings in your validation messages and attributes, you can utilize Laravel's __() helper function. Here's how you can do it:

  1. Validation Messages: You can define your validation messages in the messages() method of your Livewire component and use the __() helper to make them translatable.

  2. Validation Attributes: Similarly, you can define your validation attributes in the validationAttributes() method and use the __() helper.

Here's an example:

Question 2: Merging Messages and Validation Attributes from Parent and Form Object

Livewire does not automatically merge validation messages and attributes from the parent component and the form object. You need to handle this manually if you want to combine them.

One way to achieve this is by merging the arrays in the messages() and validationAttributes() methods. Here's an example:

In this example, the ParentComponent manually merges the messages and validation attributes from the MyFormComponent with its own.

Summary

  • Use the __() helper function to make validation messages and attributes translatable.
  • Manually merge validation messages and attributes from the parent component and the form object if needed.

I hope this helps! If you have any further questions, feel free to ask.

Chingy's avatar
Chingy
OP
Best Answer
Level 6

So i played around the core files and I found that the $this->validate() called in the parent component actually does both validations but separately.

Please or to participate in this conversation.