How to validate if a outputText is filled on JSF?

帅比萌擦擦* 提交于 2020-08-05 07:14:21

问题


Let me explain the scenario, assume that I have an class Project which contains a list of people, and you can search the a person and write his tasks on the project.

Alright, so, I have a button to add people which leads to a modal, with the fields to fill and a search button... This search button, leads to another modal which is a simple person search, you type the name, select one from the list, the search modal closes and the name goes to an outputText on the previous modal.

The thing is, how do I validate this field? I mean, the other fields I can just put required=true, set a requiredMessage and a p:message.

Everything works fine, the values, the buttons, all works, except for the validation of this field. To give a little more clearance, this is what I need:

This is my button, opens the modal, only rendered if a condition is required, all good.

<p:commandButton icon="ui-icon-search" validateClient="true" type="button" onclick="PF('modalSearchPerson').show();" rendered="#{projectMB.projectParticipant.type == 'STUDENT'}"/>

And this is the text which displays the name of the participant, which is updated by the modalSearchPerson.

<h:outputText id="personName" title="Participant" value="#{projectMB.projectParticipant.person.name}"/>

What I wanted, is something as simple as required="true" requiredMessage="You must choose a person.", on the h:outputText field.

I hope I made it clear, Thanks.

edit: It must block the submission, if I only update the :growl with error messages set on the MB, the modal will close, and I dont want that.

The button I use to not close the modal has:

oncomplete="if (args &amp;&amp; !args.validationFailed) PF('modalAddParticipant').hide();"

回答1:


JSF will only validate inputs, not outputs.

Most straightfoward would be setting it as hidden input value too.

<h:outputText value="#{bean.output}" />
<h:inputHidden value="#{bean.output}" required="true" />

Alternatively, use a readonly input field which is with little help of CSS styled like output text (without border and such) and is marked readonly during render response only (otherwise JSF won't process it in other phases).

<h:inputText value="#{bean.output}" styleClass="looks-like-output" required="true" readonly="#{facesContext.currentPhaseId.ordinal eq 6}" />


来源:https://stackoverflow.com/questions/33757441/how-to-validate-if-a-outputtext-is-filled-on-jsf

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