Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

primefaces calendar min date 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 :: how to start activity in adapter android 
Java :: check the string has spaces in it in java 
Java :: check type of variable java 
Java :: java check if directory exists 
Java :: how to get path of captured image in android 
Java :: java get distinct values from list 
Java :: list java initialize 
Java :: android studio get string 
Java :: how to set the backtound color in java 
Java :: onclick android 
Java :: shift elements in array java 
Java :: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.kodakjhelum.ItemFragment: make sure class name exists 
Java :: multiply two strings 
Java :: spigot remove every entity 
Java :: what is the difference between sc.nextLine() and sc.next() in java 
Java :: get file path java 
Java :: java do while schleife 
Java :: Private properties in JavaScript ES6 classes 
Java :: java iterate through map 
Java :: swap function java 
Java :: how to create immutable list java 
Java :: java compare strings alphabetically 
Java :: convert int array to Integer list java 
Java :: what is the use of println 
Java :: java resource file 
Java :: Java JPanel set background color 
Java :: how to divide two ints and get a double java 
Java :: declare array with values java 
Java :: java random unique key 
Java :: java template string 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =