Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:02:11+00:00 2026-06-09T22:02:11+00:00

I am currently playing around with ZF2 beta 4 and I seem to be

  • 0

I am currently playing around with ZF2 beta 4 and I seem to be stuck when i try to use fieldsets within a form and getting the data back into the form when the form is submitted. I am not sure if I am not setting the input filters right for fieldsets or I am missing something. For example, I have the following (simplified to make it clear):

Controller

public function indexAction(){
   $form = new MyForm();
   $request = $this->getRequest();
          if ($request->isPost()) {
                 $form->setData($request->post());
                 if ($form->isValid()) {
                        //Do something
                        print_r($form->getData()); //for debug
                 }
          }
   return array('form' => $form);
}

MyForm.php

class MyForm extends Form
{
    public function __construct()
    {
        parent::__construct();
        $this->setName('myForm');
        $this->setAttribute('method', 'post');

        $this->add(array(
                    'name' => 'title',
                    'attributes' => array(
                    'type'  => 'text',
                    'label' => 'Title',
                    ),
                 ));

        $this->add(new MyFieldset('myfieldset'));

        //setting InputFilters here
        $inputFilter = new InputFilter();
        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'     => 'title',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
        )));

        //Now add fieldset Input filter
        foreach($this->getFieldsets() as $fieldset){
              $fieldsetInputFilter = $factory->createInputFilter($fieldset->getInputFilterSpecification());
              $inputFilter->add($fieldsetInputFilter,$fieldset->getName());
        }

        //Set InputFilter
        $this->setInputFilter($inputFilter);
    }
}

MyFieldset.php

class MyFieldset extends Fieldset implements InputFilterProviderInterface{
    public function __construct($name)
    {
        parent::__construct($name);
        $factory = new Factory();

        $this->add($factory->createElement(array(
            'name' => $name . 'foo',
            'attributes' => array(
                'type'  => 'text',
                'label' => 'Foo',
            ),
        )));
    }

    public function getInputFilterSpecification(){
        return array(
            'foo' => array(
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
            ),
        );
    }
}

I am able to output the form as expected and I end up with two input elements named ‘title’ and ‘myfieldsetfoo’ (the name given when outputing with the ViewHelper). So of course when I submit the raw post will show values for ‘title’ and ‘myfieldsetfoo’. However, when I use SetData() the values for the field set are not being populated (although I can see the values in the raw post object). Instead, examining the output of ‘$form->getData()’ I receive:

Array(
   [title] => Test,
   [myfieldset] => Array(
                         [foo] =>
                        )
)

What am I missing? What do I need to do so that ZF2 understands how to populate the fieldset?

Thanks for any help, this is driving me crazy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:02:13+00:00Added an answer on June 9, 2026 at 10:02 pm

    Why I do is concatenate InputFilter so I could handle the whole HTML form array posted.

    <form method="POST">
    <input type="text" name="main[name]" />
    <input type="text" name="main[location]" />
    <input type="text" name="contact[telephone]" />
    
    <input type="submit" value="Send" />
    </form>
    

    This will create an array posted like

    post["main"]["name"]
    post["main"]["location"]
    post["contact"]["telephone"]
    

    Filtered and validated with:

    use Zend\InputFilter\InputFilter;
    use Zend\InputFilter\Factory as InputFactory;
    
    $post = $this->request->getPost();
    $inputFilter = new InputFilter();
    $factory = new InputFactory();
    
    // $post["main"]
    $mainFilter = new InputFilter();
    $mainFilter->add($factory->createInput(array(
        'name'     => 'name',
        'required' => true,
        'filters'  => array(
            array('name' => 'StripTags'),
            array('name' => 'StringTrim'),
        ),
        'validators' => array(
            array(
                'name'    => 'StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min'      => 1,
                    'max'      => 100,
                ),
            ),
        ),
        )));
    
    $mainFilter->add($factory->createInput(array(
        'name'     => 'location',
        'required' => true,
        'filters'  => array(
            array('name' => 'StripTags'),
            array('name' => 'StringTrim'),
        ),
        'validators' => array(
            array(
                'name'    => 'StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min'      => 1,
                    'max'      => 100,
                ),
            ),
        ),
        )));
    $inputFilter->add($mainFilter, "main");
    
    // $post["contact"]
    $contactFilter = new InputFilter();
    $contactFilter->add($factory->createInput(array(
        'name'     => 'name',
        'required' => true,
        'filters'  => array(
            array('name' => 'StripTags'),
            array('name' => 'StringTrim'),
        ),
        'validators' => array(
            array(
                'name'    => 'StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min'      => 1,
                    'max'      => 100,
                ),
            ),
        ),
        )));
    $contactFilter->add($mainFilter, "contact");
    
    //Set posted data to InputFilter
    $inputFilter->setData($post->toArray());
    

    http://www.unexpectedit.com/zf2/inputfilter-validate-and-filter-a-form-data-with-fieldsets

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently playing around with Haskell basics and stumbled upon the following use
I'm currently playing around with ASP .NET MVC 3 and - for data access
I'm currently playing a bit around with scheme, and I've been stuck for quite
I'm currently getting to grips with Android, playing around with the Lunar Lander sample.
I am currently playing around with Google's Voice Recognition API for Android SDK. What
I'm currently playing around with tipfy on Google's Appengine and just recently ran into
I'm currently playing around with some app ideas using AirPlay mirroring and a second
I'm am currently playing around with the third party facial recognition api from face.com.
I am currently playing around with chrome extensions. I would like to catch google
I've got a bit of fettish for language design and I'm currently playing around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.