Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gregtyler opened this issue Mar 21, 2023 · 0 comments
Open

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

gregtyler opened this issue Mar 21, 2023 · 0 comments
Labels
Bug Something isn't working

Comments

@gregtyler
Copy link

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.

@gregtyler gregtyler added the Bug Something isn't working label Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant