JSF datatable: adding and removing rows clear rows values

空扰寡人 提交于 2019-12-05 09:47:05

I set the immediate attribute to "true" in the buttons in order to not validate the input fields when I add or remove a row.

immediate="true" is the wrong tool for the job. It should be used to prioritize validation, not to enable/disable validation. The difference is rather huge as you encountered yourself.

You want to trigger validation conditionally. In case of e.g. required="true" that'd be as easy as

<h:inputText ... required="#{saveButtonPressed}" />

where #{saveButtonPressed} evaluates true when the save button is pressed. E.g. when its client ID is present in request parameter map.

In case of JSR 303 bean validation, that'd be a matter of

<f:validateBean disabled="#{not saveButtonPressed}">
    <h:inputText ... />
</f:validateBean>

or with OmniFaces <o:validateBean> which allows controlling that on a per-command basis.

<h:commandButton id="add" ...>
    <o:validateBean disabled="true" />
</h:commandButton>

I had exactly the same problem. In short, you can NOT use immediate for action that update data table(UIData) or facelet repeat. Short explanation:submitted values are not kept for re-display if inputs in UIData do not go through validation. Long explanation can be found here: long explanation and a related bug in Mojarra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!