0

I want to be able to read the value that I have set in the dialog and use it in Sightly to control what section of code is shown. When I have tried using the code below I have been receiving this error "Operands are not of the same type : comparison is supported for numbers only". I have tried so many different fixes and have found nothing that works nor any documentation for it. Is context = 'number' not the correct syntax or is there anything else I have to add?

IN THE DIALOG

<number
      jcr:primaryType="nt:unstructured"
      sling:resourceType="granite/ui/components/foundation/form/select"
      fieldLabel="Select Amount of Delivery Options"
      name="./number"
      value = "4" >
      <items jcr:primaryType="nt:unstructured">
          <four
               jcr:primaryType="nt:unstructured"
               text="Four"
               value= "4" />
         <three
               jcr:primaryType="nt:unstructured"
               text="Three"
               value= "3" />
         <two
             jcr:primaryType="nt:unstructured"
             text="Two"
             value= "2" />
         <one
             jcr:primaryType="nt:unstructured"
             text="One"
             value= "1" />
 </items> </number>   

IN THE HTL

<sly data-sly-test="${properties.podnumber @ context = 'number' >= 1}">
mswift
  • 5
  • 4

1 Answers1

2

first, your dialog has name="./number and in your HTL you use properties.podnumber they do not match.

To answer your question: there is no way to do this with sightly alone, the context option is only for rendering (XSS protection) and does not change the value.

Your best bet is to use a sling model, something like

I assume your dialog will have name="podNumber"

@Model(
    adaptables = {Resource.class},
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public interface MyModel {

  @Inject
  int getPodNumber();

}

Sling will then convert that value to an integer you can use in your comparison. so you can add your model with data-sly-use.myModel="package.name.MyModel" then use it:

<sly data-sly-test="${myModel.podNumber >= 1}">

By the way, all the values in your dropdown are larger than or equal to 1.

NOTE: as Florian suggested in the comment below, you should use boolean checks in the model, instead of having to compare values in HTL. for example:

@Model(
    adaptables = {Resource.class},
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MyModel {

  @Inject
  int podNumber;

  boolean isLargerThanOne(){
     return podNumber > 1;
  }
Ahmed Musallam
  • 9,523
  • 4
  • 27
  • 47
  • While I dig the model approach, I would consider to implement boolean properties to inspect the state of the model, keeping the state management out of the HTL script. – Florian Salihovic Aug 27 '18 at 13:25
  • 1
    @FlorianSalihovic I completely agree with you on that one. From a "good practice" standpoint, it makes absolute sense. However, I was spcifically answering the "comparison" question in this case. I'll add a note anyway. – Ahmed Musallam Aug 27 '18 at 14:03
  • `true`, I was too much into how I would solve the problem, not how the answer should be answered. – Florian Salihovic Aug 28 '18 at 09:31