Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Using ComposedObject annotation with Required(false) fails with exception #7487

Closed
mirfilip opened this issue May 5, 2015 · 5 comments
Closed

Comments

@mirfilip
Copy link

mirfilip commented May 5, 2015

First of all, what is the expected behaviour of having a field in dynamically built form, annotated like

/**
 * @Annotation\Required(false)
 * @Annotation\AllowEmpty()
 * @Annotation\ComposedObject("SomeObject")
 */

Should the field then be optional but SomeObject validated fully when present?

Second part is more of a bug I think. When you take above annotation and switch the order of @Annotation like

/**
 * @Annotation\ComposedObject("SomeObject")
 * @Annotation\Required(false)
 * @Annotation\AllowEmpty()
 */

then AnnotationBuilder fails to build the form with the exception:
Zend\InputFilter\Factory::createInput expects an array or Traversable; received "boolean"

I didn't find any in depth documentation / FAQ / tutorial about using ComposedObject with other annotations but it seems as if:

  1. Form object that is built with AnnotationBuilder does not take Required and AllowEmpty into consideration when ComposedObject is present.
  2. When using AllowEmpty / Required(false) with ComposedObject, the composed object specified gets validated regardless if it's present (and obviously fails if it's not).
  3. Switching order of annotations confuses Zend\Form\Factory::createInputFilter(). $inputFilterSpecification argument contains the SomeObject definition but it also has required = false and allow_empty = true keys. Iterating inputFilterSpecification passes $value = false to Zend\Form\Factory::createInput($value) which refuses to work with false value.

If my explaination is vague, maybe the example could be helpful stackoverflow question

@Maks3w
Copy link
Member

Maks3w commented Sep 5, 2015

There was a bug where optional fields was validated even if data is not set preset zendframework/zend-inputfilter#25. Update to v2.5.5.

Could you create a new issue about annotation order?

@Maks3w Maks3w closed this as completed Sep 5, 2015
@mirfilip
Copy link
Author

mirfilip commented Sep 6, 2015

Sure, I will evaluate if error occurs with 2.5.5 and will split second error to another issue. Can you confirm that it should work like I described above?

@Maks3w
Copy link
Member

Maks3w commented Sep 6, 2015

This is the list of permutations and expected validations / error messages for each one

https://github.com/zendframework/zend-inputfilter/blob/master/test/InputTest.php#L633

@mirfilip
Copy link
Author

mirfilip commented Sep 6, 2015

My question is rather about ComposedObject + Required annotation. As ComposedObject is used as a fieldset annotation, I later realized it can be a fieldset limitation. I'm not sure if there is a concept of "optional fieldset" in ZF2. Correct me if I'm wrong, but you cannot mark a fieldset optional, you rather don't list them in validationGroup.

Again, this is as I remembered it when I dug into this in May. Correct me if I'm wrong.

@mirfilip
Copy link
Author

@Maks3w Can you answer my question from the comment above? I'd like to finish up on the topic.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants