Dynamic attributes - validation for required

I am trying to use dynamic attributes with jmix 2.1.2. It works well until a certain moment.
What I am trying to do is to use dynamicAttributesPanel so that I can select the category. However my entity (which by the way implements Categorized) has also other fields that I need to fill in.
When I try to save the entity there is no validation for the dynamic attributes.
Below is a screenshot from my application; starting from Description I have my fields attached directly to the entity and not part of the dynamic attributes and no validation error that values are required.
image

In the xml I have the following:

<dynamicAttributesPanel dataContainer="documentDc"
                                cols="2"
                                width="100%"/>
        <formLayout id="form" dataContainer="documentDc">
            <formItem>
                <textField id="nameField" property="name"/>
            </formItem>
            <formItem>
                <textField id="descriptionField" property="description"/>
            </formItem>
            <formItem>
             <textField id="ownerField" property="owner"/>
            </formItem>
            <formItem>
                <textField id="keywordsField" property="keywords"/>
            </formItem>
            <formItem>
                <textArea id="locationField" height="9.5em" property="location"/>
            </formItem>
        </formLayout>

I was wondering how can I trigger the validation of the dynamic attributes ? Because when I press on OK my entity is saved without any values for the dynamic attributes.
Thanks in advance.

Hello,

This behaviour seems like bug, i ve test it, so yes its broken.

1. Wait for fix

I created the Github issue that describes this bug. So, we will fix it soon.

2. If you really need to fix this right now, you can do it…

  • Give DynAttrPanel an id and inject it into controller code:

    @ViewComponent
    private DynamicAttributesPanel dynAttrPanel;
    
  • Add validation subscription and matually find form :laughing: and validate it also manually:

    @Subscribe
      public void onValidation(final ValidationEvent event) {
          JmixFormLayout formLayout = (JmixFormLayout) dynAttrPanel.getChildren()
                  .filter(VerticalLayout.class::isInstance) // finds internal DynAttr content layoyt
                  .findFirst()
                  .orElseThrow()
                  .getChildren()
                  .filter(VerticalLayout.class::isInstance) // finds root layout for CategoryField + Form with fields
                  .findFirst()
                  .orElseThrow()
                  .getChildren()
                  .filter(JmixFormLayout.class::isInstance) // finds form that contains all our fields
                  .findFirst()
                  .orElseThrow();
    
          // manually validates all dynattr fields inside DynAttr FormLayout
          formLayout.getOwnComponents()
                  .forEach(component -> {
                      ValidationErrors validationErrors = viewValidation.validateUiComponent(component);
                      if(!validationErrors.isEmpty()) {
                          event.addErrors(validationErrors);
                      }
                  });
      }
    

But, this is a crutch. If you want to fix your problem right now, you can use it.

Regards, Dmitry

1 Like