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

change error.field -> rule.field #61

Closed
wants to merge 1 commit into from
Closed

Conversation

iwillwen
Copy link

Actually when we have to do some i18n process on the error message, we have to inject the proper field name into the message but not the field name in code (such as password VS 密码).
In async-validator developer can assign the correct field name with the fullField rule property, but when the field validation is failed the error return should contain the original field name that we can use it to do some other things what it is not do now.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 88.095% when pulling dfe30bb on iwillwen:master into 3bb6943 on yiminghe:master.

@iwillwen
Copy link
Author

iwillwen commented Aug 10, 2017

Example:

const validator = new Schema({
  password: { type: 'string', required: true, min: 6, max: 20, fullField: '密码' }
})
validator.validate({ password: 123456 }, (errors, fields) => {
  console.log(fields.password)  //=> undefined
  console.log(fields['密码'])    //=> correct, but WTF?
})

@yiminghe
Copy link
Owner

No, you can not use fullField, it's not a api, only for internal usage, use message for i18n

@iwillwen
Copy link
Author

iwillwen commented Aug 10, 2017

@yiminghe
Thanks for your response. Actually I knew the fullField is a private variable, but I did not find any way to define a method or a callback function for transforming the field name into the correct format or correct language I want in the source code.
The messages part in async-validator just only allow me to define a regular string format to receive the original field name.
For example, if thepassword field is required so that I want async-validator feedback the message like 密码必填, but not password 必填.
There are two solution to solve the issue.

  1. message could be a function, so that it could be defined by the developers to make the error message just like they want.
validator.messages({
  required: rule => `${i18n(rule.field)}必填`
})
  1. the message of every single rules could be assigned.
new Schema({
  password: {
    type: 'string',
    required: {
      enabled: true,
      message: '密码必填'
    }
  }
})

Or

new Schema({
  password: {
    type: 'string',
    required: true,
    messages: {
      required: '密码必填'
    }
  }
})

@iwillwen iwillwen closed this Aug 13, 2017
@yiminghe
Copy link
Owner

I agree with functional message. Welcome pr.

@elvuel
Copy link

elvuel commented Oct 19, 2021

@yiminghe how about add a LocalizedField to RuleItem? for instance ValidateMessage<[LocalizedField | FullField, ...]> , preserve the default internal validation messages.

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.

None yet

4 participants