You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{getTranslate,getActiveLanguage,addTranslation}from'react-localize-redux';constmessages={"validate": {"required": ["필수항목","Required","JP-Required","ZH-Required","FR-Required","ES-Required"],}};classNineSqValidatorObj{constructor(dispatch,translate){dispatch(addTranslation(messages));this.translate=translate;this.required=this.required.bind(this);}required(value){return(value ? undefined : this.translate('validate.required'));}maxLength(max){returnvalue=>value&&value.length>max ? `Must be ${max} characters or less` : undefined;}maxLengthEmail=this.maxLength(15);minLength(min){returnvalue=>value&&value.length<min ? `Must be ${min} characters or more` : undefined;}minLength2=this.minLength(2);number(value){returnvalue&&isNaN(Number(value)) ? 'Must be a number' : undefined;}minValue(min){returnvalue=>value&&value<min ? `Must be at least ${min}` : undefined;}minValue18=this.minValue(18);email(value){returnvalue&&!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
? 'Invalid email address'
: undefined;}alphaNumeric(value){returnvalue&&/[^a-zA-Z0-9 ]/i.test(value)
? 'Only alphanumeric characters'
: undefined;}phoneNumber(value){returnvalue&&!/^(0|[1-9][0-9]{9})$/i.test(value)
? 'Invalid phone number, must be 10 digits'
: undefined;}}exportdefaultNineSqValidatorObj;
I could try to implement it as HOC, however I thought it's wired to have render() in validator functions. So simply use it as a class. The current symptom is it won't find validate.required key.
If anyone can suggest how to implement this way, could you let me know?
The text was updated successfully, but these errors were encountered:
aJoohongKim
pushed a commit
to krsoft/loopback-react-materialui-redux
that referenced
this issue
Dec 26, 2017
Your validator function only really cares about what message it needs to display when value is invalid. My recommendation would be to avoid passing down the translate function, and instead provide the translated message to the validator. For example maybe each of your validation functions takes an additional param like so....
or if you want you could provide some sort of key value pair list of error messages from the component that has access to translate like so...
// from your connected component create a mapping of messagesconstmessages={required: this.props.translate('validate.required'),email: this.props.translate('validate.email'),maxLength: this.props.translate('validate.maxLength'),
...
};// pass those translated messages to your classnewNineSqValidatorObj(messages);classNineSqValidatorObj{constructor(messages){this.messages=messages;}// then use the messages from functionsemail(value,msg){returnvalue&&!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
? this.messages.email
: undefined;}}
Thank you very much, @ryandrewjohnson. I've got your idea. You approach cleared my confusions. What I wanted was centralize error messages in one place. validator functions don't have to have the message at all. I will put all my error messages into a file and import it when I need it.
I am writing validation library for redux-form using redux-localize-redux.
The followings are snippets from my project.
loginform.js
NineSqValidatorObj.js
I could try to implement it as HOC, however I thought it's wired to have render() in validator functions. So simply use it as a class. The current symptom is it won't find validate.required key.
If anyone can suggest how to implement this way, could you let me know?
The text was updated successfully, but these errors were encountered: