2

I'm trying to do something similar to this:

<h:form id="form">
    <p:inputText id="input1" value="#{mb.input}" required="true" /><br />
    <h:panelGroup id="panelGroup"><br />
        <p:inputText id="input2" required="true" /><br />
        <p:commandButton id="doSomething" value="something" action="#{mb.doSomething}" />
    </h:panelGroup><br />
    <p:commandButton id="save" value="Save" action="#{mb.save}" /><br />
</h:form>

Here is my problem: when I hit the save button I want the whole form to be validated where required="true" (for both input1 and input2, which works fine).

BUT, when I hit the doSomething button I would like it to check only if input2 is filled, ignoring the condition of input1 (in other words: it shouldn't work if input2 is empty but should work even if input1 is). Is there a way to do it? (And I can't use Managed Beans for that!)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Denis Klein
  • 101
  • 1
  • 11

2 Answers2

2

use the process attribute of the <p:commandButton> like this

<p:commandButton id="doSomething" value="something" action="#{mb.doSomething}"  process="input2"/>

that way it will only validate input2 upon click. as per Primefaces Documentation:

process | null | String | Component(s) to process partially instead of whole view.

Fritz
  • 1,144
  • 1
  • 13
  • 21
  • 1
    Yeah! It worked! I would just like to add to your answer that it makes only the input2 component work, not the button itself (which is a problem because it is supposed to do some other stuff, at least in my case). The final process tag had to be: `process="input2, doSomething"` – Denis Klein Sep 24 '15 at 00:44
  • 1
    Denis Klein, you said me in a comment that you needed to send `input1` in the submit when you clicked the `doSomething` button. The accepted answer then would not satisfy your requirements. Could you clarify it? – Javier Haro Sep 24 '15 at 08:03
  • Man, I'm terribly sorry, I guess I just got confused when I answered that... that's not what I meant: `doSomething` needs to check `input2`, not `input1`. – Denis Klein Sep 24 '15 at 12:47
0

Only put immediate="true" attribute to both components inside the panel. Or you also can use Ajax for the button inside the panel for sending only the panel content.

Note 1: The input inside the panel is claiming for a value attribute.

Note 2: Also include an update attribute to both buttons. ie:

    update="@form"

Note 3: Also you can include the <p:messages/> element inside the form.

Javier Haro
  • 1,255
  • 9
  • 14
  • Will this solution validate `input2` field when `Save` button was pressed? – Geinmachi Sep 23 '15 at 21:52
  • Yes it will. It will validate both inputs. – Javier Haro Sep 23 '15 at 22:04
  • Denis Klein, do you need `input1` value going in the request when clicking `doSomething` button? If yes then Ajax solution would not help. Anyway, if the validation requisites turn complex you would better manage the validation inside the backing bean method, and generate the corresponding validation messages by yourself. – Javier Haro Sep 23 '15 at 22:46
  • @BalusC plaese, could you tell me something about this behaviour: When I put the immediate attribute in both components inside the panel the backing bean method executes and then later the validation is checked and if it fails the message is showed. So the "immedite attributes" solution would not be valid if you need to execute a backing bean method when pressing the "doSomething" button. – Javier Haro Sep 23 '15 at 22:54
  • Your first idea didn't work here: it's not considering the requierements correctly... and for the Ajax for the button inside the panel idea, I didn't understand what exactly you mean: would you mind showing me the code for that? – Denis Klein Sep 24 '15 at 00:10
  • Yes, I need it to go in the request when clicking doSomething. This is not my actual code (I can't put it here) but the idea is that it will be a composite component that will be inside other forms with other required input fields. What I really want is my custom component to have a button which check the requirements only for its own inputs. – Denis Klein Sep 24 '15 at 00:15