How would I validate my confirmPassword
field with the tcomb-form-native library?
E-mail and password fields are quite trivial, I wouldn't know how to compare to the existing value of another field in the model though.
Here's my code.
const Email = t.refinement(t.String, (email) => {
const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
return reg.test(email);
});
const Password = t.refinement(t.String, (password) => {
const reg = /^(?=\S+$).{8,}$/;
return reg.test(password);
});
const RegistrationData = t.struct({
email: Email,
password: Password,
confirmPassword: t.String // Need some equality check
});
I've investigated the docs https://github.com/gcanti/tcomb-form-native#disable-a-field-based-on-another-fields-value but I can't make sense of it.
Here is my solution:
First, define a type in class constructor
this.samePassword
that will be use for password checking.Than, use
this.samePassword
type in the form definitionNext, prepare a
onChange
function to handle text changes and save to state.this.validate
is a variable indicating whether the form had been entered or not.Finally, hook
this.state
,this.onChange
... on<Form>
Full code are like this:
Hope this helps!