Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save settermjd/11176999 to your computer and use it in GitHub Desktop.
A simple example of creating a custom select element, using the FormElementManager to handle the external dependencies.
<?php
/**
* Define the custom form element
*/
namespace Forecaster\Form\Element;
use Zend\Form\Element\Select;
use Zend\InputFilter\InputProviderInterface;
use Zend\Validator\Db\RecordExists;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ArticleList extends Select implements InputProviderInterface, ServiceLocatorAwareInterface
{
protected $validator;
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $sl)
{
$this->serviceLocator = $sl;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
public function init()
{
$validator = new RecordExists(array(
'table' => 'articles',
'field' => 'id',
'adapter' => $this->serviceLocator->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
));
$validator->setMessage('Please choose a valid article!', RecordExists::ERROR_NO_RECORD_FOUND);
$articleTable = $this->serviceLocator->getServiceLocator()->get('Forecaster\Table\ArticleTable');
$this->setValueOptions(array_merge(array(-1 => 'Please Choose'), $articleTable->getSelectData());
$this->validator = $validator;
}
/**
* Get a validator if none has been set.
*
* @return ValidatorInterface
*/
public function getValidator()
{
return $this->validator;
}
/**
* Sets the validator to use for this element
*
* @param ValidatorInterface $validator
* @return \Forecaster\Form\Element\UserList
*/
public function setValidator(ValidatorInterface $validator)
{
$this->validator = $validator;
return $this;
}
/**
* Provide default input rules for this element
*
* Attaches a phone number validator.
*
* @return array
*/
public function getInputSpecification()
{
return array(
'name' => $this->getName(),
'required' => true,
'filters' => array(
array('name' => 'Zend\Filter\Int'),
),
'validators' => array(
$this->getValidator(),
),
);
}
}
?>
<?php
/**
* Define the form, which will use the element
*/
namespace Forecaster\Form;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Forecaster\InputFilter\ArticleInputFilter;
class AddRecordForm extends Form implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function init()
{
$this->add(array(
'name' => 'articleList',
'type' => 'ArticleList',
'options' => array(
'label' => 'Articles'
)
));
}
public function setServiceLocator(ServiceLocatorInterface $sl)
{
$this->serviceLocator = $sl;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
public function __construct()
{
parent::__construct('AddArticle');
$this->setAttribute('method', 'post')
->setAttribute('action', '/forecaster/admin/articles/add')
->setAttribute('class', 'form-horizontal');
$this->add(array(
'name' => 'submit',
'type' => 'Zend\Form\Element\Submit',
'attributes' => array(
'class' => 'btn btn-primary',
'tabindex' => 1,
)
));
$this->get('submit')->setValue('Add');
}
}
?>
<?php
/**
* Define the action retrieving the form via the FormElementManager
*/
namespace Forecaster\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AdminController extends AbstractActionController
{
public function addArticleAction()
{
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('Forecaster\Form\AddRecordForm');
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost());
if ($form->isValid()) {
// ... do something if it's valid
}
}
return new ViewModel(array(
'form' => $form
));
}
}
?>
<?php
/**
* Define the getFormElementConfig method in Module, specifying the custom form element
*/
namespace Forecaster;
use Zend\ModuleManager\Feature\FormElementProviderInterface;
class Module implements FormElementProviderInterface
{
// ...
public function getFormElementConfig()
{
return array(
'invokables' => array(
'ArticleList' => 'Forecaster\Form\Element\ArticleList'
)
);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment