1

I'm trying to display just one for two Fields that are required. At the moment there are two error messages if both fields are empty. I want to achieve that there is just one message if both or just one field is empty.

The code looks like this:

<x:inputText
    value="#{bean.proxyUrl}"
    id="idProxyUrl"
    required="true"
    />
<x:outputText value=":" />
<x:inputText
    value="#{bean.proxyPort}"
    id="idProxyPort"
    required="true"
    />
<x:message for="idProxyUrl" errorClass="errorMessage" style="margin-left: 10px;" />
<x:message for="idProxyPort" errorClass="errorMessage" style="margin-left: 10px;" />

What can I do about it, that I only get one message no matter if one or both of the fields are empty.

Przemek
  • 623
  • 2
  • 11
  • 24
  • The `x:` prefix is not recognizeable as any of the known JSF component libraries. May I assume it to be the standard JSF HTML component set with the URI `http://java.sun.com/jsf/html`? (if so, why are you changing the standard `h:` prefix everyone in the world is using?) – BalusC Sep 28 '12 at 13:48
  • 1
    This might help you a bit... http://stackoverflow.com/q/10007438/617373 – Daniel Sep 28 '12 at 15:17
  • The `x:` is pointing to `http://myfaces.apache.org/tomahawk` – Przemek Oct 01 '12 at 08:51

1 Answers1

2

You can specify a special validator for the second component that checks the SubmittedValue of the first one. I did something similar for a PasswordValidator that checked the corresponding Confirm Password field.

@FacesValidator("passwordValidator")
public class PasswordValidator implements Validator {    

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


        String password = (String) value;


        UIInput confirmComponent = (UIInput) component.getAttributes().get("confirm");
        String confirm = (String) confirmComponent.getSubmittedValue();

        if (password == null || password.isEmpty() || confirm == null || confirm.isEmpty()) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please confirm password", null);
            throw new ValidatorException(msg);
        }


        if (!password.equals(confirm)) {
            confirmComponent.setValid(false); 
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The entered passwords do not match", null);
            throw new ValidatorException(msg);
        }


    }

The reason why you must check for the Submitted Value of the other component is because the validator is called during the Process Validations phase of the lifecycle. None of the Submitted Values are applied until after this phase has been completed and every submitted value has passed validation.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • Thank you. With some little changes, because that fields do not have to be equal, that is what helped. I'm pretty new to JSF and need to learn very much! – Przemek Oct 01 '12 at 08:55