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
Allow something other than the field "name" property to be the default when showing error messages. #9954
Comments
@evantrimboli It's unnecessary to supply such a feature. You could customize your error message using |
Of course I could customize the label, but that's not the point. It doesn't scale for an app of any large size. There is no ability to have a convention based naming system. For example, all the field names in my app are lower case. This means whenever an error is displayed, it will show as Now say I have 10 fields in a form. For each of those required field, I need to manually set the message because I want the required message to use Also consider I have a field with a min and max value length rule. That means I need to duplicate the text for both rules:
Only because I want the label to be The framework should not just assume that the underlying programmatic name for a field should be something displayed to the user. Why do field labels exist? For this exact reason. The name for the error message should correspond with the label by default, because the label is shown to the user, not the name. There should a way, either globally or potentially per rule some way to automatically transform the the field name before it is sent to the formatter. The other thing you didn't address is that it seems to already have an attempt to support this using I'd suggest you reconsider this,, your answer doesn't seem particularly deeply considered. |
@evantrimboli Similar problem I hit. However, the |
Great, thanks for your post! It would be nice to be able to:
|
Any way to do this now? I'd like to use the |
I was also surprised to find out it that names are used instead of labels in error messages. I believe it should be label by default because a label is what a user sees, not I tried to customize it via: const validateMessages = {
required: "'${label}' is required!",
}; but it doesn't recognize the label property for some reason. |
The message is made by the rule validator and not the form so it doesn't have access to If you send the label in the rule, like : |
@rohit-gohri yes, that works, although I'm using typescript and gives a warning that |
@rohit-gohri 's suggestion is actually pretty promising! Perhaps it could just be made official and added to the documentation. I was even able to get it to work with TypeScript with these three steps:
Usage for required + len: <Form.Item name="zipCode" noStyle rules={getRules("Zip Code", { required: true, len: 5 })}>
<Input placeholder="Zip Code" />
</Form.Item> Usage for required + pattern w/ custom message: <Form.Item
name="legalName"
label="Legal Name"
extra="Full, official name {e.g., Robert Smith)"
rules={getRules(
"Legal Name",
{ required: true },
{
pattern: /^ *[A-Za-z-',.]+( +[A-Za-z-',.]+)+$ */,
message: `'Legal Name' must have at least two words and only these symbols: - ' , . `
}
)}
>
<Input />
</Form.Item> |
Ahh, I didn't realize. Thanks for letting me know 👍 |
Can this be closed? |
What problem does this feature solve?
By default, when an error message is displayed is uses
"%s is required"
, where%s
is the name of the field. The field name isn't necessarily something to present to the user. For example, my field name might beparentId
, but to the user I want to displayParent
. In essence, across the entirety of my app I want to use the label as opposed to the name to display errors.What does the proposed API look like?
I had a browse around the source, it seems as though
fullField
is an attempt to allow this, however it doesn't appear to work correctly. See: https://codesandbox.io/s/v06vq5zlx0 which attempts to use fullField.A potential API would be something like:
The text was updated successfully, but these errors were encountered: