Skip to content

Instantly share code, notes, and snippets.

@awartoft
Created July 6, 2012 12:54
Show Gist options
  • Save awartoft/3060019 to your computer and use it in GitHub Desktop.
Save awartoft/3060019 to your computer and use it in GitHub Desktop.
// Apply the parameters to the form
$form->setData($parameters);
var_dump($parameters);
if ($form->isValid()) {
var_dump($form->getData());
exit;
}
@awartoft
Copy link
Author

awartoft commented Jul 6, 2012

Result

array (size=2)
  'keywords' => string '' (length=0)
  'category' => string '10' (length=2)
array (size=0)
  empty

Expected result

array (size=2)
  'keywords' => string '' (length=0)
  'category' => string '10' (length=2)
array (size=0)
  'keywords' => string '' (length=0)
  'category' => string '10' (length=2)

Form

<?php
/**
 * @author Antoine Hedgecock <[email protected]>
 */

/**
 * @namespace
 */
namespace Company\Form;
use Zend\Form\Form,
    Zend\ServiceManager\ServiceManager,
    Zend\ServiceManager\ServiceManagerAwareInterface,

    Company\Service\Company\Branch    as BranchService,
    MCNCore\Sphinx\Hydrator\QueryInfo as QueryInfoHydrator;

class SearchCompany extends Form implements ServiceManagerAwareInterface
{
    public function __construct()
    {
        parent::__construct('company_search');

        $this->setAttribute('method', 'post')
                ->setInputFilter(new \Zend\InputFilter\InputFilter());

        $this->add(
            array(
                'name'       => 'keywords',
                'attributes' => array(

                    'style' => 'width: 352px;'
                )
            )
        );

        $this->add(
            array(
                 'name' => 'category',
            )
        );

        $this->add(
            array(
                 'name'       => 'submit',
                 'attributes' => array(

                     'type'  => 'submit',
                     'value' => 'Sök'
                 ),
            )
        );
    }

    public function setServiceManager(ServiceManager $sm)
    {
        $options = array(
            'sort' => array(
                BranchService::SORT_NAME => 'ASC'
            ),

            'queryOptions' => array(
                BranchService::QUERY_OPT_WITHOUT_EMPTY_BRANCHES     => true,
                BranchService::QUERY_OPT_WITHOUT_UNSPECIFIED_BRANCH => true
            )
        );

        $categories = $sm->get('company_service_branch')
                         ->fetchAll($options);

        $options = array('Alla kategorier' => '*');

        foreach($categories as $category)
        {
            $options[$category['name']] = $category['id'];
        }

        $this->get('category')
             ->setAttribute('options', $options);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment