2

I have a listener that listens for JSF validation failures, and I need to be able to turn off a specific piece of functionality depending on certain contexts.

In my listener I only have the SystemEvent, so this listener isn't component specific, I was wondering if there was any way to pass around any other information, perhaps something like attributes on the FacesContext?...so that later in the validation listener I could check the context for an attribute that I could set in the JSF.

Ie

<f:someContextParam name="turnOff" value="true"/>

then later

boolean turnOff = (Boolean) FacesContext.getCurrentInstance().someWayToGetAttribute("turnOff");

...seems like a shot in the dark, I'm just trying to see if theres any contextual way to pass back information before I rewrite the architecture.

Jonathan Viccary
  • 722
  • 1
  • 9
  • 27

1 Answers1

0

You could enclose a <f:attribute> tag inside your input component and then retrieve the attribute value inside the validator via FacesContext.getCurrentInstance().getAttributes().get(attrname);

Syntax for the tag should be <f:attribute name="attrname" value="#{ELexpr}">

Here's a semi complete example validator:

public class NameValidator implements Validator 
{
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
        Object value = (String)component.getAttributes().get("FIELD_NAME");
        // validate stuff
    }
}

and corresponding jsf

&lth:inputText id="name" value="#{registrationManager.name}">
    &ltf:validator validatorId="nameValidator" />
    &ltf:attribute name="FIELD_NAME" value="#{registrationManager.numAttempts}"/>
&lt/h:inputText>

Updated Fixed the wrong use of the context to get the attribute to get it from the component instead.

Boris Remus
  • 191
  • 1
  • 4
  • Hi, thanks for your answer. I should have been clearer, I'm not actually within a custom validator; I'm in a listener that is listening for validation fails. Essentially what I was looking for was something similar to your solution but adding the attribute to the context instead of to a specific component/validator. Also, if you add an attribute to a validator, you should actually have to retreive it from the component; ie (String) component.getAttributes().get("FIELD_NAME") – Jonathan Viccary Aug 14 '13 at 13:17
  • Ugh, thanks for catching that. I'll update the answer to at least reflect correct code, even though it might not work for your specific needs. – Boris Remus Aug 14 '13 at 13:57