简体   繁体   中英

How can I use both REQUIRED and DISABLED attributes on a PrimeFaces component?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM