5 Replies Latest reply on Feb 19, 2013 9:47 PM by sudeep.srivastava

    How to set Request Attribute value to a textfield value in a component

    sudeep.srivastava Level 1

      I have a component that containis a textfield widget, i want to set/update/prefill this text field with a value that is set in request attribute, how can i achieve this in CQ

       

      Thanks

        • 1. Re: How to set Request Attribute value to a textfield value in a component
          msulliva Level 2

          By request attribute I'm going to assume you mean an attribute in the query string.  There are probably many ways to do this, but this one appears to work pretty well.

           

          The Dialogs have many event listeners, so whatever textfield you wish to populate, add an "nt:unstructured" node called listeners.  I use the "beforeloadcontent" event to hook into and pass a Javascript Function that I want to run.  Thankfully ExtJS gives us an easy URLDecode function so it's easy to get query string parameters that way.

           

          I also included a check for if there was content present before overriding, else it would always be reset on the dialog load...

           

          In most instances, "field" is the name of the dialog "./field" and also the Query String parameter "field=value", I probably should have used a different name, but whatever.  The field in the function probably shouldn't change, the Function signature called by the event from the widget docs is:  beforeloadcontent : ( CQ.form.CompositeField field, CQ.Ext.data.Record record, String path )

           

          I also found some talk about this and the sample code on a 6D Labs post http://labs.sixdimensions.com/blog/dan-klco/2012-10-01/why-arent-my-public-events-working

           

          JS:

           

          function(field, record, path){

              // Only override if the field is blank 

              if (!record.data.field) {

                  var pageParameters = CQ.Ext.urlDecode(location.search.substring(1));

                  record.data.field = pageParameters.field;

              }

              return true;

          }

           

          Dialog Sample:

           

          {

              "title": "Param Test",

              "xtype": "tabpanel",

              "jcr:primaryType": "cq:Dialog",

              "items": {

                  "jcr:primaryType": "cq:WidgetCollection",

                  "tab1": {

                      "title": "Param Test",

                      "xtype": "panel",

                      "anchor": "100%",

                      "jcr:primaryType": "cq:Widget",

                      "items": {

                          "jcr:primaryType": "cq:WidgetCollection",

                          "field": {

                              "xtype": "textfield",

                              "name": "./field",

                              "jcr:primaryType": "cq:Widget",

                              "listeners": {

                                  "beforeloadcontent": "function(field, record, path){\n    console.log(\"on beforeloadcontent\");\n    console.log(location.search.substring(1));\n    console.log(field);\n    console.log(record);\n    console.log(path);\n\n    if (!record.data.field) {\n\n        var pageParameters = CQ.Ext.urlDecode(location.search.substring(1));\n        console.log(pageParameters);\n        record.data.field = pageParameters.field;\n\n    }\n\n    return true;\n}",

                                  "jcr:primaryType": "nt:unstructured"

                              }

                          }

                      }

                  }

              }

          }

          • 2. Re: How to set Request Attribute value to a textfield value in a component
            sudeep.srivastava Level 1

            Thanks msulliva for you reply,

             

            For your reply, I did what you mentioned and it worked for me,

            • 3. Re: How to set Request Attribute value to a textfield value in a component
              sudeep.srivastava Level 1

              Is there a way to fetch attributes set in request attribute instead of query param.

              • 4. Re: How to set Request Attribute value to a textfield value in a component
                msulliva Level 2

                Can you elaborate on what you mean by Request Attribute?  Where is it's root source?  How are you accessing it?

                • 5. Re: How to set Request Attribute value to a textfield value in a component
                  sudeep.srivastava Level 1

                  Request attribute means in component jsp page i am setting some attributes in request like request.setAttribute("url","abc") and this value i want to display in text field of dialog box.