【问题标题】:Validate a number value without custom validator in JSF在 JSF 中验证没有自定义验证器的数字值
【发布时间】:2015-06-12 14:34:41
【问题描述】:

我有一个 JSF 表单。当用户在qty 字段中输入0 并单击Add To Card 按钮时,我希望显示一条消息。

这是 JSF 表单:

<h:form>
      <h:inputText id="qtyField" value="#{booksBean.qty}">
          <!--What kind of validation should i use here?-->
          <f:ajax event="blur" render="qtyMsg"/>
       </h:inputText>

       <h:message id="qtyMsg" for="qtyField"/>

       <h:commandButton value="Add To Card"
                        action="#{booksBean.orderBook()}"
                        rendered="#{booksBean.qty>0}">
           <f:ajax execute="@form" rendered="@form"/>
       </h:commandButton>
</h:form>

我是否需要一个自定义验证器类来简单地将数字值与零进行比较?

像这样:

@FacesValidator("myValidator")
public class MyValidator implements Validator {

@Override
public void validate(FacesContext context, UIComponent component, Object value) {

   if (intValue== 0 || intValue <0) {
      throw new ValidatorException(new FacesMessage(...));
   }
    //...
}

在不创建自定义验证器类的情况下是否有更短的方法?

【问题讨论】:

    标签: validation jsf jsf-2


    【解决方案1】:

    您可以为此使用f:validateLongRange

    <h:inputText value="#{backingBean.input1}">
        <f:validateLongRange minimum="1" />
    </h:inputText>
    

    检查组件的本地值是否在某个范围内 范围。该值必须是任何数字类型或字符串,可以 转换为长。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2013-12-06
      • 2012-04-26
      • 2011-09-04
      • 2016-05-13
      相关资源
      最近更新 更多