Primefaces calendar component manually entering date beyond maxdate validation fails - validation

I'm trying to add a Calendar input component in an application.
The Calendar input component to pick a datetime and the max date restriction works ie doesn't allow to select future date
Greys out the future date - works fine / User cannot select a future date
Problem is the user can enter the future date manually, it is not validating this field.
User is able to enter the future date manually
5/24/2021
6/2/2020
<p:calendar id="userdate" value="#{calendarView.pricedate}" maxdate="#{calendarView.maxDate}" />
calendarView.java
private Date maxDate = new Date();
public Date getMaxDate() {
return maxDate;
}
public void setMaxDate(Date maxDate) {
this.maxDate = new Date();
}
Primefaces Version
<primefaces.version>6.2</primefaces.version>
How to validate this component when user enters the field value manually, I have added a max date validation attribute.

A simple search in a search engine of your liking
One that tracks you
Or even one that does not track you
Would have shown
Calendar: does not validate the input against mindate/maxdate
With a fix version of 7.0
And to prove it I created a real full [mcve]:
XHMTL:
<h:form>
<p:messages id="messages"/>
<p:calendar id="registrationDate" value="#{testView3582.date}" mindate="#{testView3582.minDate}" maxdate="#{testView3582.maxDate}" navigator="true" yearRange="c-2:c" showButtonPanel="true"
mask="true" styleClass="#{testView3582.maxDate}"/>
<p:commandButton value="submit" update="messages" />
</h:form>
Bean:
#Named(value = "testView3582")
#ViewScoped
public class TestView implements Serializable {
Date date;
Date minDate;
Date maxDate;
#PostConstruct
public void init() {
Calendar cal = Calendar.getInstance();
date = cal.getTime();
cal.add(Calendar.DAY_OF_YEAR, -10);
minDate = cal.getTime();
cal.add(Calendar.DAY_OF_YEAR, +20);
maxDate = cal.getTime();
}
//Getters/Setters omitted
}
With a min/max date of 10 days before now and 10 days past and when manually entering 04/20/20 and pressing submit I get
Next time please
Always search the issue list (contains a workaround for previous versions, see also the other answer)
Always create a real minimal reproducible example
Always try with a newer version (preferably latest)

Related

MudDatePicker DefaultValue plus DateChange

I want to use MudDatePicker element in a way where I need to be able to set a default value on load but at the same time define a onChange event for it. I am trying to do this but error says "The component parameter is used two or more times for this component". Is there a way I can do this?
<MudDatePicker #bind-Date="#DefaultValue.Value" Label="Date" DateChanged="OnDateChange"
Required="true" Class="mb-3" />
If you have a two-way binding in Blazor i.e. #bind-Date="date", you can convert it to a one-way binding with a change event which must set the value. The two-way binding is just syntax sugar and will do the same behind the scene.
<MudDatePicker Date="#_date" Label="Date" DateChanged="OnDateChange"
Required="true" Class="mb-3" />
#inject ISnackbar Snackbar
#code {
DateTime? _date = new DateTime(2021, 12, 24);
void OnDateChange(DateTime? newDate)
{
_date=newDate;
// here you can do something when the date changes.
Snackbar.Add($"Date changed to {_date}");
}
}
Here is a snippet which you can play around with: https://try.mudblazor.com/snippet/mYcPFPvLnlyEHeOF
Remove the DateChanged="OnDateChange" and change #bind-Date="DefaultValue.Value" to #bind-Date="DefaultValue". For the property DefaultValue create a getter and setter. Because you have two way binding using #bind-Date="DefaultValue", the setter part gets called every time the value is changed.
I've added some sample code below:
DateTime? _defaultValue = DateTime.Now;
private DateTime? DefaultValue
{
get => _defaultValue;
set
{
_defaultValue = value;
OnDateChange();
}
}

Google AppMaker date validation

How to do some date validation here so that when user enter end date less than start date it will prompt error to user? Is there any built in function made by AppMaker? Thanks for sharing!
If you want to limit the input to a data model, you can use the Advanced section of the data model.
You can also achieve data validation through Event section or Data Validation section. Use the the first one to implement small scripts and the second one to hardcode rules into the app.
// date is not a previous date
var date = new Date();
if (widget.value < date) {
alert('Invalid date');
}
// date is not a future date
var date = new Date();
if (widget.value > date) {
alert('Invalid date');
}
// second date is not higher than first date
if (widget.value > widget.parent.descendants.firstDate.value) {
alert('Invalid date');
}

How to solve Unparseable date:error in spring-boot

I having difficulty trying to solve an error with my date in Spring. I think i have exhausted almost all the solution on stack overflow an i still do not have a solution. I have implement a customDateEditor and i am still get the same error.
I am using the datepicker to select the date on the form.
Error
Failed to convert property value of type java.lang.String to required type java.util.Date for property date; nested exception is
java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "1 March, 2017"
Model
#NotNull(message = "Date field must not be blank.")
#DateTimeFormat(pattern = "dd-MM-yyyy")
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
Controller
#InitBinder
public void allowEmptyDateBinding( WebDataBinder binder )
{
// Custom String Editor. tell spring to set empty values as null instead of empty string.
binder.registerCustomEditor( String.class, new StringTrimmerEditor( true ));
//Custom Date Editor
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MM-yyyy");
simpleDateFormat.setLenient(false);
binder.registerCustomEditor( Date.class, new CustomDateEditor( simpleDateFormat,true));
}
Form
<input th:type="date" class="form-control input-sm datepicker" th:field="*{date}"
placeholder="Date Of Birth"/>
JS Script
$('.datepicker').pickadate({
autoclose: true,
Format: 'dd-MM-yyyy'
});
You are actually using pickadate.js rather than jquery-ui datepicker.
And according to documentation, you are supposed to give 'formatSubmit' property to ensure format of submitted date.
Another issue is your date format for javascript and Java code does not match.
Format 'dd-MM-yyyy' for SimpleDateFormat class will give 01-03-2017,
while for pickatedate.js you need to give 'dd-mm-yyyy' as format.
Therefore, for your "JS script" you should use
$('.datepicker').pickadate({
autoclose: true,
format: 'dd-MM-yyyy',
formatSubmit: 'dd-mm-yyyy'
});

Bean value not updated before ajax event listener is called

I am having some trouble implementing an ajax listener event which detects when a date is changed on a form. I have a datatable and inside one of the columns I have an <ace:dateTimeEntry> which holds a start date field which is stored in the bean. (Note: alliance is the name of the variable used for the datatable).
<ace:column headerText="Start Date" rendered="#{not alliance.deletePending}">
<ace:dateTimeEntry id="startDateField" value="#{alliance.allianceStartDate}" pattern="dd/MMM/yyyy" renderAsPopup="true" effect="fadeIn">
<ace:ajax execute="#this" render="#this" event="dateSelect" listener="#{allianceViewBean.changeAllianceActiveIndicator}"/>
<ace:ajax execute="#this" render="#this" event="dateTextChange" listener="#{allianceViewBean.changeAllianceActiveIndicator}"/>
</ace:dateTimeEntry>
</ace:column>
I am using an tag which calls a listener method in the bean called
#{allianceViewBean.changeAllianceActiveIndicator}
This is the bean value change method:
public void changeAllianceActiveIndicator(AjaxBehaviorEvent event) {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("UTC"));
java.util.Date currentDate = c.getTime();
for (AllianceBean bean : carrierAllianceDetails) {
if ((bean.getAllianceStartDate().compareTo(currentDate) < 0)
&& (bean.getAllianceEndDate().compareTo(currentDate) > 0)) {
bean.setActive(true);
} else {
bean.setActive(false);
}
}
}
However, when I debug this method errors occur. The listener correctly reaches the method but the start date value in the bean is not the updated value and it still refers to the old value before the change. If i enter a new value on the form, the value in the bean is always referring to the previously entered date value. The logic is correct in the method but the values being checked are not.
I am not sure how to ensure that the listener method picks up the latest value from the form.
Thanks
This is due to the lifecycle phase you are in. Check the lifecycle phase in which this event comes in. If it is before the UPDATE_MODEL_VALUES phase (e.g. in the PROCESS_VALIDATIONS phase) then your bean values are simply not updated yet. In that case I would recommend to set the phaseId to INVOKE_APPLICATION on the event, queue it and return the method:
public void changeAllianceActiveIndicator(AjaxBehaviorEvent event) {
if (!event.getPhaseId().equals(PhaseId.INVOKE_APPLICATION) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
return;
}
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("UTC"));
java.util.Date currentDate = c.getTime();
for (AllianceBean bean : carrierAllianceDetails) {
if ((bean.getAllianceStartDate().compareTo(currentDate) < 0)
&& (bean.getAllianceEndDate().compareTo(currentDate) > 0)) {
bean.setActive(true);
} else {
bean.setActive(false);
}
}
}
Apart from that the code of your method is not ideal either. Setting the default timeZone is pretty obsolete - as well the whole Calendar block. You could just do
Date currentDate = new Date();
I would also recommend to convert the date that is set on the bean to the server timeZone's date - otherwise you are comparing apples and oranges.

JSF convertDateTime validation error [duplicate]

This question already has an answer here:
"Validation Error: Value is not valid" error from f:datetimeConverter
(1 answer)
Closed 6 years ago.
I have a problem mentioned in the topic. I have
<h:selectOneMenu class="time" id="time" value="#{auctionBean.expDate}">
<f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
<f:selectItem itemValue="11/11/1111 11:11:11" itemLabel="1 day" />
<f:selectItem itemValue="#{auctionBean.calculateExpDate(4)}" itemLabel="4 days" />
<f:selectItem itemValue="#{auctionBean.calculateExpDate(7)}" itemLabel="7 days" />
<f:selectItem itemValue="#{auctionBean.calculateExpDate(14)}" itemLabel="14 days" />
</h:selectOneMenu>
The problem is i am getting Validation Error: Value is not valid message for all items but first one.
The method:
public String calculateExpDate(int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, days);
Format formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println("data: " + formatter.format(cal.getTime()));
return formatter.format(cal.getTime());
}
It returns String in good format. Output from system.out:
INFO: data: 10/10/2013 20:40:04
Where is the problem? I have no clue at all
A good one!
If what you are getting is a VALIDATION error, not a CONVERSION problem, then the probable scenario is:
the list of available values is created, and the values have precision of 1 second,
user picks one of them,
but on postback the available values get recalculated, and they are all a couple of seconds later than the original ones.
so the value that your user picked is no more available on the list of possible values,
and so a validation error happens (which is what JSF does always when the value chosen is no longer on the list of select items).
you do not get it with the first item, because it's the only one that does not change with time :-).
If you move the backing bean to view scope (or session scope), or cut the precision, it should work. Or better yet - make an enum with values of NOW, IN_2_DAYS, IN_4_DAYS and so on. And calculate the real date after the enum is chosen.
fdreger is right! I marked his post as an answer. Thanks:)
This is my solution if you are lazy (however it might be done better I guess):
JSF:
<h:selectOneMenu class="time" id="time" value="#{auctionBean.choosenOption}">
<f:selectItems value="#{auctionBean.days}" var="days" itemValue="#{days}" itemLabel="#{days.label}"/>
</h:selectOneMenu>
fragment of my auctionBean:
public enum Days {
IN_1_DAY("1 dzieƄ", 1),
IN_4_DAYS("4 dni", 4),
IN_7_DAYS("7 dni", 7),
IN_14_DAYS("14 dni", 14);
private String label;
private int days;
private Days(String label, int days) {
this.label = label;
this.days = days;
}
public String getLabel() {
return label;
}
public Date calculateExpDate() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, this.days);
return cal.getTime();
}
}
private Days choosenOption;
public void setChoosenOption(Days choosenOption) {
this.choosenOption = choosenOption;
expDate = choosenOption.calculateExpDate();
}
public Days getChoosenOption() {
return choosenOption;
}
public Days[] getDays() {
return Days.values();
}
User chooses how many days his auction should be active and i calculate what is an expiration date.
expDate is Date object which i set only once, after choosing the single enum and sumbitting the form. Pretty good solution suggested :)

Resources