2

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();"
Tacca
  • 149
  • 14

1 Answers1

6

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}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • On both cases, I get a Caused by: javax.el.PropertyNotFoundException: Target Unreachable, 'pessoaFisica' returned null... which is expected since my object (on the example) projectMB.projectParticipant.person is not setted, cause you need to click on the search button and select one for it get setted. – Tacca Nov 17 '15 at 13:27
  • 1
    That's a different problem which is answered here http://stackoverflow.com/questions/30128395/identifying-and-solving-javax-el-propertynotfoundexception-target-unreachable – BalusC Nov 17 '15 at 13:30
  • Works like a charm, I prepared the object projectParticipant.person, with a person.name = "", and now it solved the null problem and the required problem with a hidden field. Thanks. – Tacca Nov 17 '15 at 13:47
  • This "#{facesContext.currentPhaseId.ordinal eq 6}" just solved me another problem... thank you again. – Tacca Nov 24 '15 at 13:24