【问题标题】:Set start date in Calendar RichFaces在日历 RichFaces 中设置开始日期
【发布时间】:2014-03-29 17:30:06
【问题描述】:

我需要在日历组件 Richfaces 中设置开始日期。我的 ManagedBean 中有一个名为 dateInitial 的变量,它有一个日期(即 2013-07-07 是下限日期),我配置了我的日历组件:

 <rich:calendar id="dateToEvaluate" popup="true" 
    required="true" requiredMessage="Required Field"
    buttonIcon="#{request.contextPath}/resources/images/calendar.gif"
    value="#{valueMB.dateToEvaluate}"
    datePattern="dd/MM/yyyy">
 </rich:calendar>

但我需要 验证 其值大于我的变量 dateInitial,如果不是 .. 则启动带有错误的消息验证器。

感谢您的提前!我很抱歉我的英语不好。

【问题讨论】:

    标签: java jsf calendar richfaces


    【解决方案1】:

    如果您想限制用户不选择早于您的初始日期的日期,您可以使用rich:calendar 的dayDisableFunction 属性。

      <rich:calendar isDayEnabled="disableBeforeInitialDay" …>  
    

    你可以拥有像

    这样的 javascript 函数
    function disableBeforeInitialDay(day)  {
       Date initialDay = new Date(2014,1,27); //equals to 27th Feb 2014  
       return (initialDay.getTime() - day.date.getTime() < 0);  
    }    
    

    上述代码禁用所有早于您的初始日期的日期。

    如果你想在服务器端做,那就写你自己的验证器类

     <rich:calendar .....>  
        <f:validator validatorId="myDateValidator" /> 
     </rich:calendar>  
    
    @FacesValidator("myDateValidator")
    public class MyDateValidator implements Validator {  
          @override
          public void validate(FacesContext context, UIComponent comp, Object value) throws    
           ValidatorException {
    
               Date date = (Date) value;
               Date refDate = new Date(2014,1,27);  
               if (date.before(refDate )) {
                   String message = "Date is earlier than initial date.";
                   throw new ValidatorException(new FacesMessage(message));
               }  
           }  
      }  
    

    如果您使用的是richfaces 4.X,则该属性应为dayDisableFunction 希望这会有所帮助

    【讨论】:

    • 感谢您的回复 Srikanth,我正在选择您的第二个选项,但我有一个问题,如何从 ManagedBean 或其他 componentUI 获取初始日期?因为我测试了您的示例并且效果很好!但我需要从 MB 或 ComponentUI 动态获取日期(在您的示例中为 refDate)。谢谢!
    • 您可以使用
    • @Candres 我觉得更好的方法是向日历添加属性。您可以像这样 作为 Rich:calendar 的子标签。您可以在验证器中以 String param = (String) component.getAttributes().get("initialdate"); 的形式访问它在这种方法中,无需使用 FacesContext。
    • 非常感谢 Srikanth!它就像一个魅力!我添加了 f:attribute,正如您在上面指出的那样,我很容易使用 component.getAttributes().get("initialdate") 获取参数。再次感谢伙计!
    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 2014-12-05
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多