Read only property makes it unable to update managed bean value - jsf

I am submitting a form in prime faces with two text fields which are read only. I am setting value in them using JavaScript. But when I submit the form then in manage bean value of both text filed is null. If I however, remove read only, then it works fine. Please tell me the way out?

Related

jsf get disabled field values in requestscoped bean

Could you please give me some directions on how to pass values from disabled or readonly input fields in xhtml page to requestscoped bean?
I thought that I can bypass jsf checking the field state by disabling
fields in javascipt code on form open and then submit form, but that
did not help too.
I cannot use view scope, because I would have to set then almost
every page in my application in view scope.
It is very inconvenient to use hidden fields for this purpose,
because it would double the number of fields on the page.
Maybe I have missed some clean solution?
Thank you in advance for any help.
Disabling fields using JavaScript didn't work probably because you didn't enable them just before sending a form. Values of disabled fields are not sent (see input documentation).
For example the following code works perfectly well:
<h:form>
<h:inputText id="disabledinput" styleClass="disabled"
value="#{someBean.property}"></h:inputText>
<h:outputScript>
$('.disabled').attr('disabled', 'disabled');
</h:outputScript>
<h:commandButton action="#{someBean.action}"
onclick="$('.disabled').removeAttr('disabled'); return true;"
value="Submit" />
</h:form>
onclick attribute executes JavaScript code that enables input just before sending the form.
If you use AJAX request you have to restore disabled state using oncomplete or similar.
The problem with this solution is that user can manipulate the value. E.g. she/he can use javascript console in a browser to change the input to enabled or use some tool (e.g. curl) to prepare or tamper request. So if the value is sensitive or should never be changed by the user consider storing the value in the session.
IMHO if the value was provided by the user in one of the previous steps then it doesn't matter that much. However, if value is calculated (like total value or something) you should not depend on its value as users could change it. Personally I would prefer to store the value on server side (in session or in flash).

User manipulating the legal values in a JSF selectOneMenu

Technology: Java EE 6, JSF 1.1, ajax4jsf
I'm representing a selectOneMenu to the user (dropdown-list) that is pre-filled with all the legal values the user can submit. Let's say it's datevalues:
01.01.2010
01.01.2011
01.01.2012
The selected value in the dropdown-list is bound to a property on a backing object (form.chosenDate) and the values in the list is retrieved from a list on a backing object (form.selectableDates). form.chosenDate is updated with a4j as the user selects different values.
The question is, is it possible for the user to submit the form with other values than the list was populated with (values in form.selectableDates)? If so is the case, how can this be done? In the dev-tools in Google Chrome, I can manipulate the html and change the values "runtime", but when I submit, the dropdown-list is refreshed with the original values, and I have to submit again. Is this enforced by Google Chrome or JSF?
The question also goes for radio-buttons. Please enlighten me!
Yes, it is possible for user to submit a value that is not in the dropdown. Tamper data plugin for Firefox could do this for example. However, it will fail the JSF validation as the submitted value is checked against the list to see if it is contained in it. The user will get a "form:location: Validation Error: Value is not valid" error message.

JSF-Calling BackingBean method twice maintaining value of inputFileUpload

I am very new to JSF. I have the following requirement:
On click of a commandButton, call a backing bean method to check if there is some data present satisfying the condition.
If yes, confirm from user for overwrite.
If user says OK, call the same method of backing bean with some parameters set to tell the program to overwrite the data.
What I am doing is:
having action of the commandButton as the method name.
in the backing bean method, check if we have come with certain condition, check if the data is already present.
If yes, go back to page and ask for confirmation.
If confirmed, call the click method of the button.
The problem is, when I come back to the page, the inputFileUpload component on the page loses its value.
What can I do to achieve this? Please help.
This is fully by HTML specification and completely outside control of JSF. It's by HTML specification for security reasons not possible to (re)display the value of a HTML input file field with a value coming from the server side. Otherwise a hazard scenario as shown in this answer would be possible.
You need to redesign the form in such way that the input file field is not been updated during confirmation. You can use among others JavaScript/ajax for this: just submit the form by ajax and make sure that the input file field is not been updated on ajax response.

Since upgraded to JSF2.0, Client ID:FormID:ComponentID being preceded to the validation message while using f:validateDoubleRange

I have upgraded from JSF 1.1 to JSF 2.0. Earlier I was using f:validateLongRange or f:validateDoubleRange and the validation worked fine. However, since I have migrated to JSF 2.0 the validation message precedes with client ID which looks something like below :
FormID:ClientID:0:ComponentID in my case, the message appears as
premiumCategory:j_idt368:0:tlNewLOS: Validation Error: Specified attribute
is not between the expected values of 1 and 999,999.
There is a Jira which talks about prefixing ClientID but I dont think its of any use to me.
I learnt from the blog Communication in JSF 2.0 these tag handlers do not work correctly in JSF 2.0 thus we should create custom validators using f:validator
I followed BalusC’s answer from here; The only difference was I did not get my max and min values from the bean, I specified them in the xhtml,it still printed the validation message as stated above.
While debugging I realised that the client ID is generated as FormID:ClientID:0:ComponentID and is stored in the UIComponent instance which gets passed to the MessageFactory.getMessage method.
The same blog which I mentioned before has a topic in it saying "Ajax rendering of content outside form" which I thought would have a solution to my issue, but again no luck.
Now I have my own custom validator class which extends to DoubleRangeValidator and overrides validate method. I get the desired message. However, I am not convinced that writing a custom validator for each f:validatorXXXX component is the solution.
I am still on my learning curve, can anyone explain me why and where MessageFactory.getMessage binds these IDs to to the faces message?
Please advise,
Dakshata Gulkhobare
The label is overrideable by setting the label attribute on the input component of interest.
<h:inputText ... label="Your label" />
This way the validation message will be formatted as follows:
Your label: Validation Error: Specified attribute
is not between the expected values of 1 and 999,999
Otherwise it will indeed default to the component's client ID.
If you happen to use <h:outputLabel> already and would like to use exactly its value, but don't want to duplicate it into the label attribute of every single input component, then you may find OmniFaces <o:outputLabel> helpful.

Selenium input text not working in JSF

I have some page in JSF2.0 where I need to validate fields. This is cross-field validation, so I binded fields to UIComponents. All three fields are <h:inputText> so all are binded to HtmlInputText.
Validation method just reads values from all the HtmlInputTexts. It works pretty well when I run the app and use it by hand. Problem is it doesn't work when selenium inputs values. Part of selenium code is:
selenium.type("identifier=myForm:myTextField", "someText");
So it inputs text into the text field. It works and I can see text is really there. But when I debug validation method, which reads values from UIComponents, the value is null after clicking "Submit". It is not null when entered by hand, only when selenium enters it.
Why is that? Why selenium doesn't work? It does pretty the same I do by hand, yet it works by hand, doesn't work by selenium.
Note that JSF-generated IDs are usually not the ID you have assigned. For example myTextField in a form called myForm will have an ID myForm:myTextField
As JSF takes charge of ID's I prefer setting an 'unique id' in the styleClass attribute which is rendered as class. Then you select the input with that class.
You may have more than the 'unique id' on an element. Then I prefer to search for an element with a class that contains the 'unique id'. It lets you change styling without changing the test.

Resources