0

I have this p:inputTextarea:

<p:inputTextarea id="inputTextArea" value="#{myBean.data}" />

Since I moved the submitting form button elsewhere, myBean.data stays null when clicking on the button

So I did this:

<p:inputTextarea id="inputTextArea" value="#{myBean.data}}">
    <f:ajax event="blur" /> 
</p:inputTextarea>

which seems to work !

But I have a feeling that there is a more elegant way of doing this. I just need the value from the inputTextarea written to the bean, so that when I click a button in another form, the data will be available.

Any ideas ?

Tim
  • 3,910
  • 8
  • 45
  • 80
  • 1
    I find this rather elegant. You explicitly state that you want to send the value to the server on the moment you leave the field. So a more elegant way can hardly be found. But if you still only want it on the 'submit', then post the code that does not work and maybe someone can help you correct that. – Kukeltje Jul 26 '16 at 07:01
  • Thanks. I thought maybe there is a dedicated function or keyword to do this. – Tim Jul 26 '16 at 07:40
  • Correct there is a dedicated keyword/function for this. It is `f:ajax` (or `p:ajax`) ;-) – Kukeltje Jul 26 '16 at 07:46
  • This is all kinda wrong. You have to understand the lifecycle of JSF. To make a short answer, on your button you should "process" "@this (the button itself) and inputTextArea". And then all will work alright. As a further performance improvement, you could use on the same button "partialSubmit= true" so that only the said fields (the input and the button) are submitted with the form. So you only need () (no double brackets in the end) and then – Eduard Korenschi Jul 26 '16 at 20:15
  • @EduardKorenschi: it is not wrong, it is one of the common ways of sending data to the server. But like I said in my first comment, it could be that the OP did something wrong when moving the button, but it could also be that due to the fact that having nested forms is not allowed AND you do not want a 'god' form that the input and button are in different forms. Than all your remarks are not valid. So please do not say it is wrong without having seen the [mcve] – Kukeltje Jul 26 '16 at 22:09
  • With "moving the button" I mean that it is not in the same form as my inputTextarea anymore, but in another one. – Tim Jul 27 '16 at 07:44

1 Answers1

1

This is all kinda wrong. You have to understand the lifecycle of JSF. To make a short answer, on your button you should "process" "@this (the button itself) and inputTextArea". And then all will work alright. As a further performance improvement, you could use on the same button "partialSubmit= true" so that only the said fields (the input and the button) are submitted with the form. So you only need

<p:inputTextarea id="inputTextArea" value="#{myBean.data}" />

(no double brackets in the end) and then

<p:commandButton ..... process = "@this inputTextArea" .... partialSubmit = "true" />

But trust me, if you hit this (so simple) problem now ... you need to get a serious JSF book and start loving the Primefaces manual.

  • First see my comment above (you got notification right?), secondly, the OP should first create an [mcve] IF OP wants us to have a look at the problem that arose when moving the button. – Kukeltje Jul 26 '16 at 22:20
  • Besides that, if you have dozens of inputs in a form and you use a `process="@this inputTextArea1 inputTextArea2 ... inputTextArea99`, using your solution does not really make a difference (if you include all id's it is the same as using the default (`@form`). Using `p:ajax` in each input AND it use a `partialSubmit="true"` (the default of the `process` on the `p:ajax` already being `@this) is the more performant for several parts of the different jsf phases then (besides the restore view, but view caching of MyFaces comes into play here). So there is no right or wrong without more info. – Kukeltje Jul 26 '16 at 22:22