Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic required fields and asterisks on input labels #205

Closed
dan-osterrath opened this issue Apr 20, 2018 · 5 comments
Closed

dynamic required fields and asterisks on input labels #205

dan-osterrath opened this issue Apr 20, 2018 · 5 comments

Comments

@dan-osterrath
Copy link

I have a schema for an address like this:

export const AddressSchema = object().shape(
    {
        country: string().required(),
        zip: string().required(),
        city: string().required(),
        street: string()
            .when('poBox', {
                is: (e) => !e,
                then: string().required()
            }),
        number: string()
            .when('street'.toString(), {
                is: (e) => e,
                then: string().required()
            }),
        poBox: string()
            .when('street', {
                is: (e) => !e,
                then: string().required()
            }),
    },
    [['street', 'poBox']]
);

The user has to enter street and number or the post office box. Now I want to add asterisks to the labels of the input fields dynamically. That means, when the input fields for street, number and poBox are empty, there are asterisks at all there 3 labels. When the user enters a PO box, the asterisks at street and number disappear.

The question is, how can we 'calculate' whether a specific field is required for the current form values?

@jquense
Copy link
Owner

jquense commented Apr 20, 2018

You can use schema.describe() to introspect a schema object.

@jquense jquense closed this as completed Apr 20, 2018
@latviancoder
Copy link

For future generations:

reach(validationSchema, 'my.deep.object', currentValues).describe();

The resulting object contains tests objects which has the data you need.

Passing currentValues is important if your schema is dynamic (using when/lazy etc).

@dan-osterrath
Copy link
Author

I tried describe() before, but had no success.
The combination of reach and describe did the trick. Thanks.

@joefiorini
Copy link

I was trying to do this exact thing. Doing a lot of searching after it didn't work led me to:

reach() no longer resolves the returned schema meaning it's conditions have not been processed yet; prefer validateAt/castAt where it makes sense

This is a pretty important feature for accessibility. It doesn't seem like validateAt/castAt make sense for this use case. Is there some other way we can do this now that the above change has been made?

@joefiorini
Copy link

I ended up implementing this by doing the reach and then calling resolve({ value: ... }) on the returned schema. Basically doing what reach used to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants