Skip to content

ComposedObject annotation doesn't work with Required(false) #211

@gregtyler

Description

@gregtyler

Bug Report

Q A
Version(s) 3.9.0

Summary

In laminas-form v3.9.0 some changes were made (#207) to ComposedObject. I believe this has caused Required(false) to now be ignored, meaning ComposedObjects are always checked, which breaks existing configuration.

Current behavior

If you annotate an element with

    /**
     * @Form\Required(false)
     * @Form\ComposedObject("Application\Model\SubForm")
     */
    private ?SubForm $subForm;

and then plug empty values into a form using laminas-form's ->setData([]), then:

  • ->isValid() returns false
  • ->getMessages() returns ['subForm' => ['name' => 'Value is required and can't be empty']]

How to reproduce

Full working example below. Uncomment the 'name' => 'test', for the form to be valid, and run in 3.8.0 for it to work fine.

<?php

declare(strict_types=1);

use Laminas\Form\Annotation\AnnotationBuilder;
use Laminas\Form\Annotation as Form;

require __DIR__ . '/vendor/autoload.php';

class SubForm
{
    /**
     * @Form\Required(true)
     */
    private string $name;
}

class MyForm
{
    /**
     * @Form\Required(false)
     * @Form\ComposedObject("SubForm")
     */
    private SubForm $subForm;
}

$builder = new AnnotationBuilder();
$form = $builder->createForm(MyForm::class);

$form->setData([
  'subForm' => [
    // 'name' => 'test',
  ],
]);

var_dump($form->isValid());
var_dump($form->getMessages());

Expected behavior

I expected the behavior in 3.8.0, that the entire object can be omitted if @Form\Required(false) is present.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions