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
constpasswordValidation=(schema)=>schema.min(10,'Password must be at least 10 characters').matches(/(?=.*[A-Z])/,'Password must have at least one uppercase letter',).matches(/(?=.*[a-z])/,'Password must have at least one lowercase letter',).matches(/(?=.*[0-9])/,'Password must have at least one number').matches(/(?=.*[!@#$%^&*])/,'Password must have at least one special character',);constschema=yup.object({firstName: yup.string().required('First name is required'),lastName: yup.string().required('Last name is required'),email: yup.string().email('Invalid email').required(),currentPassword: yup.string().when(['newPassword','email','$originalEmail'],{is: (newPassword,email,originalEmail)=>{console.log(newPassword,email,originalEmail);return!!newPassword||email!==originalEmail;},then: (schema)=>{console.log('running then');returnschema.required('Current password is required');},otherwise: (schema)=>{console.log('running otherwise: ');returnschema.notRequired();},}),newPassword: passwordValidation(yup.string().notRequired()),newPasswordCopy: yup.string().when('newPassword',{is: (newPassword)=>!!newPassword,then: (schema)=>schema.required('New password confirmation is required').oneOf([yup.ref('newPassword')],'Passwords must match'),otherwise: (schema)=>schema.notRequired(),}),});
...
const{
control,
handleSubmit,formState: { errors },}=useForm({mode: 'all',criteriaMode: 'all',reValidateMode: 'onChange',resolver: async(data,context,options)=>{varresolverResults=awaityupResolver(schema)(data,context,options,);console.log('validation result',resolverResults);returnresolverResults;},defaultValues: {firstName: auth.user.name,lastName: auth.user.lastname,email: auth.user.email,newPassword: null,currentPassword: null,newPasswordCopy: null,},context: {originalEmail: '[email protected]'},});console.log(JSON.stringify(errors,null,2));
it outputs:
LOG s [email protected][email protected]
LOG running then
LOG validation result {"errors": {"currentPassword": {"message": "Current password is required", "ref": "currentPassword", "type": "required", "types": [Object]}, "newPassword": {"message": "Password must be at least 10 characters", "ref": "newPassword", "type": "min", "types": [Object]}, "newPasswordCopy": {"message": "New password confirmation is required", "ref": "newPasswordCopy", "type": "nullable", "types": [Object]}}, "values": {}}
LOG {
"newPassword": {
"message": "Password must be at least 10 characters",
"type": "min",
"types": {
"min": "Password must be at least 10 characters",
"matches": [
"Password must have at least one uppercase letter",
"Password must have at least one number",
"Password must have at least one special character"
]
},
"ref": "newPassword"
}
}
Expected behavior
The Errors object should contain errors for currentPassword and newPasswordCopy
Hello @Cbridger-mywellabee, did you manage to resolve the issue you faced? My team and I are facing the same issue here and we could not find a solution yet.
Thanks @Cbridger-mywellabee . I thought about this last night, but doing this honestly feels like a workaround, but it is the only resolution while working with reack-hook-form as they validate one field at a time.
Describe the bug
I have this react native code:
it outputs:
Expected behavior
The Errors object should contain errors for currentPassword and newPasswordCopy
Snack reproducing error
https://snack.expo.dev/@cdbridger-mywellabee/yupresolvererror
Versions:
"expo": "~51.0.12",
"react": "18.2.0",
"react-hook-form": "^7.52.0",
"react-native": "0.74.2",
"yup": "^1.4.0"
"@hookform/resolvers": "^3.6.0",
The text was updated successfully, but these errors were encountered: