Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

primefaces custom validate

<p:outputLabel for="piBirthDay" value="Example label" />
<p:datePicker id="piBirthDay"
  validatorMessage="You are not allowed"
  value="#{data.motorbikePlateRequest.bikeOwner.birthday}"
  showTime="false" required="true">
    <f:validator binding="#{greaterOrEqual18YearOldValidator}" />
</p:datePicker>

=============================

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;

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

@ManagedBean(name = "greaterOrEqual18YearOldValidator")
public class GreaterOrEqual18YearOldValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		if (value == null) {
			return;
		}

		if (!isGreaterOrEqual18YearOld((Date) value)) {
			String message = "You are not allowed to make a request";
			FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", message);

			throw new ValidatorException(Arrays.asList(facesMessage));
		}
	}

	private boolean isGreaterOrEqual18YearOld(Date value) {
		final int YEAR_OLD = 18;
		LocalDate idealDateTime = this.convertToLocalDateViaInstant(value).plusYears(YEAR_OLD);
		return idealDateTime.compareTo(LocalDate.now()) < 0; // when the ideal date time less than now
	}

	private LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
		return dateToConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
	}
}
Comment

primefaces custom validate


@FacesValidator("fooValidator")
public class FooValidator implements Validator {

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

        if (valueIsInvalid) {
            throw new ValidatorException(new FacesMessage("Value is invalid!"));
        }
    }

}

Comment

PREVIOUS NEXT
Code Example
Java :: how to check if a char is equal to int java 
Java :: add value with n variable with Arraylist in java 
Java :: java max integer 
Java :: infinity constatn in java 
Java :: swing enter key 
Java :: how to add java_home in mac 
Java :: show input dialog java 
Java :: How to chage font progrmatically 
Java :: java switch 
Java :: input java 
Java :: list in java 
Java :: print a to z in java 
Java :: anagram java program 
Java :: string to enum java 
Java :: random password generator java 
Java :: get time stamp java 
Java :: java get unique elements from array 
Java :: read file in java 
Java :: java 8 group a collections by 2 property 
Java :: border in android 
Java :: How to perform counting sort in Java? 
Java :: how to print multiple lines in java 
Java :: how to change background tint color programmatically android 
Java :: spring boot mongodb update subdocument 
Java :: java how to find the largest number in an arraylist 
Java :: java array to arraylist 
Java :: java int to string 
Java :: android use attribute color programmatically 
Java :: reading csv file in java 
Java :: make frame visible java 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =