On my application I have an xp:inputText control with the disabled property set to true and the required property computed as followed:
<xp:inputText
id="txtSecurityLevel"
styleClass="form-control-static"
value="#{employeeBean.employee.securityLvl}"
disabled="true"
>
<xp:this.validators>
<xp:validateRequired
message="Unsufficient level of security">
</xp:validateRequired>
</xp:this.validators>
<xp:this.required><![CDATA[#{javascript:return submittedBy("btnProceed")}]]></xp:this.required>
</xp:inputText>
The value of the field is pre-populated and I do not want users to alter the value.
When I use the same approach for other inputText controls but with the disabled property set to false the validation is initiated for these fields.
Is there another approach I should use?
To make the field non-editable, use the readonly property.
Internet Explorer used to allow the disabled and readonly property interchangeably. But with IE10 they changed the behaviour so disabled worked as in other browsers, i.e. the value is not passed from the browser to the server. Because it's not passed back from the browser, your validation fails. disabled is for an input that should be unusable for the browser, see https://www.w3schools.com/Tags/att_input_disabled.asp. What you want is for it to be readonly for the user.
Related
Java developer here.
The question may be confusing, so I'll give it some context: I have an inputText field that needs to be disabled if the backing bean's boolean is set to true and vice-versa.
But if the field is not disabled, it NEEDS to be filled. Like so:
<p:inputText tabIndex="3" id="someInput" maxlength="50"
disabled="#{!backingBean.idIsUnknown}"
required="#{backingBean.idIsUnknown}"
value="some text">
</p:inputText>
One works without the other, but not both at the same time. I keep getting that particular error when running the application:
Cannot set content type. Response already committed
Is there any workaround?
If i got it right, then you want to have the Field disabled if the Value of idIsUnknown is true.
So you dont need the ! in front of the Method-Call, since disabled="" sets the field to disabled if the value is true.
Also i would suggest that you dont use ! at all. Try to set the correct boolean Value in your Backing Bean already and just request the value instead of switching it in the XHTML. If necessary, make a second boolean attribute just for the disabled=""
UPDATE
Found an answer from "Makhiel" whitch might help you with your problem:
disabled="true" disables the input (so it's skipped when the form is submitted), if you don't want the user to type in it use readonly="readonly"
Remove required attribute. Move the validation to backend.
Add some style/css to the field to make it look like required.
I would assume that hidden input fields defined as read-only such as
<h:inputHidden value="#{bean.field}" readonly="true" id="field" />
are faster than fields defined with readonly="false" (or with the readonly attribute ommited altogether), but I couldn't find any information on this topic.
Any insights will be greatly appreciated, as I have several pages with large number of h:inputHidden fields that are read-only.
h:inputHidden doesn't have attribute readonly. See this Link
But h:inputText does have that attribute readonly.
I don't think Perfomance(Faster) is the reason for using the readonly="true". Link
Flag indicating that this component will prohibit changes by the user.
Primefaces creator Cagatay Civici comented in forum that:
JSF and PrimeFaces do not process values when inputs are disabled or
readOnly for security purposes. So even a hacker enables the input and
submits the form, JSF-PrimeFaces checks the component. So it is
standard behavior. You need to enable it on server side.
I'm having an issue with some server side validation and I can't tell if this is the expected behavior in XPages or if I'm missing something simple here.
I have a field that has a computed validation formula - basically, it becomes required when a viewScope variable gets set.
My submit button sets the viewScope and does a full refresh.
I would expect the form to be submitted, the validation formula gets evaluated, and if it fails the validation failure would be displayed. But when I click my submit button the form is submitted without error. If I click it again, validation fails as expected.
To illustrate my issue I created a simple example:
<xp:inputText id="inputText1" required="#{javascript:viewScope.validate}">
<xp:this.validators>
<xp:validateRequired message="This field is required."></xp:validateRequired>
</xp:this.validators>
</xp:inputText>
<xp:button value="Submit" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete" immediate="false" save="true">
<xp:this.action><![CDATA[#{javascript:viewScope.validate=true;}]]></xp:this.action>
<xp:this.script><![CDATA[dojo.attr(dojo.byId("#{id:inputText1}"),"aria-required","true")]]></xp:this.script>
</xp:eventHandler>
</xp:button>
<xp:br></xp:br>
<xp:message id="message1" for="inputText1"></xp:message>
As you can see, I also tried adding the aria-required attribute with csjs within the button in the hopes it would initiate validation.
If validation is working as expected are there any suggestions for getting dynamic validation like this to function properly? I know I can do something with querySave on the data source (I excluded a data source from the example for simplicity) but was hoping there is a simpler solution than that.
It looks like the problem is misunderstanding the lifecycle. You're specifying whether the field is required in a viewScope variable. When the page is first loaded, the viewScope is false, so the field is not required. After you submit, the viewScope variable is set, the page rendered again. Only now does the field become required, but it is only required the next time the form is submitted.
If you want the field to be required, you'll need the required property to be evaluated to true and passed to the browser before the form is submitted.
There are a number of alternatives for the functionality you're trying to manage, running SSJS and failing if certain criteria are met.
One is to use the validator property rather than calculating the required property. The validator allows you to run code but you will need to use this.getSubmittedValue() to check the field's value - the validator runs earlier in the lifecycle before the submittedValue is passed to the value property.
The second option is to put the validation in the Submit button. You can use facesContext.addMessage(). If you google xpages facesContext addMessage there are plenty of examples of how to do it. Then do "return false" to abort.
It looks like you might want conditional validation. I wrote a blogpost about this a while back:
Making validation behave properly
I am using JSF 1.1. I have a JSF page with a request scoped bean and a readonly input field.
<h:inputText id="dt" value="#{bean.sdate}" readonly="#{bean.disable}" />
<a onclick="cal('dt');"><img src="fr.gif" border="0"></a>
When I set the input value using JavaScript and click on command button, then the data in input field disappears.
How is this caused and how can I solve it.
That's because the property is set to readonly. If this evaluates true, then JSF won't process the submitted value and hence the model won't be updated. If you want to set it to readonly on rendering the view and have JSF to process the submitted value, then you'd need to make it to evaluate true on render response phase only. You can use FacesContext#getRenderResponse() for this. You'd need to do this in your isDisable() method.
public boolean isDisable() { // TODO: rename to isReadonly().
return FacesContext.getCurrentInstance().getRenderResponse();
}
Note: in JSF2 you could access FacesContext#getCurrentInstance() by #{facesContext} in the view as well, this saves some boilerplate in the model:
<h:inputText ... readonly="#{facesContext.renderResponse}" />
Also note that when you're using JSF2 <f:viewParam>, then this approach won't work on GET requests anymore. See also Make a p:calendar readonly for the explanation and workaround.
I'm trying to render a button that should be initially rendered disabled and at some point enabled by client side actions. I'm using the component from a4j with the following code:
<a4j:commandButton id="myButton" disabled="true" onclick="myFunction()">
The problem is when the disabled attribute is set to true, the events are not attached to the component, resulting in this html code:
<input type="button" onclick="return false" ... />
So when I try to enable via javascript, the button is enabled, but the buttons don't have the event listeners attached.
So far, the only two solutions I can think about are:
Assigning the value of the disabled attribute to a bean property and rerender the button.
Render initially enabled and disabled on the load page through javascript
Both options would work but they are not very clean, I don't want to make a petition to the server every time I enable the button.
That's just how stateful component based MVC frameworks like JSF works. As part of safeguard against tampered/hacked requests, the framework re-evaluates the disabled (and rendered) attribute of an input element whenever it's about to apply the request values. Otherwise endusers would be able to invoke actions or submit values they're not allowed to do by server side restrictions which would potentially put doors wide open to attacks.
The two solutions which you mentioned are perfectly fine. I'd opt for the first one if you don't want to allow the enduser to tamper/hack it. It can easily be done by ajax.