Currently I'm using the following schema, in conjunction with https://github.com/jurassix/react-validation-mixin:
validatorTypes: function() {
return {
username: Joi.string().min(4).max(20).required().label(this.refs.username.props.label),
email: Joi.string().email().required().label(this.refs.email.props.label),
password: Joi.string().min(6).max(20).required().label(this.refs.password.props.label),
confirmPassword: Joi.any().valid(Joi.ref('password')).required().label(this.refs.confirmPassword.props.label)
};
},
However this is leading to situations which might be incorrect, but are definitely unwanted:
How can I ensure that confirmPassword matches something like oi.string().min(6).max(20).required() and also matches password?
Can't you do Joi.string().min(6).max(20).required().valid([ref here])?
@AdriVanHoudt No, that gives an _Cannot set valid values when rules are specified_ error unfortunately.
I don't get it, the global validation passes ?
@Marsup The global valiation does not pass, it may have to do with the react-validation-mixin library I am using.
Using React JSX I have the following:
form = (
<form onSubmit={this.handleSubmit}>
<Input type="text" label="Username" bsStyle={this.validationStyle('username')} help={this.getValidationMessages('username')[0]} ref="username" onChange={this.customValidate.bind(null, 'username')} hasFeedback />
<Input type="email" label="Email" bsStyle={this.validationStyle('email')} help={this.getValidationMessages('email')[0]} ref="email" onChange={this.customValidate.bind(null, 'email')} hasFeedback />
<Input type="password" label="Password" bsStyle={this.validationStyle('password')} help={this.getValidationMessages('password')[0]} ref="password" onChange={this.customValidate.bind(null, 'password')} hasFeedback />
<Input type="password" label="Confirm password" bsStyle={this.validationStyle('confirmPassword')} help={this.getValidationMessages('confirmPassword')[0]} ref="confirmPassword" onChange={this.customValidate.bind(null, 'confirmPassword')} hasFeedback />
<br/>
<ButtonInput type="submit" bsStyle={this.validationStyle()} value="Sign Up" />
</form>
);
The message that is incorrect in my case is the one generated by this.getValidationMessages('confirmPassword')[0], which is supplied by the ValidationMixin.
That's business logic, one field is supposed to match the other, if that other field is good, so is this one, if not and they match both are wrong, if it doesn't match then the form is wrong anyway.
No activity.
How do we set the language for Joi.string().required().valid(Joi.ref('password')) password mismatch errors?
Would be so much easier in this case if we could just say .custom(data => {...}) and check that it equals ourselves and return the error message we want
Okay I got it with
const confirmPassword = Joi.string().required().valid(Joi.ref('password')).options({
language: {
any: {
allowOnly: '!!Passwords do not match',
}
}
})
@jedwards1211 the above code for confirming password does not work for me. when I type in confirmPassword filed, it does not send request, nor gives error.
Not sure what to say, I only used Joi briefly, but now I've switched to using flow-runtime for validation
const confirmPassword = Joi.string().required().valid(Joi.ref('password')).options({
language: {
any: {
allowOnly: '!!Passwords do not match',
}
}
})
Works for me BUT the "Passwords do not match" message remains on the page even after the validation passes???
I don't think your page rendering is of any concern to joi, it's your problem.
Wow, nice comment. Super helpful. Bet you're a ball at parties.
So me saying that the culprit is likely not in joi but in your code is not helpful but you barging in an issue that has little to do with your case is helpful?
Wouldn't have been as bad if you weren't so rude about it, especially from someone who is a repository owner/contributor. Prick.
You assumed rudeness and tone while there was not. Sorry you feel that way but I wasn't.
@blu3printchris perhaps a better way to put it would have been "since joi doesn't control the code that displays the validation message on screen, if you're still seeing it after validation passes, it must be because of some code in your app failing to remove the earlier validation error message it got from joi after validation passes." I can understand your anger but it's best saved for times that repo maintainers blow you off with incorrect assumptions, or incorrectly deny that their package is causing a bug, which is not the case here.
@blu3printchris If you were to read the documentation for Joi, you'd understand that it's used for validating objects against a Schema and then providing a response. It's up to you how you handle that response (Server => Client HTTP Request Response).
Plus, if you're going to happily take part in consuming the product of someones labor, at least have the decency to read the documentation.
"message remains on the page even after the validation passes???" - quite clearly a front-end issue.
Most helpful comment
Okay I got it with