0

I am using JSF 2.0.

I have a h:selectOneMenu on my page which contains a list of values and there is one h:inputText on the same page, whose required should depend on the currently selected value of the h:selectOneMenu. Only a certain set of values should trigger the required check, others not.

This is what I have tried:

<h:inputText ... required="#{(selectedPaymentType.value == 'some value') || (selectedPaymentType.value == 'other value')}" />

In the code above #{selectedPaymentType} is definied in h:selectOneMenu binding.

There are 3 more values like this which should trigger the required attribute to true. This looks kind of clumsy. Are there better ways to do so?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77

3 Answers3

2

If it's possible within your design, you could use an enum as your PaymentType which include some Interface like

public interface RequireSomeMoreStuff {
   public boolean required();
}

public enum PaymentType implements RequireSomeMoreStuff {
   FOO { boolean required() { return true; } },
   BAR { boolean required() { return false; } }
}
stg
  • 2,757
  • 2
  • 28
  • 55
  • Thank you for your quick response. but even if i do that and stuff.. wont that be server side validation? I am looking for client side stuff. – Mukul Goel Nov 21 '12 at 19:11
  • 1
    It is, as your solution has already been. But now the code in your JSF will be shorter and better readable. And you don't habe to change sth in your jsf, if you add or remove some PaymentTypes. I thought that THAT was, where your were looking for. --- if you want some client side validation you have to use some JavaScript or sth like that. But I think it's not a very good idea to do the validation (only) on client side, because you can always bypass client sided validation. – stg Nov 21 '12 at 19:26
  • refer to [here](http://stackoverflow.com/questions/4532955/what-is-the-advantages-of-using-binding-attribute-in-jsf) for what I am trying to achieve. the `selectedPaymentType` is a binding on the `selectOneMenu` and not a bean property. so i suppose this should be client side. Not sure though – Mukul Goel Nov 21 '12 at 19:48
2

Fant has given a hint in the right direction, that you should be using an enum which has a required property, but it seems that you're not entirely sure how to properly implement it. Admittedly, Fant's answer is not elaborate enough. So here's a more elaborate answer.

Basically, you need to replace all dropdown values by an enum which look like this:

public enum PaymentType {
    FOO("Some label for foo", true),
    BAR("Some label for bar", false),
    BAZ("Some label for baz", true);

    private String label;
    private boolean required;

    private PaymentType(String label, boolean required) {
        this.label = label; 
        this.required = required;
    }

    public String getLabel() { 
        return label;
    }

    public boolean isRequired() {
        return required;
    }
}

And use it as follows

<h:selectOneMenu binding="#{selectedPaymentType}" value="#{bean.selectedPaymentType}">
    <f:selectItems value="#{bean.availablePaymentTypes}" var="paymentType"
        itemValue="#{paymentType}" itemLabel="#{paymentType.label}" />
</h:selectOneMenu>
<h:inputText ... required="#{selectedPaymentType.value.required}" />

with

private PaymentType selectedPaymentType; // +getter+setter

public PaymentType[] getAvailablePaymentTypes() { 
    return PaymentType.values();
}

(or if you're using OmniFaces, use <o:importConstants>, then you don't need such a getter for the <f:selectItems>; no, you don't need a converter in any case, JSF/EL has already builtin conversion for enums)

See, the required attribute is now so much more simplified as it's already definied in the model associated with the selected value.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you. Now I understand how to implement it with enums. But I cant do that, its a list. So a function `isRequired()` my best bet in that case? – Mukul Goel Nov 23 '12 at 15:20
  • I don't see how exactly that forms a problem. What do you mean with "its a list"? A list of strings? From where? Database? Isn't your concrete problem rather that you're unsure how to use enums as database values? – BalusC Nov 23 '12 at 15:22
  • Ah sorry it isnt a list. I confused it with the list thats used to populate selectOneMenu. Yup i can change it to enums. I do know basics about ising enums. I think i will be able to.do it now. Thankyou :-) – Mukul Goel Nov 23 '12 at 16:43
-2

I would use a4j in order to render the inputText only if certain values of your selectOneMenu are selected:

<h:selectOneMenu value="#{MyBean.selectedValue}">
    <f:selectItems value="#{MyBean.listOfValues}" />
    <a4j:support event="onchange" reRender="requiredText" ajaxSingle="true"/>
</h:selectOneMenu>

<a4j:outputPanel id="requiredText">
    <h:inputText value="#{MyBean.inputValue}" rendered="#{(MyBean.selectedValue == value_1) || (MyBean.selectedValue == value_2) || ...}" />
</a4j:outputPanel>

In order to avoid a large string on inputText's rendered parameter, I suggest you to create a boolean function which performs these conditions: rendered="#{MyBean.inputTextIsRendered}.

CLIENT-SIDE SOLUTION

There's also a solution based on Validators. Here's my approach:

  • JSF Code

The selectOneMenu code above uses the binding tag to bind the value selected in the selectOneMenu with an attribute that will be retrieved in the validator method, declared for the inputText component.

  • Validator class

Then, you need to create the CheckInputValue validator class.

public class CheckInputValue implements Validator {
    public CheckInputValue() {}

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        //We retrieve thei binded value:
        UIInput confirmComponent = (UIInput) component.getAttributes().get("selectedValue");
        String theValue = confirmComponent.getSubmittedValue();

        //Here you check the retrieved value with the list of values that makes your inputText required.
        //In these cases you will chech the inputText is not empty and, if so, you return a ValidatorException:
        if(isEmpty){
            FacesMessage message = new FacesMessage();
            message.setDetail("inputText cannot be empty");
            message.setSummary("inputText cannot be empty");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }
}
  • faces-config.xml

Finally, you have to declare your validator class in faces-config.xml:

<validator>
    <validator-id>CheckInputValue</validator-id>
    <validator-class>myPackage.myValidations.CheckInputValue</validator-class>
</validator>
jmrodrigg
  • 600
  • 6
  • 24
  • 1
    The Ajax4jsf answer makes no sense. You're not understanding the concrete question. OP's code works, but he finds the EL expression in `required` attribute pretty clumsy. Also, answering a JSF 2.x question with a outdated JSF 1.x specific approach makes no sense (you could just have used JSF 2.x own ``). Also, your validator example is absolutely not a "client side solution" at all. This answer is wrong in all areas. – BalusC Nov 22 '12 at 11:47