Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Subform validation #2583

Closed
zfbot opened this Issue · 10 comments

6 participants

@zfbot
Owner

Jira Information

Original Issue: ZF2-557
Issue Type: Bug
Reporter: Chris Willis
Created: 09/14/12
Assignee: weierophinney
Components: Zend\Form

Description

Subform validation seems to be broken for Checkbox, Radios, MultiCheckboxes and Selects. All of these seem to be ignoring the required value and they also seem to be adding additional InputFilters as "subformname[elementname]" which prevent validation. I believe it has something to do with the attachInputFilterDefaults method on the form class, but can't pinpoint the reason for this behaviour.

Debug output from code by submitting with nothing filled out:

Post data:

Zend\Stdlib\Parameters Object
(
    [storage:ArrayObject:private] => Array
        (
            [select] => 
            [text] => 
            [checkbox] => 0
            [textarea] => 
            [subform] => Array
                (
                    [select] => 
                    [text] => 
                    [checkbox] => 0
                    [textarea] => 
                )

            [submit] => Submit
        )

)

Form messages:

Array
(
    [subform] => Array
        (
            [radio] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [multicheckbox] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [select] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

        )

)

InputFilter messages:

Array
(
    [subform] => Array
        (
            [radio] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [multicheckbox] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [select] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [subform[radio]] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [subform[multicheckbox]] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [subform[select]] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

            [subform[checkbox]] => Array
                (
                    [isEmpty] => Value is required and can't be empty
                )

        )

)
// Main form
$form = new Form();
$form->setInputFilter( $filter = new InputFilter() );

// Radio on main form
$form->add( array(
    'type' => 'Zend\Form\Element\Radio',
    'name' => 'radio',
    'options' => array(
        'value_options' => array(
            '1' => 'Approve',
            '2' => 'Deny',
        ),
        'label' => 'Radio',
    ),
) );

$filter->add( array(
    'name' => 'radio',
    'required' => false,
) );

// MultiCheckbox on main form
$form->add( array(
    'type' => 'Zend\Form\Element\MultiCheckbox',
    'name' => 'multicheckbox',
    'options' => array(
        'value_options' => array(
            '1' => 'Approve',
            '2' => 'Deny',
        ),
        'label' => 'MultiCheckbox',
    ),
) );

$filter->add( array(
    'name' => 'multicheckbox',
    'required' => false,
) );

// Select on main form
$form->add( array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'select',
    'options' => array(
        'value_options' => array(
            '' => 'Select...',
            '1' => 'Approve',
            '2' => 'Deny',
        ),
        'label' => 'Select',
    ),
) );

$filter->add( array(
    'name' => 'select',
    'required' => false,
) );

// Text on main form
$form->add( array(
    'name' => 'text',
    'options' => array(
        'label' => 'Text',
    ),
) );

$filter->add( array(
    'name' => 'text',
    'required' => false,
) );

// Checkbox on main form
$form->add( array(
    'type' => 'Zend\Form\Element\Checkbox',
    'name' => 'checkbox',
    'options' => array(
        'label' => 'Checkbox',
    ),
) );

$filter->add( array(
    'name' => 'checkbox',
    'required' => false,
) );

// Textarea on main form
$form->add( array(
    'type' => 'Zend\Form\Element\Textarea',
    'name' => 'textarea',
    'options' => array(
        'label' => 'Textarea',
    ),
) );

$filter->add( array(
    'name' => 'textarea',
    'required' => false,
) );

// Subform
$form->add( $subForm = new Form(), array( 'name' => 'subform' ) );
$subForm->setInputFilter( $subFilter = new InputFilter() );

// Radio on subform
$subForm->add( array(
    'type' => 'Zend\Form\Element\Radio',
    'name' => 'radio',
    'options' => array(
        'value_options' => array(
            '1' => 'Approve',
            '2' => 'Deny',
        ),
        'label' => 'Radio',
    ),
) );

$subFilter->add( array(
    'name' => 'radio',
    'required' => false,
) );

// MultiCheckbox on subform
$subForm->add( array(
    'type' => 'Zend\Form\Element\MultiCheckbox',
    'name' => 'multicheckbox',
    'options' => array(
        'value_options' => array(
            '1' => 'Approve',
            '2' => 'Deny',
        ),
        'label' => 'MultiCheckbox',
    ),
) );

$subFilter->add( array(
    'name' => 'multicheckbox',
    'required' => false,
) );

// Select on subform
$subForm->add( array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'select',
    'options' => array(
        'value_options' => array(
            '' => 'Select...',
            '1' => 'Approve',
            '2' => 'Deny',
        ),
        'label' => 'Select',
    ),
) );

$subFilter->add( array(
    'name' => 'select',
    'required' => false,
) );

// Text on subform
$subForm->add( array(
    'name' => 'text',
    'options' => array(
        'label' => 'Text',
    ),
) );

$subFilter->add( array(
    'name' => 'text',
    'required' => false,
) );

// Checkbox on subform
$subForm->add( array(
    'type' => 'Zend\Form\Element\Checkbox',
    'name' => 'checkbox',
    'options' => array(
        'label' => 'Checkbox',
    ),
) );

$subFilter->add( array(
    'name' => 'checkbox',
    'required' => false,
) );

// Textarea on subform
$subForm->add( array(
    'type' => 'Zend\Form\Element\Textarea',
    'name' => 'textarea',
    'options' => array(
        'label' => 'Textarea',
    ),
) );

$subFilter->add( array(
    'name' => 'textarea',
    'required' => false,
) );

// Submit
$form->add( array(
    'name' => 'submit',
    'attributes' => array(
        'value' => 'Submit',
    ),
) );

// Process
echo 'Post data:<pre>';
print_r( $this->request->getPost() );
echo '</pre>';
$form->prepare();
if ( $this->request->isPost() && $form->setData( $this->request->getPost() ) )
{
    if ( $form->isValid() )
    {
        echo 'Form data:<pre>';
        print_r( $form->getData() );
        echo '</pre>';
    }
    else
    {
        echo 'Form messages:<pre>';
        print_r( $form->getMessages() );
        echo '</pre>';
        echo 'InputFilter messages:<pre>';
        print_r( $filter->getMessages() );
        echo '</pre>';
    }
}

return new ViewModel( array( 'form' => $form ) );
<?php echo $this->form()->openTag( $form ) ?>
    <fieldset>
        <?php foreach ( $form->getElements() as $element ): ?>
            <?php if ( 'submit' == $element->getName() ) continue ?>
            <?php echo $this->formLabel( $element ) ?>
            <?php $type = 'form' . $element->getName() ?>
            <?php echo $this->{$type}( $element ) ?>
            <br/>
            <?php echo $this->formElementErrors( $element ) ?>
        <?php endforeach ?>

        <fieldset>
            <?php foreach ( $form->get( 'subform' )->getElements() as $element ): ?>
                <?php echo $this->formLabel( $element ) ?>
                <?php $type = 'form' . substr( $element->getName(), 8, strlen( $element->getName() ) - 9 ) ?>
                <?php echo $this->{$type}( $element ) ?>
                <br/>
                <?php echo $this->formElementErrors( $element ) ?>
            <?php endforeach ?>
        </fieldset>

        <?php echo $this->formSubmit( $form->get( 'submit' ) ) ?>
    </fieldset>
<?php echo $this->form()->closeTag() ?>
@zfbot
Owner

This issue was ported from the ZF2 Jira Issue Tracker at
http://framework.zend.com/issues/browse/ZF2-557

Known GitHub users mentioned in the original message or comment:
@weierophinney

@Celtico

working properly subforms?
I'm in my software, and if I'm not knowing do it well
https://github.com/Celtico/QuAdmin/blob/master/src/QuAdmin/Form/QuForm.php

@Celtico Celtico referenced this issue in cgmartin/CgmConfigAdmin
Open

Filters the subform? #13

@ThaDafinser

@Celtico so this is a non-issue?

@Celtico

yes,

@ThaDafinser

ping @weierophinney - can u close this?

@Maks3w Maks3w closed this
@lboynton

What was the solution?

@Celtico

in ZF v2.2.5 does not work, right?

@Celtico

in ZF 2.1.4 is good!

@Celtico

Module for testing subform
https://github.com/Celtico/QuTestForm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.