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

endian's avatar

Best way to provide custom validation error messages for a field depending on context

I'm trying to figure out the best way to customize my validation errors for a given field depending on context.

Ex: I have a field username which is required in both a login and a registration form. I want the error message to be different on both forms. On the login form - "Your username is required." on the registration form "A username is required."

I know I can pass a 3rd argument to Validator:make with my custom messages in my controller, but this seems dirty. I would rather keep all my messages in language files if possible.

The problem with using custom fields in the validation.php file is that there does not seem to be a way to have context.

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],
0 likes
0 replies

Please or to participate in this conversation.