仅在按下某些命令按钮后才根据需要验证输入 [英] Validate input as required only if certain command button is pressed

查看:157
本文介绍了仅在按下某些命令按钮后才根据需要验证输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用于JSF验证的特定用例.例如,我有一个inputText字段:

I have specific use case for JSF validation. For example I have an inputText field:

<p:inputText id="input" required="#{myBean.required}" value="#{myBean.value}" maxlength="20" disabled="#{myBean.disabled}">
  <p:ajax event="blur" process="@this" update="name" listener="#{myBean.listener}"/>
</p:inputText>

输入的值是数字(在某些情况下,它也可以是字符串,因为它是复合组件的一部分,但是如果我们假设这是数字,则可以更好地描述问题).此输入是表单的一部分,在表单的末尾有提交"按钮:

Value of input is number (in some cases it can also be a string, because this is part of composite component, but problem is better described if we assume this is a number). This input is part of the form, at the end of form I have submit button:

<p:commandButton value="Save" actionListener="#{myBean.save}"/>

我的要求是什么

  1. 当按下提交"按钮时,应处理所有验证,这是可以的,这很好.
  2. 如果字段不为空,则在输入字段上引发模糊事件时,应处理数字验证,这也可以.最后,我用一些值更新ID为name的字段.
  3. 现在我有一个问题.我的第三个请求是当输入为空时,不应处理对输入的验证.在特殊情况下,我将清除ID为name的字段.当我删除输入中已经输入的文本,将焦点从组件中删除时(例如按TAB),在这种情况下也应处理AJAX请求,并且名称输入也将被清除.
  1. When submit button is pressed all validation should be processed and this is OK, this works fine.
  2. When blur event is fired on input field if field is not empty a number validation should be processed, and this is also OK. At the end I update field with id name with some value.
  3. Now I have a problem. My third request is when input is empty validation on input should not be processed. This is special case in which I will clear field with id name. This is also case when i remove text which is already entered in input, remove focus from component (press TAB for example) and in that case AJAX request should also be processed and name input will also be cleared.

如果此输入字段为空,并且仅针对此ajax事件,如何禁用此输入字段的验证?

How I can disable validation of this input field in case when it is empty, and just for this ajax event?

推荐答案

让输入的required属性检查是否按下了保存按钮(可以通过在请求参数图中是否存在其客户端ID来标识该按钮) ).

Let the input's required attribute check if the save button is pressed or not (which can be identified by the presence of its client ID in the request parameter map).

<h:form>
    <p:inputText ... required="#{not empty param[save.clientId] and myBean.required}" />

    <p:commandButton binding="#{save}" ... />
</h:form>

(注意:不要将其绑定到bean属性!代码保持原样)

(note: do not bind it to a bean property! the code is as-is)

这样,只有在实际按下保存按钮时,它才会评估true.

This way it would only evaluate true when the save button is actually pressed.

或者,如果您在使用binding时遇到问题和/或在对按钮的客户端ID进行硬编码时没有问题:

Or, if you have problems with binding and/or don't have a problem hardcoding the button's client ID:

<h:form id="formId">
    <p:inputText ... required="#{not empty param['formId:buttonId'] and myBean.required}" />

    <p:commandButton id="buttonId" ... />
</h:form>

这篇关于仅在按下某些命令按钮后才根据需要验证输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆