Highlighted

Checking null condition in sightly

Avatar

Avatar

satishn21237075

Avatar

satishn21237075

satishn21237075

02-09-2018

<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

Replies

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

02-09-2018

Hi,

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

Please check Apache Sling :: Sling Models

Highlighted

Avatar

Avatar

satishn21237075

Avatar

satishn21237075

satishn21237075

02-09-2018

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

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

02-09-2018

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>

Highlighted

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

02-09-2018

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

Highlighted

Avatar

Avatar

satishn21237075

Avatar

satishn21237075

satishn21237075

03-09-2018

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. 

Highlighted

Avatar

Avatar

satishn21237075

Avatar

satishn21237075

satishn21237075

03-09-2018

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;

  }

}

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

03-09-2018

Hi,

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

@Inject @Optional

  private String fullName;

Highlighted

Avatar

Avatar

satishn21237075

Avatar

satishn21237075

satishn21237075

03-09-2018

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

Highlighted

Avatar

Avatar

satishn21237075

Avatar

satishn21237075

satishn21237075

03-09-2018

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