简体   繁体   English

提交时禁用JSF表单字段验证

[英]Disable JSF form field validation when submit

I have a <h:selectOneMenu> . 我有一个<h:selectOneMenu> Depending on what is selected will be shown one of the many <div> s related to the selection and will be hidden the others. 根据选择的内容,将显示与选择相关的许多<div>之一,而其他隐藏。 Each <div> has some <h:inputText> that write to different @ViewScoped beans. 每个<div>都有一些<h:inputText> ,它们分别写入不同的@ViewScoped bean。 Some of this <div> s even write to the same properties in the beans. 某些<div>甚至会写入Bean中的相同属性。

Ex. 防爆。

<div>
    <h:outputLabel for="list" value="Items"/>
    <div>
        <h:message for="list"/>
        <h:selectOneMenu id="list" value="#{bean.selectedItem}" >
            <f:selectItem itemLabel="Select one"></f:selectItem>
             <f:selectItems value="bean.someItemsList" />
        </h:selectOneMenu>
    </div>
</div>

<div id="item1">
    <!-- some other input fields -->
    <div>
        <h:message for="item1input1"/>
        <h:inputText id="item3input1" value="bean.thisIsTheSameProperty" />
    </div>
</div>
<div id="item2">
    <!-- some other input fields -->
</div>
<div id="item3">
    <!-- some other input fields -->
    <div>
        <h:message for="item3input1"/>
        <h:inputText id="item3input1" value="bean.thisIsTheSameProperty" />
    </div>
</div>

The problem: When I select an item that will display a <div> (ex. <div id="item1"> ) and there is also another hidden <div> (ex. <div id="item3"> ) that writes to the same bean properties (ex. value="bean.thisIsTheSameProperty" ) and this properties are annotated with javax.validation.constraints.@NotNull , even I give a value to this input fields, when I submit the form, I think JSF runs also the hidden <div> (which normally has no input set). 问题:当我选择一个将显示<div> (例如<div id="item1"> )并且还有另一个隐藏的<div> (例如<div id="item3"> )写为到相同的bean属性(例如value="bean.thisIsTheSameProperty" ),并且使用javax.validation.constraints.@NotNull对该属性进行注释,即使我在提交表单时也给此输入字段赋值,我认为JSF还运行隐藏的<div> (通常没有输入集)。

What I see during debugging: When the form will be submited I see the setter of the bean will be called twice. 我在调试期间看到的内容:提交表单时,我看到Bean的setter将被调用两次。 The first time the bean properties will be set with the correct valeus I typed in but the second time the setter will be called with null values. 第一次使用我输入的正确valeus设置bean属性,但是第二次将使用空值调用setter。 So the validation will fail because of the @NotNull. 因此,由于@NotNull,验证将失败。

My assumption is that JSF tries to set the bean values twice, one for the input fields on the shown <div> and the second time for the hidden <div> (because they point to the same bean properties), but for the hidden bean there are not input fields set (they are null). 我的假设是,JSF尝试两次设置bean值,一次用于显示的<div>上的输入字段,第二次设置隐藏的<div> (因为它们指向相同的bean属性),但是设置隐藏的bean没有设置输入字段(它们为null)。

I show/hide the <div> s with jQuery depending on the item selected from the <h:selectOneMenu> . 根据从<h:selectOneMenu>选择的项目,我使用jQuery显示/隐藏<div> <h:selectOneMenu> Ex. 防爆。

$('#item1').show();
$('#item1').hide();
$('#item2').show();
$('#item2').hide();
$('#item3').show();
$('#item3').hide();

Is there a way to say JSF to not consider the hidden <div> s at all? 有没有办法说JSF根本不考虑隐藏的<div>

You seem to be assuming that hidden inputs (hidden via CSS) are not submitted to the server. 您似乎假设隐藏的输入(通过CSS隐藏)未提交到服务器。 This assumption is wrong and it is a plain html thing btw, not JSF related at all. 这个假设是错误的,顺便说一句,这是纯HTML内容,与JSF无关。

See eg Stop an input field in a form from being submitted 请参阅例如停止提交表单中的输入字段

But still, it would allow client-side manipulation via a browser developer tool to 'attack' you application. 但是,它仍然允许通过浏览器开发人员工具的客户端操作来“攻击”您的应用程序。 JSF, contrary to all the hyped javascript UI frameworks is a full grown ' MVC framework ' that has build in protection against client-side manipulation/tampering and CSRF, XSS for which you'd need OWASP related functionality in other framework. 与所有被炒作的javascript UI框架相反,JSF是一个成熟的“ MVC框架 ”,它内置了针对客户端操纵/篡改和CSRF,XSS的保护 ,而您需要在其他框架中使用OWASP相关功能。 (That and other things makes JSF (with PrimeFaces, OmniFaces and DeltaSpike) for me still a great framework to quickly develop business oriented applications.) (这一切使JSF(带有PrimeFaces,OmniFaces和DeltaSpike)对我来说仍然是一个快速开发面向业务的应用程序的好框架。)

You'd better use ajax to conditionally render one div or the other but you cannot 'update' the divs when they are defined like you have them. 最好使用ajax 有条件地渲染一个div或另一个 div ,但是在定义了div就像拥有它们一样时,您不能“更新” div

See also: 也可以看看:

The answer from @Kukeltje and comment from @drkunibar worked for me. @Kukeltje的答案和@drkunibar的评论对我有用。 I modified it a little. 我做了一点修改。

Actual solution: 实际解决方案:

$('#myForm').submit(function() {
            if($('#list').find(':selected').val() === 'itemOption1') {
                $('item3').remove();
            } else if($('#list').find(':selected').val() === 'itemOption3') {
                $('item1').remove();
            }
});

I check to see which option is selected. 我检查以查看选择了哪个选项。 If option1 is selected then I remove the div with id item3 from the DOM, or else if the option3 is selected then I remove the div with id item1 from the DOM. 如果选择了option1, item3 DOM中删除ID为item3div ;否则,如果选择了option3,则从DOM中删除标识为item1div In this way the binding bean.thisIsTheSameProperty is transmitted only one and the values will not be overriden. 通过这种方式,绑定bean.thisIsTheSameProperty仅传输一个,并且不会覆盖这些值。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM