private function buildFieldSet($widgetManager, $palette, $propertyValues)
    {
        $propertyDefinitions = $this->definition->getPropertiesDefinition();
        $isAutoSubmit        = ($this->environment->getInputProvider()->getValue('SUBMIT_TYPE') === 'auto');

        $fieldSets = [];
        $errors    = [];
        $first     = true;
        foreach ($palette->getLegends() as $legend) {
            $legendName = $this->translator->translate(
                $legend->getName() . '_legend',
                $this->definition->getName()
            );
            $fields     = [];
            $hidden     = [];
            $properties = $legend->getProperties($this->model, $propertyValues);

            if (!$properties) {
                continue;
            }

            foreach ($properties as $property) {
                $propertyName = $property->getName();
                $this->ensurePropertyExists($propertyName, $propertyDefinitions);

                // If this property is invalid, fetch the error.
                if ((!$isAutoSubmit)
                    && $propertyValues
                    && $propertyValues->hasPropertyValue($propertyName)
                    && $propertyValues->isPropertyValueInvalid($propertyName)
                ) {
                    $errors[] = $propertyValues->getPropertyValueErrors($propertyName);
                }

                $fields[] = $widgetManager->renderWidget($propertyName, $isAutoSubmit, $propertyValues);
                $hidden[] = sprintf('<input type="hidden" name="FORM_INPUTS[]" value="%s">', $propertyName);
            }

            $fieldSet['label']   = $legendName;
            $fieldSet['class']   = $first ? 'tl_tbox' : 'tl_box';
            $fieldSet['palette'] = implode('', $hidden) . implode('', $fields);
            $fieldSet['legend']  = $legend->getName();
            $fieldSets[]         = $fieldSet;

            $first = false;
        }

        if ([] !== $errors) {
            $this->errors = array_merge(...$errors);
        }

        return $fieldSets;
    }