Skip to content

[Form] isEmpty #13940

Closed
Closed
@nino-s

Description

@nino-s

The method isEmpty checks the input form data. It checks for simple nullable elements, empty strings, empty arrays and empty traversables. Am i wrong? If i have an object, which consist only of empty collections, nullable fields etc., shouldn't it check such an 'empty' object and return true aswell?

Following use case: I have an embedded collection in an entity form. It's attributes have some asserts (notnull, empty, etc.). Adding and Removing with prototype works perfectly, but if i just add a new entry and enter nothing, it should delete the entry on submit with the flag delete_empty. Actually, it does not, because modelData is an object, therefore not empty and the standard validation errors occur.

Current isEmpty method:

    public function isEmpty()
    {
        foreach ($this->children as $child) {
            if (!$child->isEmpty()) {
                return false;
            }
        }

        return FormUtil::isEmpty($this->modelData) ||
            // arrays, countables
            0 === count($this->modelData) ||
            // traversables that are not countable
            ($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData));
    }

My idea would be, to additionally check if modelData is an object and if the object has a self made isEmpty method. (A generic version is also possible - all getter)
(is_object($this->modelData) && method_exists($this->modelData, 'isEmpty') && call_user_func(array($this->modelData, 'isEmpty')))

Best regards,
Nino

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions