表单中的表单:跳过对父表单的验证 [英] form within form: skip validation of parent form

查看:168
本文介绍了表单中的表单:跳过对父表单的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<h:form prependId="false" id="parentForm">
    ...
    <h:form prependId="false" id="commentForm">
        ...
        add comment
    </h:form>
    save
</h:form>

不起作用...

当我只想添加评论时,无需内部表单即可验证父元素.
添加评论"应仅验证评论,而单击保存"则应验证其父项.

Without the inner form the parent's elements get validated when I just want to add a comment.
"add comment" should just validate the comment and when "save" is clicked the parent should be validated.

推荐答案

嵌套表单在HTML中是非法的,在JSF中也是如此,因为它所做的只是生成HTML.您需要将它们并排放置.

Nesting forms is illegal in HTML, so also in JSF since all it does is just generating HTML. You need to put them next to each other.

如果您有多个具有相同形式的按钮,而您希望在某些按钮按下时跳过某些验证,则将immediate="true"添加到有问题的按钮上.这样,所有不具有immediate="true"的输入字段将被跳过.

If you have multiple buttons in the same form of which you'd like to skip certain validation on certain button press, then add immediate="true" to the button in question. This way all input fields which do not have immediate="true" will be skipped.

更新:好的,您希望在一个表单中包含两个物理上分开的表单.如果不能将上帝形式"分成各自负责的多种形式,那么有几种方法可以解决此问题:

Update: OK, you want two physically separate forms inside a single form. If splitting the "God Form" in multiple forms with each its own responsibility is not an option, then there are several ways to go around this:

如果您不使用Ajax,而在输入元素上只有一个required="true",而您实际上想在按下某个按钮时将其设为不需要,那么请执行以下操作:

If you don't use Ajax and you just have a required="true" on an input element which you actually want to make non-required when you press a certain button, then do:

<h:form>
    ...
    <h:commandButton value="Submit form but but do not validate comment" />
    ...
    <h:inputTextarea id="comment" required="#{not empty param[foo.clientId]}" immediate="true" />
    <h:commandButton binding="#{foo}" value="Submit and validate comment" immediate="true" />
</h:form>

如果您实际使用Ajax,则只需在execute属性中指定执行区域.

If you actually use Ajax, then just specify the execute region in execute attribute.

<h:form>
    <h:panelGroup id="other">
        ....
        <h:commandButton value="Submit form but but do not validate comment">
            <f:ajax execute="other" render="other" />
        </h:commandButton>
    </h:panelGroup>
    <h:panelGroup id="comments">
        <h:inputTextarea required="#{not empty param[foo.clientId]}" />
        <h:commandButton value="Submit and validate comment by ajax">
            <f:ajax execute="comments" render="comments" />
        </h:commandButton>
    </h:panelGroup>
</h:form>

这篇关于表单中的表单:跳过对父表单的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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