Skip to content

ContainsXxx field should be editable in UI #1860

@abbadon1334

Description

@abbadon1334

Example code
https://gist.github.com/abbadon1334/313e166efb598b8a416d6241c62ddbbb

if ContainsOne or ContainsMany fields in persistence are not null and an exception is raised :
Critical Error
Atk4\Data\ValidationException: ContainsXxx does not support unmanaged data modification

in Crud ContainsOne/ContainsMany must be not visible and should be managed by the framework when a model with those types of fields is added to crud

to solve some issues i already create these Helpers, but I like to have a way in the framework to avoid these helpers, any ideas?

    /**
     * Use on Form to manage ContainsOne fields
     * 
     * @param Layout|Form $form
     * @param string      $fieldName
     *
     * @throws Exception
     * @throws \Atk4\Data\Exception
     * @throws \Atk4\Ui\Exception
     */
    public static function addControlContainsOne($form, string $fieldName): void
    {
        $model = $form->model ?? $form->form->model;

        $ml = $form->addControl($fieldName . '_contains_one', [
            Multiline::class,
            'rowLimit'    => 1,
            'addOnTab'    => false,
            'caption'     => $model->getField($fieldName)->getCaption(),
            'renderLabel' => false,
        ], [
            'neverPersist' => true,
        ]);

        Multiline::assertInstanceOf($ml)->setModel($model->ref($fieldName)->getModel());
        $model->onHook(Model::HOOK_BEFORE_SAVE, function ($m, $update) use ($ml) {
            Multiline::assertInstanceOf($ml)->saveRows();
        });
    }

    /**
     * Use on Form to manage ContainsMany fields
     * 
     * @param Layout|Form $form
     * @param string      $fieldName
     *
     * @throws Exception
     * @throws \Atk4\Data\Exception
     * @throws \Atk4\Ui\Exception
     */
    public static function addControlContainsMany($form, string $fieldName): void
    {
        $model = $form->model ?? $form->form->model;

        $ml = $form->addControl($fieldName . '_contains_many', [
            Multiline::class,
            'addOnTab'    => false,
            'caption'     => $model->getField($fieldName)->getCaption(),
            'renderLabel' => false,
        ], [
            'neverPersist' => true,
        ]);

        Multiline::assertInstanceOf($ml)->setModel($model->ref($fieldName));
        $model->onHook(Model::HOOK_BEFORE_SAVE, function ($m, $update) use ($ml) {
            Multiline::assertInstanceOf($ml)->saveRows();
        });
    }

    /**
     * Call this on model before setModel to Table/Grid/Crud 
     */
    public static function removeContainsXxx(Model $model) {
        foreach($model->getReferences() as $refName => $ref) {
            if ($ref instanceOf ContainsBase) {
                $model->removeField($refName);
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions