1 2 Previous Next 28 Replies Latest reply on Oct 27, 2010 3:48 AM by Aram Nigiyan

    Disabling a group of input fields.

    Marco Huber Newbie

      Hi all,


      I have a simple questiong about JSF, hoping Seam or RichFaces provides a solution. I could not find anyone.


      I have a form with some input fields. Now I want disable or enable all input fields at once depending on security roles, without writing on every components

      disabled="#{action.isEditable}"

      .
      I was looking for a panel or something, which I can set to disabled and all inner components are disabled, too, but couldn't find anyone.


      Does someone of you guys know if there is such a panel (grouping) component out there in the JSF space?


      Any help is welcome.


      Thank you,
      Marco

        • 1. Re: Disabling a group of input fields.
          Christian Bauer Master
          <s:fragment rendered="true|false">...</s:fragment>

          • 2. Re: Disabling a group of input fields.
            Marco Huber Newbie

            Thank you Christian for your reply.
            But that was not the solution I was looking for. If I set rendered to true or false, I can show or hide some components. But I am looking for a solution to set a group of components in an editable or non editable state.


            Any further suggestions?


            Regards
            Marco


            • 3. Re: Disabling a group of input fields.
              Chris Simons Expert

              I don't know of any way that you can disable all inner components in one fell swoop like that; you could look into doing this via jQuery, however. 


              Is there anything preventing you from just setting disabled="#{someValue}" on each component, individually?

              • 4. Re: Disabling a group of input fields.
                Marco Huber Newbie

                Sure, I could set the disabled attribute at all input components. But for maintenance reason it would be better to have one panel where I can set this attribute, like the rendered attribute in s:fragement or other panels.


                Regards
                Marco

                • 5. Re: Disabling a group of input fields.
                  Chris Simons Expert

                  Agreed, it would be neat if there were a formGroup component wherein the children components could be qualified at this level (rendered, disabled, etc.).


                  You could specify a jQuery function on the panel's onLoad or onChange, perhaps?

                  • 6. Re: Disabling a group of input fields.
                    Tim Evers Master

                    I think jQuery is most likely the easiest way to implement this.


                    But, if you don't like the idea of jQuery, then investigate making your own component to do this for you. I'm still thinking about how I would do it, as components that affect their children are fairly difficult to write... but not impossible.


                    My guess is something like this:
                    NOTE: This is not tested or even tried, just a guess from some of the stuff I've done


                    Write a component say...


                    public UIInputRestrictionComponent extends UIFragment {
                    
                        private boolean componentsDisabled = false;
                        
                        public void encodeBegin(FacesContext context) {
                            for (UIComponent component : this.getChildren()) {
                                if (component instanceof UIInputText) { //Do one of these for each type of component
                                    ((UIInputText)component).setDisabled(componentsDisabled);
                                }
                            }
                            super.encodeBegin(context);
                        }
                    }
                    



                    This has not been compiled or tested or even put into an IDE, so there will be bugs/errors. I'm just putting out an idea that might be worth trying if this is the sort of behaviour you need in your app.


                    If anyone knows if this will not work for some reason please say so, cause I really don't know and don't really have the time to go work it out :P


                    Hope it helps.

                    • 7. Re: Disabling a group of input fields.
                      Marco Huber Newbie

                      Hello Tim,


                      thank you for your response. I was thinking about building a custom component, but hoping that there was a solution still outthere.


                      But thank you very much for your prototype code. I will try this out, this is a good starting point.


                      Regards
                      Marco

                      • 8. Re: Disabling a group of input fields.
                        Shervin Asgari Master

                        Here is an example of a menu that is visible depending on security role.
                        Some are shared, some are only for officer, some only for admin.
                        Christian is right that the best thing is to use s fragment



                        <ul
                            xmlns="http://www.w3.org/1999/xhtml"
                            xmlns:ui="http://java.sun.com/jsf/facelets"
                            xmlns:h="http://java.sun.com/jsf/html"
                            xmlns:f="http://java.sun.com/jsf/core"
                            xmlns:s="http://jboss.com/products/seam/taglib">
                            
                            <s:fragment rendered="#{s:hasRole('officer')}">
                             <li class="#{menu_home?'active':''}">
                                <div><s:link propagation="none" view="/minesider.xhtml" title="#{messages['home']}" value="#{messages['home']}" /></div>
                                 </li>
                                 
                                   <li class="#{menu_admin?'active':''}">
                                        <div><s:link propagation="none" view="/admin/officer/officer_admin.xhtml" title="#{messages['settings']}" value="#{messages['settings']}" /></div>
                                   </li>
                              </s:fragment>
                        
                              <li class="#{menu_support?'active':''}">
                                   <div><s:link propagation="none" view="/support.xhtml" title="#{messages['support']}" value="#{messages['support']}" /></div>
                              </li>
                        
                              <s:fragment rendered="#{s:hasRole('orgadmin') or s:hasRole('sysadmin')}">
                                  <li class="#{menu_admin?'active':''}">
                                       <div><s:link propagation="none" view="/admin/admin.xhtml" title="#{messages['admin']}" value="#{messages['admin']}"/></div>
                                  </li>
                            </s:fragment> 
                              
                              <li>
                                  <div><s:link view="/home.xhtml" action="#{identity.logout}" value="#{messages['logout']}" title="#{messages['admin']}" rendered="#{identity.loggedIn}"/></div>
                              </li>
                        </ul>



                        • 9. Re: Disabling a group of input fields.
                          Tim Evers Master

                          Shervin, the original poster is not trying to control the 'visibility' of components. He is trying to control the readonly aspect. So the components will always be visible but, not necessarily editable.


                          Thus, the s:fragment will not actually do what is required.


                          Marco, if you do get the component working could you please post your code in this topic for the rest of us to see. :)

                          • 10. Re: Disabling a group of input fields.
                            Shervin Asgari Master

                            All components have a disabled attribute. It shouldn't be any problem disabling them? They accept EL too, so you can even have some nice condition before disabling them.


                            Or you can do it onLoad with JavaScript (or using JQuery which makes it very easy).
                            I don't get what the big deal is?

                            • 11. Re: Disabling a group of input fields.
                              Shervin Asgari Master
                              <h:inputText value="#{someBean.myText} disabled="#{s:hasRole('admin')}" />





                              • 12. Re: Disabling a group of input fields.
                                Marco Huber Newbie

                                Hello Shervin,


                                I know how to disable one component, as your example show.


                                But I'm looking / working on a solution like:


                                <somecomponent disabled="\#{s:hasRole('admin')}>
                                <h:inputText value="#{someBean.myText}" />
                                <h:inputText value="#{someBean.myText2}" />
                                <h:inputText value="#{someBean.myText3}" />
                                </somecomponent>
                                



                                So that I have the same behaviour for all components inside the somecomponent.


                                Regards,
                                Marco

                                • 13. Re: Disabling a group of input fields.
                                  Shervin Asgari Master

                                  That is not possible. You will have to add the disabled statement on all the components you want disabled,

                                  • 14. Re: Disabling a group of input fields.
                                    Marco Huber Newbie

                                    Hello Shervan,


                                    maybe I'm a dreamer in the world of Java and JSF, but so far I know I can write Java classes with own functionalities, JSF compoenents with own functionalities. So why should this not be possible? As far I have investigated this problem there is no JSF component out there (what my initial posting asked), but as Tim suggested above it is possible to write such a component.


                                    Regards,
                                    Marco


                                    P. S.: Wondering how the rendered attribute at a s:fragement could be impact the rendering behaviour of all inner components? :-)

                                    1 2 Previous Next