Granite select option isn't working as expected | Community
Skip to main content
joshuau50160339
Level 2
June 24, 2017
Solved

Granite select option isn't working as expected

  • June 24, 2017
  • 6 replies
  • 3766 views

I'm creating a select list using granite/ui/components/foundation/form/select and using the emptyOption and required properties.   The emptyOption appears to work as expected, but the required option isn't enforcing something to be selected?  Also, could emptyText be used in conjunction with emptyOption to provide a default message, i.e. "select something"?

<type

    jcr:primaryType="nt:unstructured"

    sling:resourceType="granite/ui/components/foundation/form/select"

    fieldLabel="Type"

    name="./type"

    emptyOption="{Boolean}true"

    required="{Boolean}true">

    <items jcr:primaryType="nt:unstructured">

        <detail

            jcr:primaryType="nt:unstructured"

            key="test"

            text="Test"

            value="test"/>

        <landing

            jcr:primaryType="nt:unstructured"

            key="test2"

            text="Test"

            value="test2"/>

    </items>

</type>

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Nupur_Jain

Hi Josh,

I believe the functionality you want to achieve is not possible with just field values, you have to write the js validation code for this.

For this modify your select node structure as :

<type

    jcr:primaryType="nt:unstructured"

    sling:resourceType="granite/ui/components/foundation/form/select"

    fieldLabel="Type"

    name="./type"

  required="{Boolean}true">

    <items jcr:primaryType="nt:unstructured">

        <select

            jcr:primaryType="nt:unstructured"

            key="Select"

            disabled="{Boolean}true"

            text="Select"

            value=""/>

        <detail

            jcr:primaryType="nt:unstructured"

            key="test"

            text="Test"

            value="test"/>

        <landing

            jcr:primaryType="nt:unstructured"

            key="test2"

            text="Test"

            value="test2"/>

    </items>

</type>

Create a clientlibray with category as "cq.authoring.dialog" and write the following js code for validation on form submission:

(function (document, $, ns) {

  $(document).on("click", ".cq-dialog-submit", function (e) {

       e.stopPropagation();

       e.preventDefault(); // stop default form submission

      var $myform = $(this).closest("form.foundation-form");

      var type = $myform.find("[name='./type']").val();    // the name of the select field given in node structure , in your case it is './type'

     if(!type){

          ns.ui.helpers.prompt({

               title: Granite.I18n.get("Error"),

               message: "Please Select Type",

               actions: [{

                    text: "CANCEL",

                    className: "coral-Button"

               }]

       });

     }

     else{

       $myform.submit();

     }

  });

})(document, Granite.$, Granite.author);

This way, you will be able to achieve your required functionality.

Hope this helps!

Nupur

6 replies

joshuau50160339
Level 2
June 28, 2017

Any thoughts?

smacdonald2008
Level 10
June 28, 2017
joshuau50160339
Level 2
June 28, 2017

No.  It's just two options.  I'd prefer to have a blank option, and then require at least one option is selected if possible.

Nupur_Jain
Adobe Employee
Nupur_JainAdobe EmployeeAccepted solution
Adobe Employee
June 29, 2017

Hi Josh,

I believe the functionality you want to achieve is not possible with just field values, you have to write the js validation code for this.

For this modify your select node structure as :

<type

    jcr:primaryType="nt:unstructured"

    sling:resourceType="granite/ui/components/foundation/form/select"

    fieldLabel="Type"

    name="./type"

  required="{Boolean}true">

    <items jcr:primaryType="nt:unstructured">

        <select

            jcr:primaryType="nt:unstructured"

            key="Select"

            disabled="{Boolean}true"

            text="Select"

            value=""/>

        <detail

            jcr:primaryType="nt:unstructured"

            key="test"

            text="Test"

            value="test"/>

        <landing

            jcr:primaryType="nt:unstructured"

            key="test2"

            text="Test"

            value="test2"/>

    </items>

</type>

Create a clientlibray with category as "cq.authoring.dialog" and write the following js code for validation on form submission:

(function (document, $, ns) {

  $(document).on("click", ".cq-dialog-submit", function (e) {

       e.stopPropagation();

       e.preventDefault(); // stop default form submission

      var $myform = $(this).closest("form.foundation-form");

      var type = $myform.find("[name='./type']").val();    // the name of the select field given in node structure , in your case it is './type'

     if(!type){

          ns.ui.helpers.prompt({

               title: Granite.I18n.get("Error"),

               message: "Please Select Type",

               actions: [{

                    text: "CANCEL",

                    className: "coral-Button"

               }]

       });

     }

     else{

       $myform.submit();

     }

  });

})(document, Granite.$, Granite.author);

This way, you will be able to achieve your required functionality.

Hope this helps!

Nupur

joshuau50160339
Level 2
June 29, 2017

Thanks Nupur!  Correct me if I'm wrong but based on your answer it seems the SELECT required attribute doesn't do anything unless you write custom validation?  I don't see any indication from the documentation that the steps you outlined are necessary based on reviewing the docs.

Select — Granite UI 1.0 documentation

Nupur_Jain
Adobe Employee
Adobe Employee
June 29, 2017

Josh, I have never seen required attribute working for SELECT.

Thanks,

Nupur