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 :: assert system.out.println 
Java :: java how to get current time 
Java :: how to iterate over a string in java 
Java :: android studio float to int 
Java :: swing event enter key 
Java :: java read string input 
Java :: Lunar New Year 
Java :: input array through scanner in java 
Java :: short java 
Java :: java sort 2d array 
Java :: java main class 
Java :: java swing timer sleep 
Java :: string to long java 
Java :: find duplicate value in array java 
Java :: java stream filter 
Java :: create jframe java 
Java :: android fragment fullscreen 
Java :: change status bar color android programmatically 
Java :: sprint jpa properties for application.yml 
Java :: spring jpa count all rows 
Java :: java how to print 
Java :: java prime 
Java :: how to print array 
Java :: minecraft chunk size 
Java :: nginx change root directory 
Java :: byte to bufferedimage java 
Java :: android push notification icon 
Java :: java gettime 
Java :: java random uuid 
Java :: java random usage 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =