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

Form::getMessages() on plain array collection - fix for issue #135 #136

Merged
merged 3 commits into from
Apr 26, 2017
Merged

Form::getMessages() on plain array collection - fix for issue #135 #136

merged 3 commits into from
Apr 26, 2017

Conversation

vaclavvanik
Copy link
Contributor

I used this fix. If anyone has better idea let me know!

*/
public function testRetrieveErrorMessagesForArrayInputCollection()
{
$form = new TestAsset\ArrayInputCollectionForm();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test here the Fieldset not the Form class or the Collectionelement.

$messages = $form->getMessages();

$this->assertArrayHasKey('foo', $messages);
$this->assertArrayNotHasKey('bar', $messages);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is bar and why do you test it?

use Zend\InputFilter\ArrayInput;
use Zend\InputFilter\InputFilterProviderInterface;

class ArrayInputCollectionForm extends Form\Form implements InputFilterProviderInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, the class is not needed. Please have a look at ZendTest\Form\Element\CollectionTest.

@vaclavvanik
Copy link
Contributor Author

@froschdesign I think about the test again and again. Basically this patch allows storing messages for elements which are not founds. It does not break any other test. I would rewrite test to:

    public function testSetAndGetErrorMessagesForNonExistentElements()
    {
        $messages = [
            'foo' => [
                'foo_message_key' => 'foo_message_val',
            ],
            'bar' => [
                'bar_message_key' => 'bar_message_val',
            ],
        ];

        $fieldset = new Fieldset();
        $fieldset->setMessages($messages);

        $this->assertEquals($messages, $fieldset->getMessages());
    }

agreed?

@froschdesign
Copy link
Member

froschdesign commented Feb 4, 2017

@vaclavvanik
I have written one more test.

This works with your changes:

// ZendTest\Form\Element\CollectionTest

public function testGetErrorMessagesForInvalidCollectionElements()
{
    // Configure InputFilter
    $inputFilter = $this->form->getInputFilter();
    $inputFilter->add(
        [
            'name'     => 'colors',
            'type'     => ArrayInput::class,
            'required' => true,
        ]
    );
    $inputFilter->add(
        [
            'name'     => 'fieldsets',
            'type'     => ArrayInput::class,
            'required' => true,
        ]
    );


    $this->form->setData([]);
    $this->form->isValid();

    $this->assertEquals(
        [
            'colors'     => [
                'isEmpty' => "Value is required and can't be empty",
            ],
            'fieldsets' => [
                'isEmpty' => "Value is required and can't be empty",
            ],
        ],
        $this->form->getMessages()
    );
}

@vaclavvanik
Copy link
Contributor Author

First look at testValidationForInvalidChildElements() => its problem in InputFilter.

Code from Form::isValid()

        $this->isValid = $result = $filter->isValid();
        ...
        if (!$result) {
            $this->setMessages($filter->getMessages());
        }

Error messages are set only when validation fails. This test is imho unrelated to my patch.

@froschdesign
Copy link
Member

froschdesign commented Feb 4, 2017

@vaclavvanik

First look at testValidationForInvalidChildElements() => its problem in InputFilter.

Right, the InputFilter was wrong.

My original intention: we need another test for the Collection element. (based on ZendTest\Form\TestAsset\FormCollection)

  • both Collection elements are required
  • all target elements are required

All error messages received?

@froschdesign
Copy link
Member

@vaclavvanik
👍

@vaclavvanik
Copy link
Contributor Author

@froschdesign tests updated. I hope I did not miss anything.

@vaclavvanik
Copy link
Contributor Author

@froschdesign can you merge if don't have any other ideas?

@akrabat
Copy link
Contributor

akrabat commented Feb 23, 2017

Why is this change on the Fieldset class and not the Collection class?

@froschdesign
Copy link
Member

@akrabat
The Fieldset class resets all previous added messages. Look at this unit test: https://github.com/vaclavvanik/zend-form/blob/b5c442dbab50d9817e3c965aaf083c6b6bf3fed0/test/FieldsetTest.php#L579

@akrabat
Copy link
Contributor

akrabat commented Feb 23, 2017

I mean, why isn't this fixed as new methods setMessages and getMessages on Collection?

i.e. does this bug affect Fieldset as well as Collection?

@froschdesign
Copy link
Member

i.e. does this bug affect Fieldset as well as Collection?

Right!

@akrabat
Copy link
Contributor

akrabat commented Feb 23, 2017

i.e. does this bug affect Fieldset as well as Collection?
Right!

That's a "yes it does" I assume. Next question. Will this have any side-effects for people using normal elements inside Fieldsets?

@froschdesign froschdesign mentioned this pull request Feb 23, 2017
@froschdesign
Copy link
Member

@akrabat
Sorry, for the late response.

Will this have any side-effects for people using normal elements inside Fieldsets?

No and the unit tests for these scenarios says the same.

@weierophinney weierophinney added this to the 2.10.1 milestone Apr 26, 2017
@weierophinney weierophinney merged commit 4b5d4cc into zendframework:master Apr 26, 2017
weierophinney added a commit that referenced this pull request Apr 26, 2017
Form::getMessages() on plain array collection - fix for issue #135
weierophinney added a commit that referenced this pull request Apr 26, 2017
weierophinney added a commit that referenced this pull request Apr 26, 2017
weierophinney added a commit that referenced this pull request Apr 26, 2017
@weierophinney
Copy link
Member

Thanks, @vaclavvanik

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

Successfully merging this pull request may close these issues.

4 participants