-1

I have a jsf page named as page.xhtml like following:

<p:calendar id="start_date_time_label" widgetVar="myCalendar1"
    value="#{bean.startDateTime}" pattern="dd/MM/yy"
    style="width: 359px; " required="true"
    requiredMessage="Start Date is mandatory" />

Here I have added one javascript function like following to restrict the minimum date value:

<script type="text/javascript">
        jQuery(document).ready(function(){
            myCalendar1.jqEl.datepicker("option", "minDate", -365);//set minDate to 1 year back 
        });     
</script>

Till now the script is working fine.

Now as per requirement I have to change the pattern of p:calender tag to pattern="dd/MM/yy HH:mm:ss". But then the script is not working. Please help me to make it work with the time value.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
krish
  • 1
  • 3
  • Why don't you use a JSF validator? See https://stackoverflow.com/questions/6047866/how-to-perform-validation-in-jsf-how-to-create-a-custom-validator-in-jsf – Jasper de Vries Sep 22 '17 at 14:03
  • 1
    @JasperdeVries: since using the minDate prevents you selecting it in the UI (but I'd prefer the PrimeFaces `minDate` calendar attribute then which OP could have found in the docs, showcase, IDE code completion and google) – Kukeltje Sep 22 '17 at 14:29

1 Answers1

0

Why not use primefaces' mindate attribute?

<p:calendar id="start_date" mindate="#{of:addMonths(now, -12)}" value="#{bean.startDateTime}" pattern="dd/MM/yy HH:mm:ss" />

The 1 year back minimum date is set via a combination of omnifaces' now bean and their el date functions, but you could replace it with a bean method that returns the same thing.

Rung Ip
  • 81
  • 4
  • Does it work when using the mentioned pattern? I got the impression it worked with a pattern without the time but fails (in OP's case) with the time added. Can you explicitly test that? And personnaly, although this `mindate="#{of:addMonths(now, -12)}"` works, I'd put it in the model/backingbean like you suggest. Since it is less a UI/View thing (I think) – Kukeltje Sep 22 '17 at 14:28
  • I've just tested the code and i couldn't see any glaring problems. I agree with using a backing bean as a means of consolidating the business logic nature of the > 1 year in the past requirement. That said, i love using omnifaces and its perfect for this quick demo! :) – Rung Ip Sep 22 '17 at 14:40
  • Ok... And OmniFaces is perfect for waaaay more than just a quick demo (but it indeed saves code here ;-)) – Kukeltje Sep 22 '17 at 14:41