tags:

views:

260

answers:

2

Hi,

I'm new to JSF and EL, and was wondering if there is a way to use EL to get the current value of an h:inputText field. Am I doing it wrong, or is it possible at all?

Thanks, -Ben

+1  A: 

I don't really understand what you are looking for...

With this code:

<h:form id="myForm">
    <h:inputText id="myInput" value="#{myBean.myValue}"/>

The value of the input field, at the creation of the HTML page, will be equal to the value of the myValue property of the bean myBean.

If the value is changed by the user, JSF will automatically update the value of myBean.myValue when the form will be submitted.

If you need to get the value of the input on the client side, i.e. using Javascript, you need to do the following code:

<script type="text/javascript">
    function getInputTextValue() {
        var valueOfInput = document.getElementById("myForm:myInput").value;
    }
</script>

Note that you must prefix the ID by the ID of the form that contains the input ("myForm:myInput").

romaintaz
Thanks for the thorough response! Basically, what I need to do is take the input and validate it server-side (have a rather large bean method for this). Then, if it fails validation, I want to give a warning dialogue to the user, preferably with javascript, since that seems to be the easiest way. Now that I know how to get the current value of the field with javascript, I'll see if there's a way to call a bean method and do something with the return value.
Ben
+1  A: 

(Based on your comment) If you want to validate it server-side then you should look at an Ajax library like Richfaces.

You can then easily add an ajax call to your input field

<h:inputText id="myInput" value="#{myBean.myValue}">
   <a4j:support event="onchange" ajaxSingle="true"/>
</h:inputText>

When you change the text the Ajax call will update your model on the Server-side. If you have a validator then you can add it to the inputText tag or use the action attribute on the support tag to call another method.

Damo
Wow! As it turns out, I am using richfaces. I'm going to give this a shot right now...
Ben
This works! Thanks a lot for the help! I do have one nitpick, however. If I make a change to a field that would cause a confirm prompt to come up (this happens when the user clicks a 'save' button, since many fields need to be validated together), and then directly go to the save button, the prompt doesn't come up. However, if I click on some other field (to lose focus, and therefore cause a model update), and then click save, the confirm prompt will show. So the question is, can a blur be used in the save button somehow? (I'm already using the onClick for prompting)
Ben
I'm not really sure what you mean. What causes the confirm prompt to appear?
Damo
To paint a better picture: There are several fields, which are validated together when the user clicks a 'save' button. If they fail validation, a confirm prompt comes up. Since ajax is used to update the fields on the server-side while they are being edited, its possible to edit a field and click the save button (immediately after) without ajax updating the server-side value. However, if one edits a field and 'tabs around' or anything else to trigger the 'onchange' event, everything works normally. So, is there a way to force an ajax update when the save button is clicked, just to be sure?
Ben
Make the save button a a4j:commandButton and set the value of the eventsQueue attribute to be the same as the eventsQueue attribute on your support tags.
Damo