Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Checking null condition in sightly

Avatar

Level 1

<div data-sly-use.dailogvalue="org.dell.emc.aem.dellemcsupport.core.models.DropDownModel"/>

<div data-sly-test="${dailogvalue.fullName}"> ${dailogvalue.fullName}</div>

I am getting error :

Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class org.dell.emc.aem.dellemcsupport.core.models.DropDownModel

Could not inject private java.lang.String org.dell.emc.aem.dellemcsupport.core.models.DropDownModel.fullName caused by No injector returned a non-null value!

  at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:534)

  at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:306)

  at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:200)

  at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:132)

  at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:84)

As i am not providing any value to fullName parameter but data-sly-test command is not handling the null value. Please suggest what i am doing wrong.

AEM 6.2

Thanks

Satish

9 Replies

Avatar

Community Advisor

Hi,

you need to add @Optional in your case if property values are not mandatory

Please check Apache Sling :: Sling Models



Arun Patidar

Avatar

Level 1

By making @Optional to property means if its not required. If some property is giving null value then how to check in sightly.

I think this is alternative solution but not to correct to check the null value.

Please correct me if i am wrong

Thanks,

Satish

Avatar

Community Advisor

Hi,

The issue is with Sling Model because of missing values properties, you need to make @Optional so that Model will returns null and sightly you can check with data-sly-test like <div data-sly-test="${dailogvalue.fullName}"> ${dailogvalue.fullName}</div>



Arun Patidar

Avatar

Employee

This to me is a sling-model issue/feature, can you share your code of this sling-model?

Avatar

Level 1

package org.dell.emc.aem.dellemcsupport.core.models;

import java.util.Date;

import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.Optional;

@Model(adaptables=Resource.class)

public class DropDownModel {

  private String result;

  @Inject

  private String fullName;

  @Inject

  private String country;

  public String getDate() {

  return date;

  }

  @Inject

  private String date;

  public String getResult() {

  result=fullName+"satish";

  return result;

  }

  public String getCountry() {

  return country;

  }

  public String getFullName() {

  return fullName;

  }

}

Avatar

Community Advisor

Hi,

The issue is with Model class, add @Optional with @Inject e.g.

@Inject @Optional

  private String fullName;



Arun Patidar

Avatar

Level 1

Is there no option with out giving @optional annotation.?

Avatar

Level 1

I find the solution without using @Optional annotation.

<sly data-sly-test.div="${properties.fullName && properties.country && properties.date }">

<pre data-sly-use.dv="org.dell.emc.aem.dellemcsupport.core.models.DropDownModel">

${dv.fullName}

${dv.date}

${dv.country}

</pre>

</sly>

<sly data-sly-test ="${!div}">

<pre>

enter value through dialog

</pre>

</sly>

Now when there no input from dialog then  "enter value through dialog" this will print in the page.

Thanks,

Satish

Avatar

Level 1

Sorry, I have dialog and there is property full name . When i drag and drop component then this error comes.

But after providing value to property full name it get reflected to page.

While drag and drop time how to check null value condition in sightly.