2
<h:form id="formEdit">
    <p:selectOneMenu value="#{testView.selection}"
                     required="true">
        <f:selectItem itemValue="noMenu" itemLabel="selectOneMenu not rendered"/>
        <f:selectItem itemValue="haveMenu" itemLabel="selectOneMenu rendered"/>
        <p:ajax update="formEdit"/>
    </p:selectOneMenu>
    <p:panel>
        <p:selectOneMenu id="conditionallyRnedered" value="#{testView.value}"
                         rendered="#{testView.selection eq 'haveMenu'}"
                         required="true">
            <f:selectItem itemValue="#{null}" itemLabel="-" noSelectionOption="true"/>
        </p:selectOneMenu>
    </p:panel>
    <p:messages id="messages"/>
    <p:commandButton value="Submit"/>
</h:form>

Component "conditionallyRnedered" is required, and rendered on page after i select "haveMenu" value in first menu. This component have only empty option and initialy its not rendered on page. If i press Submit button, then response is:

<partial-response><changes>
<update id="javax.faces.ViewState"><![CDATA[stateless]]></update>
</changes></partial-response>

There is no validation error. If i change value of rendered attribute in "conditionallyRnedered" from "#{testView.selection eq 'haveMenu'}" to just "true", then response is:

<partial-response><changes>
<update id="javax.faces.ViewState"><![CDATA[stateless]]></update>
<extension ln="primefaces" type="args">{"validationFailed":true}</extension></changes>
</partial-response>

Validation error returned. The questions is:

  1. Why conditionally rendered component is not validated?
  2. It is possible to make them validated?

UPD Originally in my question is absent Bean source code, in which Bean declared as @ViewScoped. After read @BalusC comment, i try to change scope from @ViewScoped to @SessionScoped, and after that validation is working correctly. Wherein javax.faces.ViewState in response changed from stateless to some view id:

<update id="javax.faces.ViewState">-5902669082498843838:729675320168079573</update>

I still doubt, this is solution or still workaround, because I thought that instance of @ViewScoped bean is exist while we dont left the page. Maybe this behavior is caused by the fact that in the same page present another bean, with @SessionScoped scope.

Community
  • 1
  • 1
bobzer
  • 163
  • 1
  • 11
  • 1
    @MahendranAyyarsamyKandiar No, the result is same. Attribute `update` is working - conditionally rendered component is showed on form after ajax request. The component `p:messages` is useless, because there is no validation errors in response, nothing to show. – bobzer Nov 18 '15 at 17:38
  • i have added answer. please check if it works – Mahendran Ayyarsamy Kandiar Nov 18 '15 at 19:05
  • why was added in your xhtml? – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 12:52
  • i added and even with my "work-around", the validation message is not working. Can you please explain why you said it works after using my solution. I am confused. – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 14:39
  • Did the people who downvoted my answer which has been mentioned as workaround even test with no and have @ViewScoped like balusC mentioned and Removed my "work-around" Still no validation message on clicking "Submit" button. only AFTER adding ajax="false" which was mentioned as "workaround", it is working for me. – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 15:13

2 Answers2

3

It failed for the technical reason explained in this Q&A: Form submit in conditionally rendered component is not processed. In a nutshell, JSF will re-check the rendered attribute during processing the form submit/conversion/validation and skip components which aren't rendered during that moment. The answer is to use a @ViewScoped bean.

That it still failed in spite of that you're actually using a @ViewScoped bean is because you're using a stateless view via <f:view transient="true">, as confirmed by the actual javax.faces.ViewState value and What is the usefulness of statelessness in JSF? In other words, JSF won't save/restore the view, including any view scoped beans. Those beans will technically behave like @RequestScoped beans and thus be recreated on every request, resetting their properties to defaults everytime.

To solve your problem, just turn off stateless view by removing <f:view transient="true"> and keep your bean @ViewScoped.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i added to my page. Even with my "work-around" validation message is not coming in. Then why did the OP comment on my question saying "Thank you, this solution is works fine! – bobzer 10 hours ago". How did it work for him? I am confused why it worked for him. Can you explain, if you have some time please? – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 14:36
  • i tested with no and i have @ViewScoped like you mentioned. Removed my "work-around" Still no validation message on clicking "Submit" button. I do not have context param javax.faces.STATE_SAVING_METHOD in web.xml. – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 15:10
  • I tried import javax.faces.bean.ViewScoped; now without my "work-around". Still doesn't work. I am still learning, – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 17:13
-1

@bobzer,

primefaces commandButton has become ajax="true" by default. So when i set ajax="false", the form is submitted and i can see validation errors

i get validation error coming

My xhtml:

    <h:form id="formEdit">
    <p:selectOneMenu value="#{testView.selection}" required="true">
        <f:selectItem itemValue="noMenu"
            itemLabel="selectOneMenu not rendered" />
        <f:selectItem itemValue="haveMenu" itemLabel="selectOneMenu rendered" />
        <p:ajax update="formEdit" />
    </p:selectOneMenu>
    <p:panel>
        <p:selectOneMenu id="conditionallyRnedered" value="#{testView.value}"
            rendered="#{testView.selection eq 'haveMenu'}" required="true">
            <f:selectItem itemValue="#{null}" itemLabel="-"
                noSelectionOption="true" />
        </p:selectOneMenu>
    </p:panel>
    <p:messages id="messages" />
    <p:commandButton value="Submit" ajax="false"/>
</h:form>

My Bean

        package citi.manageID.framework.admin.roleMgmt;

    import javax.faces.view.ViewScoped;
    import javax.inject.Named;

    @Named
    @ViewScoped
    public class TestView {


        private String selection="";

        private String value="";

        public String getSelection() {
            return selection;
        }

        public void setSelection(String selection) {
            this.selection = selection;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }


    }
  • 4
    This is not a solution. This is a workaround. Pay closer attention to `javax.faces.ViewState` value in OP's ajax response to find the real explanation and answer. – BalusC Nov 19 '15 at 09:38
  • @BalusC i added to my page. Even with my "work-around" validation message is not coming in. Then why did the OP comment on my question saying "Thank you, this solution is works fine! – bobzer 10 hours ago". – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 14:37
  • i tested with no and i have @ViewScoped like you mentioned. Removed my "work-around" Still no validation message on clicking "Submit" button. I do not have context param javax.faces.STATE_SAVING_METHOD in web.xml Did the people who downvoted even test the answer this way? – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 15:12
  • Possibly the difference is in you using the newer CDI ViewScope (javax.faces.view.ViewScoped) and the question is about the original JSF ViewScope (javax.faces.bean.ViewScoped). Impossible to tell really, the question doesn't say if CDI is used or not. – Gimby Nov 19 '15 at 16:55
  • Hmmm....I tried import import javax.faces.bean.ViewScoped; now. Still doesnt work. @BalusC will respond the correct solution i hope. – Mahendran Ayyarsamy Kandiar Nov 19 '15 at 16:59