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

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

Projects

None yet

2 participants

@jepsar
jepsar commented Aug 24, 2016 edited

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
Member
BalusC commented Aug 24, 2016

Makes sense.

@BalusC BalusC closed this in 5e83e2d Aug 24, 2016
@BalusC
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
jepsar commented Aug 24, 2016

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

@jepsar
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 BalusC added a commit that referenced this issue Aug 24, 2016
@BalusC BalusC Improved #305 4cb7e9a
@BalusC
Member
BalusC commented Aug 24, 2016

Reasonable. I improved it.

@jepsar
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
Member
BalusC commented Aug 24, 2016

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

@jepsar
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
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