Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

Multifield component creation error in AEM 6.5

Avatar

Level 2

Hello everyone!
I am trying to no avail to create a component for my Multifield, I have followed some guides (https://aemhints.com/2020/10/24/coral-nested-multifield-aem65/ ,https://blogs.perficient.com/2018/08/24/using-sling-models-with-nested-composite-mulitifields-in-aem... ) but unfortunately it seems not to take the multifield field.

This is my html:

 

                                    <multi
                                            jcr:primaryType="nt:unstructured"
                                            sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
                                            composite="{Boolean}true"
                                            fieldLabel="String">
                                        <field
                                                jcr:primaryType="nt:unstructured"
                                                sling:resourceType="granite/ui/components/coral/foundation/container"
                                                name="./multifield">
                                            <items jcr:primaryType="nt:unstructured">
                                                <column
                                                        jcr:primaryType="nt:unstructured"
                                                        sling:resourceType="granite/ui/components/coral/foundation/container">
                                                    <items jcr:primaryType="nt:unstructured">
                                                        <text                                                            
                                                   jcr:primaryType="nt:unstructured"
                                                                sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
                                                                fieldLabel="Text"
                                                                name="./text"/>
                                                    </items>
                                                </column>
                                            </items>
                                        </field>
                                    </multi>

 

 

this is the component:

@Model(...)
public class Multi {

   
    @inject
    @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
    private String text;


    public  String getText() {
        return text;
    }

    @inject
    @getter
    @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
    public java.util.List<String> multifield = new ArrayList<>();


}

 

But when I go to debug I see that nothing passes and always returns the null. What am I doing wrong?

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@Jacket97 hi,

 

thanks for conforming, I think you are not supposed to use getter annotation with Arraylist.

Instead replace it with :

@Inject
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
public java.util.List<String> multifield;

 

public List<String> getMultifield() {
if(multifield!=null){
return new ArrayList<String>(multifield);
}else{
return Collections.emptyList();
}
}

View solution in original post

0 Replies

Avatar

Employee Advisor

Hey @Jacket97 ,

 

Had a quick look of your code, I see following issues on my glance :
annotation is @Inject & NOT @inject also @Getter & NOT @getter.

Can you please check if you are importing and using correct annotations :

javax.inject.Inject;
jdk.nashorn.internal.objects.annotations.Getter;


OR is it the case that while pasting the ode you used small case by mistake ?

You can use this video reference as well : https://www.youtube.com/watch?v=TJaAdbkyPJQ


Thanks.

Avatar

Level 2

Thanks for the answer, in the code they are written correctly, it is just a mistake when I copied it in the question box

Avatar

Correct answer by
Employee Advisor

@Jacket97 hi,

 

thanks for conforming, I think you are not supposed to use getter annotation with Arraylist.

Instead replace it with :

@Inject
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
public java.util.List<String> multifield;

 

public List<String> getMultifield() {
if(multifield!=null){
return new ArrayList<String>(multifield);
}else{
return Collections.emptyList();
}
}

Avatar

Community Advisor

Hi @Jacket97 ,

you can use simple in slingmodel

@inject 

public Resource multifield;

 in sightly:

<sly data-sly-use.model="yourmodel">

<data-sly-list.list1=model.multifield.listchildren">

like that you can access item.

 

Kr,

Sanjay