10

Readonly inputText doesn't validate if required="true" is set.

<h:panelGrid columns="3" id="townShipPanelGroup">
    <p:inputText value="#{AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township == null ? '' : AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township.name}" 
        style="width:250px;margin-left:-4px;" id="townShip" readonly="true">
            <f:validateLength maximum="36"/>
    </p:inputText>
    <p:commandLink immediate="true" oncomplete="selectTownShipDialog.show()" id="selectTownShipDialogLink" action="#{AddNewLifeProposalActionBean.loadTownshipList()}">  
    <h:graphicImage url="/images/search.png" style="height:30px;width:30px"/>
    </p:commandLink>
</h:panelGrid>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pyae Phyo Aung
  • 161
  • 1
  • 1
  • 10

1 Answers1

17

This is behaving as expected. For all intents and purposes, JSF doesn't evaluate a value that is designated as readonly="true" throughout the request lifecycle. The recommended way to get this done is to make the value readonly during the RENDER_RESPONSE phase, where the view is being presented to the user. During any other phase, you want the JSF runtime to interpret the input field as writeable. For that purpose, you can use:

<p:inputText value="#{AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township == null ? '' : AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township.name}" 
        style="width:250px;margin-left:-4px;" id="townShip" readonly="#{facesContext.renderResponse}">
            <f:validateLength maximum="36"/>
    </p:inputText>

This way, the readonly property is only true when the user is viewing the page. For all other phases, the JSF runtime will see the field as writeable and as a result, validation will be carried out on the field

References:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • 3
    Since JSF2, `#{facesContext.renderResponse}` is not anymore guaranteed to work in all cases. http://stackoverflow.com/questions/17639415/make-pcalendar-readonly/17639554#17639554 – BalusC Sep 09 '13 at 10:55
  • Yes @Kukeltje. If a malicious user edits the client-side markup to remove the readonly attribute, they could possibly supply a value. OP should envisage that as part of the "validation" that's referenced in the question – kolossus Aug 11 '16 at 13:12
  • 1
    readonly="#{facesContext.renderResponse}" onkeypress="return false;" is working fine – Hazim Mar 28 '19 at 11:32