Return validator messages in custom Rule #45997
Replies: 2 comments
-
When using PHP 8+, you may encounter errors when trying to return validator messages in custom rules in the framework. This can be due to a mismatch in syntax between the older PHP 7.4 version and the newer PHP 8+ version. In order to fix this error, you must modify the code to account for the syntax differences. Specifically, you need to change the return statement in the message function from:
to
This will ensure that the format of the message remains the same and the error is resolved. Additionally, you should also check and update any other code that might be affected by the change in syntax, such as the |
Beta Was this translation helpful? Give feedback.
-
@Kristories You say that the proposed changes will not affect the message format, but that is not the case.
We get an error because there are no sides in the example and return a message:
That's why I have a question to which there is no answer yet, how can you save the format for PHP 8.1 that we have for PHP 7.4 by performing minimal fixes ... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
A problem that I probably didn't know about when switching to PHP 8+: Very often, in the rules I returned a message like this:
When using PHP 7.4 - this isn't a problem, but not for PHP 8+
Since looking "deeper" into how the laravel framework forms messages, we get an error in the replaceAttributePlaceholder method of the FormatsMessages class:
And indeed, if open any editor and run the same code, but for two different versions, we'll get:

Importantly:
If you return the message like this:
We will avoid the error, but accordingly, the format of the message will be different - this doesn't suit me, and the format of the message should remain the same.
Does anyone have any ideas on how to save the format and fix the error?
Beta Was this translation helpful? Give feedback.
All reactions