Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom validation messages #764

Merged

Conversation

eaguad1337
Copy link
Member

Some people would like to customize their app in their own language but Validator class doesn't allow us to specify which message should return if the validation fails.

To resolve it, I extended the validate method to accept a custom parameter messages that will be a dict with custom messages.

For example:

validate = request.validate(
    {
        "email": "required|email",
        "username": "required",
        "secondary_email": "required|email"
    },
    messages={
        "email.required": "Custom Message for required Email.",
        "email.email": "Custom Message for Email not being an email.",
        "username.required": "Custom Message for required Username."
    },
)

Will return:

{"email": ["Custom Message for required Email.", "Custom Message for Email not being an email."], "username": ["Custom Message for required Username."], "secondary_email": ["The secondary_email field is required.", "The secondary_email must be a valid email address."]}

Note that rules without custom message stays the same.

@josephmancuso
Copy link
Member

why are tests not running on this? lol

@eaguad1337
Copy link
Member Author

Which tests? I see them working

image

@eaguad1337 eaguad1337 force-pushed the feat/custom-validator-messages branch from ea77213 to 79f2f80 Compare October 29, 2023 01:27
@josephmancuso josephmancuso merged commit 2b36590 into MasoniteFramework:4.0 Dec 6, 2023
5 of 6 checks passed
@eaguad1337 eaguad1337 deleted the feat/custom-validator-messages branch December 6, 2023 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants