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

Feature request: Components: reset input(s in form) #305

Closed
jepsar opened this Issue Aug 24, 2016 · 9 comments

Comments

Projects
None yet
2 participants
@jepsar

jepsar commented Aug 24, 2016

I've created the code below to my project. Maybe something to add to Components.

    /**
     * Find the {@link UIForm} for the given event's component and
     * {@link #resetFormInputs(javax.faces.component.UIForm) reset all inputs}.
     *
     * @param event
     */
    public static void resetFormInputsActionListener(ActionEvent event)
    {
        UIForm form = Components.getClosestParent(event.getComponent(), UIForm.class);
        if (form != null) {
            resetFormInputs(form);
        }
    }


    /**
     * Find all {@link UIInput} components in the given form and
     * {@link #resetInput(javax.faces.component.UIInput) reset the input}.
     *
     * @param form
     */
    public static void resetFormInputs(UIForm form)
    {
        for (UIInput input : Components.findComponentsInChildren(form, UIInput.class)) {
            resetInput(input);
        }
    }


    /**
     * Reset the input to value {@code null} and not validated.
     *
     * @param input
     */
    public static void resetInput(UIInput input)
    {
        input.setValue(null);
        input.setSubmittedValue(null);
        input.setLocalValueSet(false);
        input.setValid(true);
    }
@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Aug 24, 2016

Member

Makes sense.

Member

BalusC commented Aug 24, 2016

Makes sense.

@BalusC BalusC closed this in 5e83e2d Aug 24, 2016

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Aug 24, 2016

Member

Added.

Components.resetInputs(event.getComponent());

I only did a tree visit instead of findComponentsInChildren() because the latter doesn't take into account all different states of the very same UIInput component when it's enclosed in a repeating component such as h:dataTable.

Member

BalusC commented Aug 24, 2016

Added.

Components.resetInputs(event.getComponent());

I only did a tree visit instead of findComponentsInChildren() because the latter doesn't take into account all different states of the very same UIInput component when it's enclosed in a repeating component such as h:dataTable.

@jepsar

This comment has been minimized.

Show comment
Hide comment
@jepsar

jepsar Aug 24, 2016

OK, thanks! I also see I've managed to overlook input.resetValue();, thanks for that as well :-)

jepsar commented Aug 24, 2016

OK, thanks! I also see I've managed to overlook input.resetValue();, thanks for that as well :-)

@jepsar

This comment has been minimized.

Show comment
Hide comment
@jepsar

jepsar Aug 24, 2016

What if you have a form component? Then you don't need to / cannot find a form in one of the parents. Maybe only check parents if !(component instanceof UIForm)?

jepsar commented Aug 24, 2016

What if you have a form component? Then you don't need to / cannot find a form in one of the parents. Maybe only check parents if !(component instanceof UIForm)?

BalusC added a commit that referenced this issue Aug 24, 2016

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Aug 24, 2016

Member

Reasonable. I improved it.

Member

BalusC commented Aug 24, 2016

Reasonable. I improved it.

@jepsar

This comment has been minimized.

Show comment
Hide comment
@jepsar

jepsar Aug 24, 2016

Sorry, one last thing. I cannot get the tree visit to compile. I had to change new Callback.WithArgument<UIInput>() to new Callback.WithArgument<UIComponent>(). Is that because I'm using OmniFaces 2.4?

jepsar commented Aug 24, 2016

Sorry, one last thing. I cannot get the tree visit to compile. I had to change new Callback.WithArgument<UIInput>() to new Callback.WithArgument<UIComponent>(). Is that because I'm using OmniFaces 2.4?

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Aug 24, 2016

Member

It won't compile when invoke() method still takes UIComponent.

Member

BalusC commented Aug 24, 2016

It won't compile when invoke() method still takes UIComponent.

@jepsar

This comment has been minimized.

Show comment
Hide comment
@jepsar

jepsar Aug 25, 2016

Clear. Thank you for the prompt responses.

One other thing that came to mind though: for me getting the form and resetting its inputs is what I was going for. In other cases you might just want to reset a panel, subview, etc. Wouldn't it be better to simply reset the child inputs of the given component? If you need to reset a form you can simply get it yourself and use it when calling resetInputs.

jepsar commented Aug 25, 2016

Clear. Thank you for the prompt responses.

One other thing that came to mind though: for me getting the form and resetting its inputs is what I was going for. In other cases you might just want to reset a panel, subview, etc. Wouldn't it be better to simply reset the child inputs of the given component? If you need to reset a form you can simply get it yourself and use it when calling resetInputs.

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Aug 25, 2016

Member

Makes sense. There's now resetForm() and resetInputs().

Member

BalusC commented Aug 25, 2016

Makes sense. There's now resetForm() and resetInputs().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment