表单内的表单:跳过父表单的验证

vog*_*err 3 jsf jsf-2

<h:form prependId="false" id="parentForm">
    ...
    <h:form prependId="false" id="commentForm">
        ...
        add comment
    </h:form>
    save
</h:form>
Run Code Online (Sandbox Code Playgroud)

不起作用......

如果没有内部形式,当我只想添加注释时,父元素会被验证.
"添加评论"应该只是验证评论,当点击"保存"时,应该验证父级.

Bal*_*usC 12

嵌套表单在HTML中是非法的,因此在JSF中也是如此,因为它只是生成HTML.你需要把它们放在一起.

如果您在同一表单中有多个按钮,您希望在按某个按钮时跳过某些验证,则添加immediate="true"到相关按钮.这样,将跳过所有具有的输入字段immediate="true".

也可以看看:


更新:好的,您需要在单个表单中包含两个物理上独立的表单.如果将"神形式"分成多种形式并且各自自己的责任不是一种选择,那么有几种方法可以解决这个问题:

如果你不使用Ajax并且你只需要一个required="true"输入元素,当你按下某个按钮时你实际上想要不需要它,那么执行:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)