Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Custom validator not being found

849717Mar 23 2011 — edited Apr 26 2011
Hi Forum,

I searched everywhere and couldn't find anybody who has received the following message when attempting to register a custom validator in JSF 2.

+<f:validator> A validator id was not specified. Typically the validator id is set in the constructor ValidateHandler(ValidatorConfig)+

Using Mojarra 2.0.3 under Tomcat 7. Here is my validator:
package com.company.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.apache.log4j.Logger;

import com.company.util.StringUtils;

@FacesValidator("com.company.validators.PhoneValidator")
public class PhoneValidator implements Validator {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		try {
			log.info("Starting PhoneValidator.validate");
			if (!StringUtils.isValidPhoneNumber((String)value)) {
				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Requestor Phone Number is an invalid format.", null);
				throw new ValidatorException(msg);
			}
		} finally {
			log.info("Ending validateRequestorPhone");
		}
	}
}
As you can see I gave it the proper annotation, I assume that by doing that I don't need to register it in faces-config.xml right?

Here is my markup:
<h:outputText value="Requestor Phone: *" />
					<h:inputText required="true" label="requestorPhone"
							value="#{scheduleVisit.visit.requestorPhone}">
						<f:validator validatorId="com.company.validators.PhoneValidator" />
					</h:inputText>
					<h:outputText value="(000-000-0000 or 000-000-0000EXT0000)" />
As you can see in my markup, the validatorId attribute matches exactly the validatorid declared in the validator class. The validator class is on the classpath. Further, Eclipse cannot locate this validator either and provides a warning, I assume because it is stupid and looks for it in the faces-config.xml.

I read a podunk blog article that stated to do it this way. Perhaps this article is full of baloney?

Edited by: 846714 on Mar 23, 2011 6:58 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 24 2011
Added on Mar 23 2011
1 comment
951 views