Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

primefaces datepicker validation

<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

PREVIOUS NEXT
Code Example
Java :: primefaces datepicker custom validation 
Java :: check if string is null or empty java 
Java :: check the string has spaces in it in java 
Java :: java ee check if session exists or is new 
Java :: enter key java 
Java :: topological sort java 
Java :: string starts with alphabet java 
Java :: jackson object to string 
Java :: get date by timezone java 
Java :: get file extension java 
Java :: last day of month from localdate java 
Java :: java create list of strings 
Java :: how to change jframe background color 
Java :: find duplicate value in array using java 
Java :: java program to find perimeter of rectangle 
Java :: java string format 2 decimal places 
Java :: java how to override a private method 
Java :: java.lang.Long cannot be cast to java.lang.Integer 
Java :: install java 8 arch 
Java :: password encryption and decryption in java 
Java :: java foreach list 
Java :: offsetdatetime to date 
Java :: What is null mean in java 
Java :: How to add music to andoid project 
Java :: java reverse loop 
Java :: array to arraylist java 
Java :: how to reverse a string in java 
Java :: java collection to list 
Java :: how to hide menu items in android 
Java :: java jframe example 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =