Portal Showcase

f:validator

The f:validator Facelet tag exposes the functionality of a Validator inside Facelet views. f:validator tags facilitate validation of complex data structures.

General Usage

Validators must be annotated with @FacesValidator (or declared as a validator in a faces-config.xml file) and implement Validator in order to be accessible via the f:validator tag.


Source Code

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html">

	<h:form>
		<label class="control-label">#{i18n['please-enter-your-email']}</label>
		<h:inputText id="email" value="#{inputTextModelBean.text}" label="#{i18n['validate-email-label']}">
			<f:validator validatorId="com.liferay.faces.showcase.validator.EmailValidator" />
		</h:inputText>
		<br />
		<h:message for="email" />
		<hr />
		<h:commandButton value="#{i18n['submit']}">
			<f:ajax	execute="@form" render="@form" />
		</h:commandButton>
		<h:outputText value="#{inputTextModelBean.text}" />
	</h:form>

</ui:composition>
@ManagedBean
@RequestScoped
public class InputTextModelBean {

	private Date date = new GregorianCalendar().getTime();
	private String text;

	public Date getDate() {
		return date;
	}

	public String getText() {
		return text;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setText(String text) {
		this.text = text;
	}
}
@FacesValidator("com.liferay.faces.showcase.validator.EmailValidator")
public class EmailValidator implements Validator {

	private static final String EMAIL_REGEX = ".+[@].+[.].+";

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

		String message = "";

		try {
			Pattern pattern = Pattern.compile(EMAIL_REGEX);
			Matcher matcher = pattern.matcher((String) value);

			if (!matcher.matches()) {
				message = ValidatorHelper.getMessage(context, "email-is-not-valid");

				FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, message, message);
				throw new ValidatorException(facesMessage);
			}
		}
		catch (PatternSyntaxException pse) {
			message = ValidatorHelper.getMessage(context, "unexpected-validation-error-ocurred");

			FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_FATAL, message, message);
			throw new ValidatorException(facesMessage);
		}
	}
}
Liferay Faces Bridge Implementation 4.0.0 + Liferay Faces Portal 3.0.0 + Showcase Common 3.0.0 + Liferay Faces Util 3.0.0 + Mojarra 2.2.13