简体   繁体   English

Primefaces日历允许无效日期

[英]Primefaces calendar allows invalid date

I am using primefaces calendar but i am allowed to enter invalid date.For eg enter date as 32-06-2012 in the input box for date field and save the record. 我正在使用primefaces日历,但我被允许输入无效日期。例如,在日期字段的输入框中输入日期为32-06-2012并保存记录。 It is saving the date as is saving the date as 02-07-2012. 它将保存日期保存为02-07-2012。 Same behavior can be observed in showcase of primefaces also. 在表面的展示中也可以观察到相同的行为。

Reference : http://www.primefaces.org/showcase/ui/calendarBasic.jsf 参考: http//www.primefaces.org/showcase/ui/calendarBasic.jsf

Here is my code 这是我的代码

<p:calendar id="copyStartDateCalendar" pattern="dd/MM/yyyy"

         mode="popup" showOn="button" size='8' >

                <f:convertDateTime pattern="MM/yyyy" />

</p:calendar>

What should be done as there seems to be some error with the component itself. 应该怎么做,因为组件本身似乎有一些错误。

Thanks & Regards 感谢和问候

Tarun Madaan Tarun Madaan

I had similar problems with the primefaces calendar. 我在primefaces日历上遇到了类似的问题。

For one it accepts dates with two digits though a pattern of pattern="dd.MM.yyyy" is set. 例如,虽然设置了pattern="dd.MM.yyyy"的模式,但它接受两位数的日期。 Like 20.06.12 will be shown in the calendar popup as 20.06.2012 misleading the user to think the date was correctly recognized. 20.06.12将在日历弹出窗口中显示为20.06.2012误导用户认为日期被正确识别。 But the year 12 is actually set. 但实际上是第12年。

Anyways, I ended up setting a <f:validator> inside the <p:calendar> like this: 无论如何,我最终在<p:calendar>设置了一个<f:validator> ,如下所示:

<p:calendar value="#{abschnittDView.bogen.pruefungsDatum}
    mode="popup" locale="de" pattern="dd.MM.yyyy" required="true"
    requiredMessage="Please provide a date."
    converterMessage="Date is invalid.">

    <f:convertDateTime type="date" pattern="dd.MM.yyyy" 
        timeZone="Europe/Berlin" locale="de" />

    <f:validator validatorId="de.common.DateValidator" />

</p:calendar> 

Then doing some validation on the given date: 然后在给定日期进行一些验证:

@FacesValidator(DateValidator.VALIDATOR_ID)
public class DateValidator implements Validator {

    public static final String VALIDATOR_ID = "de.common.DateValidator";

    @Override
    public void validate(FacesContext facesContext, UIComponent component, 
        Object value) throws ValidatorException {

        Date inputDate = (Date) value;
        Calendar cal = Calendar.getInstance();
        cal.setTime(inputDate);
        if (cal.get(Calendar.YEAR) < 1000) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please provide a date with 4 digits for the year", null));
        }
    }

I know this prevents dates below 1000 but in my case it is absolutely clear that the date can not be lower then 2000. 我知道这可以防止1000以下的日期,但在我的情况下,绝对清楚日期​​不能低于2000。

So the suggestion is: Use a Validator to make sure the dates are correct. 所以建议是:使用Validator确保日期正确。 I know it is not the perfect solution but maybe a possible workaround. 我知道这不是完美的解决方案,但可能是一种可能的解决方法。

Otherwise, try to ask for this on the primefaces forum. 否则,尝试在primefaces论坛上提出这个要求。

Try to use readonly="true" at such case you don't need to use any server side validators. 尝试使用readonly="true"在这种情况下,您不需要使用任何服务器端验证器。 This option will allow to the end user only to pick up the date from calendar panel. 此选项仅允许最终用户从日历面板中选择日期。

Your pattern and f:convertDateTime have different patterns? 你的模式和f:convertDateTime有不同的模式?

It's likely not able to figure out what you want in your converter as entering in data as dd/mm/yyyy - then your converter tries to convert that to MM/yyyy. 它可能无法在转换器中找出您想要的数据,因为输入数据为dd / mm / yyyy - 然后您的转换器会尝试将其转换为MM / yyyy。

The issue you describe is because leniency is set to true in the simple date formatter in side primfaces (its a default action). 您描述的问题是因为leniency在side primfaces中的简单日期格式化程序中设置为true(它是默认操作)。 To force is you then use your convertDatetime which should fix it but your patterns may not match it seems. 强制是你然后使用你应该修复它的convertDatetime,但你的模式似乎不匹配。

However, if using a verison of PF < 4 you'll then get java script issues as there is a bug in p:calendar converters returning null objects after validation - you can do some manual fixes for this inside the PF code after rebuilding it. 但是,如果使用PF <4的版本,则会出现java脚本问题,因为p:calendar转换器在验证后返回空对象时会出现错误 - 您可以在重建后在PF代码中对此进行一些手动修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM