yup-phone-lite icon indicating copy to clipboard operation
yup-phone-lite copied to clipboard

setLocale not working

Open Reocin opened this issue 2 years ago • 4 comments
trafficstars

Hey,

First, thanks for your really useful library!

Is it possible for to connect your phone function with the setLocale function from yup ?

With the set Locale function, i can change all the different languages on one place. But your phone function isn't working.

The native yup functions predefine the error message from the locale definition like:

length( length: number | Reference<number>, message: Message<{ length: number }> = locale.length, ) { .... } source: https://github.com/jquense/yup/blob/master/src/string.ts

In your case, it is a hard coded string in English (or you change it explicitly when you call the phone function).

regards Reocin

Reocin avatar Dec 20 '22 13:12 Reocin

I've never looked into the setLocale function in yup before, can you give me an example of how you'd be using it?

csandman avatar Dec 21 '22 00:12 csandman

Actually, i don't know much either. As i discovered this package a couple of days ago.

But you hav to get somehow access to the following file.

import { string as locale } from './locale';

Then you can check if here is a phone property like.

def phone(countryCode, errorMess){
  let message = errorMess ? errorMess :  'default error message'
  if(!errorMess && local.phone){
    message = local.phone
  }

}

So the main questions is how to get access to the local file of Yup.

Reocin avatar Dec 21 '22 07:12 Reocin

https://github.com/jquense/yup/pull/936

Take a look into this issue. The comment mentions how to import the locale, but it probably won't work out of the box in typescript, as the phone is included in the interface. (Probably thy would have to add a \ ts-ignore somewhere) But at least in javascript would work ;-)

I also recommend following this issue, so you stay updated :-)

Reocin avatar Dec 21 '22 07:12 Reocin

we can now use yup.defaultLocale. I'm trying to add support for this as well in yup-password: https://github.com/knicola/yup-password/pull/16/files

knicola avatar Mar 08 '24 00:03 knicola