Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

primefaces datepicker custom 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 :: add value with n variable with Arraylist in java 
Java :: Duplicate class org.intellij.lang.annotations. 
Java :: Convert Octal to Binary in java 
Java :: How to efficiently generate all combinations of well-formed parentheses for n pairs of them, in Java? 
Java :: formartted string java 
Java :: java format money 
Java :: bat artifact 
Java :: android studio get string from strings.xml 
Java :: jackson create object node from string 
Java :: how to import jframe in java 
Java :: compare date with current date in android 
Java :: javafx every second 
Java :: how to add to arras java 
Java :: greatest common divisor java 
Java :: java list to set 
Java :: java yes or no loop 
Java :: into to string in java 
Java :: read double java 
Java :: Triangle Pattern In Java 
Java :: package within another java 
Java :: android.permission.INTERNET 
Java :: string startswith java 
Java :: java check how many digits in a number 
Java :: spring boot sql logging 
Java :: java create file and parent directories 
Java :: java settimeout 
Java :: how to convert bitmap to uri in android 
Java :: selenide wait 
Java :: java random unique number 
Java :: 64 encode java 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =