I'm porting code from ZF2 to ZF3.
In ZF2 when I create a form via FormElementManager I can access the servicelocator on the init method and configure some stuff like this:
public function init()
{
$this->serviceLocator = $this->getFormFactory()->getFormElementManager()->getServiceLocator();
$this->translator = $this->serviceLocator->get('translator');
}
This is convenient in very large applications. In fact all my forms inherit from a BaseForm class.
In ZF3 this is bad pratic and serviceLocator are deprecated.
Which is the best way to get the same result ?
One way is to inject every form in the ControllerFactory or ServiceFactory with the stuff needed but this is very tedious.
Any help is appreciate.
First of, you should not have the ServiceManager and/or childs of it (like the FormElementManager) available in your Form objects.
Using the Factory pattern, you should create fully functional, stand-alone Form, Fieldset and InputFilter objects.
There will definitely be some tedious work, as you put it, but you need only do it once.
Let's say you want to create a Location. A Location consists of a name property and a OneToOne unidirectional Address reference. This creates the following needs:
LocationForm (-InputFilter)
LocationFieldset (-InputFilter)
AddressFieldset (-InputFilter)
Config for the above
Factory for each of the 6 classes above
In this answer I'll mash everything down to bare minimums and use classes and examples from my own repositories, so for full code you can go here and for examples here.
After the creation of the classes themselves, I'll show you the config you need for this use case and the Factories which tie all of it together.
AbstractFieldset
abstract class AbstractFieldset extends Fieldset
{
public function init()
{
$this->add(
[
'name' => 'id',
'type' => Hidden::class,
'required' => false,
]
);
}
}
AbstractInputFilter
abstract class AbstractFieldsetInputFilter extends AbstractInputFilter
{
public function init()
{
$this->add([
'name' => 'id',
'required' => false,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
['name' => IsInt::class],
],
]);
}
}
AddressFieldset
class AddressFieldset extends AbstractFieldset
{
public function init()
{
parent::init();
$this->add([
'name' => 'street',
'required' => true,
'type' => Text::class,
'options' => [
'label' => 'Address',
],
]);
}
}
AddressInputFilter
class AddressFieldsetInputFilter extends AbstractFieldsetInputFilter
{
public function init()
{
parent::init();
$this->add([
'name' => 'street',
'required' => true,
'filters' => [
['name' => StringTrim::class],
['name' => StripTags::class],
[
'name' => ToNull::class,
'options' => [
'type' => ToNull::TYPE_STRING,
],
],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 3,
'max' => 255,
],
],
],
]);
}
}
So far, easy. Now, we need to create the LocationFieldset and LocationFieldsetInputFilter. These will make use of the Address(-Fieldset) classes.
LocationFieldset
class LocationFieldset extends AbstractFieldset
{
public function init()
{
parent::init();
$this->add([
'name' => 'name',
'required' => true,
'type' => Text::class,
'options' => [
'label' => 'Name',
],
]);
$this->add([
'type' => AddressFieldset::class,
'name' => 'address',
'required' => true,
'options' => [
'use_as_base_fieldset' => false,
'label' => 'Address',
],
]);
}
}
LocationFieldsetInputFilter
class LocationFieldsetInputFilter extends AbstractFieldsetInputFilter
{
/**
* #var AddressFieldsetInputFilter
*/
protected $addressFieldsetInputFilter;
public function __construct(AddressFieldsetInputFilter $addressFieldsetInputFilter)
{
$this->addressFieldsetInputFilter = $addressFieldsetInputFilter;
}
public function init()
{
parent::init();
$this->add($this->addressFieldsetInputFilter, 'address');
$this->add(
[
'name' => 'name',
'required' => true,
'filters' => [
['name' => StringTrim::class],
['name' => StripTags::class],
[
'name' => ToNull::class,
'options' => [
'type' => ToNull::TYPE_STRING,
],
],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 3,
'max' => 255,
],
],
],
]
);
}
}
Ok, so that's not very exciting yet. Do note, the LocationFieldset uses the AddressFieldset as a type. Instead, in the InputFilter class a full fledged class object (an InputFilter instance) is expected.
So, the Form. I also use an AbstractForm (BaseForm in your case) to handle a few defaults. In my complete one (in linked repo), there's a bit more, but for here this'll suffice. This adds CSRF protection to the Form and adds a submit button if the form does not have one. This only gets done if the Form class does not have either one when you call the init, so you can override these settings.
AbstractForm
abstract class AbstractForm extends \Zend\Form\Form implements InputFilterAwareInterface
{
protected $csrfTimeout = 900; // 15 minutes
public function __construct($name = null, $options = [])
{
$csrfName = null;
if (isset($options['csrfCorrector'])) {
$csrfName = $options['csrfCorrector'];
unset($options['csrfCorrector']);
}
parent::__construct($name, $options);
if ($csrfName === null) {
$csrfName = 'csrf';
}
$this->addElementCsrf($csrfName);
}
public function init()
{
if (!$this->has('submit')) {
$this->addSubmitButton();
}
}
public function addSubmitButton($value = 'Save', array $classes = null)
{
$this->add([
'name' => 'submit',
'type' => Submit::class,
'attributes' => [
'value' => $value,
'class' => (!is_null($classes) ? join (' ', $classes) : 'btn btn-primary'),
],
]);
}
public function get($elementOrFieldset)
{
if ($elementOrFieldset === 'csrf') {
// Find CSRF element
foreach ($this->elements as $formElement) {
if ($formElement instanceof Csrf) {
return $formElement;
}
}
}
return parent::get($elementOrFieldset);
}
protected function addElementCsrf($csrfName = 'csrf')
{
$this->add([
'type' => Csrf::class,
'name' => 'csrf',
'options' => [
'csrf_options' => [
'timeout' => $this->csrfTimeout,
],
],
]);
}
}
LocationForm
class LocationForm extends AbstractForm
{
public function init()
{
$this->add([
'name' => 'location',
'type' => LocationFieldset::class,
'options' => [
'use_as_base_fieldset' => true,
],
]);
parent::init();
}
}
Now we have everything to make the Form. We still need the validation. Let's create these now:
AddressFieldsetInputFilter
class AddressFieldsetInputFilter extends AbstractFieldsetInputFilter
{
public function init()
{
parent::init();
$this->add([
'name' => 'street',
'required' => true,
'filters' => [
['name' => StringTrim::class],
['name' => StripTags::class],
[
'name' => ToNull::class,
'options' => [
'type' => ToNull::TYPE_STRING,
],
],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 3,
'max' => 255,
],
],
],
]);
}
}
LocationFieldsetInputFilter
class LocationFieldsetInputFilter extends AbstractFieldsetInputFilter
{
protected $addressFieldsetInputFilter;
public function __construct(AddressFieldsetInputFilter $addressFieldsetInputFilter)
{
$this->addressFieldsetInputFilter = $addressFieldsetInputFilter;
}
public function init()
{
parent::init();
$this->add($this->addressFieldsetInputFilter, 'address');
$this->add(
[
'name' => 'name',
'required' => true,
'filters' => [
['name' => StringTrim::class],
['name' => StripTags::class],
[
'name' => ToNull::class,
'options' => [
'type' => ToNull::TYPE_STRING,
],
],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 3,
'max' => 255,
],
],
],
]
);
}
}
LocationFormInputFilter
class LocationFormInputFilter extends AbstractFormInputFilter
{
/** #var LocationFieldsetInputFilter */
protected $locationFieldsetInputFilter;
public function __construct(LocationFieldsetInputFilter $filter)
{
$this->locationFieldsetInputFilter = $filter
}
public function init()
{
$this->add($this->locationFieldsetInputFilter, 'location');
parent::init();
}
}
Right, that's all of the classes themselves. Do you see how they'll be nested together? This creates re-usable components, which is why I said you'll need to do this only once. Next time you need an Address or a Location, you just make sure to load the AddressFieldset and set the InputFilter in the Factory. The latter, setting the right InputFilter, is done via Setter Injection the Factories. Shown below.
AbstractFieldsetFactory
abstract class AbstractFieldsetFactory implements FactoryInterface
{
/**
* #var string
*/
protected $name;
/**
* #var string
*/
protected $fieldset;
/**
* #var string
*/
protected $fieldsetName;
/**
* #var string
*/
protected $fieldsetObject;
public function __construct($fieldset, $name, $fieldsetObject)
{
$this->fieldset = $fieldset;
$this->fieldsetName = $name;
$this->fieldsetObject = $fieldsetObject;
$this->hydrator = new Reflection(); // Replace this with your own preference, either Reflection of ZF or maybe the Doctrine EntityManager...
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$fieldset = $this->fieldset;
$fieldsetObject = $this->fieldsetObject;
/** #var AbstractFieldset $fieldset */
$fieldset = new $fieldset($this->hydrator, $this->name ?: $this->fieldsetName);
$fieldset->setHydrator(
new DoctrineObject($this->hydrator)
);
$fieldset->setObject(new $fieldsetObject());
return $fieldset;
}
}
AddressFieldsetFactory
class AddressFieldsetFactory extends AbstractFieldsetFactory
{
public function __construct()
{
parent::__construct(AddressFieldset::class, 'address', Address::class);
}
}
LocationFieldsetFactory
class LocationFieldsetFactory extends AbstractFieldsetFactory
{
public function __construct()
{
parent::__construct(LocationFieldset::class, 'location', Location::class);
}
}
AbstractFieldsetInputFilterFactory
abstract class AbstractFieldsetInputFilterFactory implements FactoryInterface
{
/**
* #var ContainerInterface
*/
protected $container;
/**
* #var HydratorInterface
*/
protected $hydrator;
/**
* #var InputFilterPluginManager
*/
protected $inputFilterManager;
/**
* Use this function to setup the basic requirements commonly reused.
*
* #param ContainerInterface $container
* #param string|null $className
* #throws \Psr\Container\ContainerExceptionInterface
* #throws \Psr\Container\NotFoundExceptionInterface
*/
public function setupRequirements(ContainerInterface $container, $className = null)
{
$this->inputFilterManager = $container->get(InputFilterPluginManager::class);
}
}
AddressFieldsetInputFilterFactory
class AddressFieldsetInputFilterFactory extends AbstractFieldsetInputFilterFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
parent::setupRequirements($container, Address::class);
return new AddressFieldsetInputFilter($this->hydrator);
}
}
LocationFieldsetInputFilterFactory
class LocationFieldsetInputFilterFactory extends AbstractFieldsetInputFilterFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
parent::setupRequirements($container, Location::class);
/** #var AddressFieldsetInputFilter $addressFieldsetInputFilter */
$addressFieldsetInputFilter = $this->inputFilterManager->get(AddressFieldsetInputFilter::class);
return new LocationFieldsetInputFilter(
$addressFieldsetInputFilter,
$this->hydrator
);
}
}
That takes care of the FieldsetInputFilterFactory classes. Just the Form left.
In my case I use the same abstract factory class as for the Fieldset classes.
LocationFormInputFilterFactory
class LocationFormInputFilterFactory extends AbstractFieldsetInputFilterFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
parent::setupRequirements($container);
/** #var LocationFieldsetInputFilter $locationFieldsetInputFilter */
$locationFieldsetInputFilter = $this->getInputFilterManager()->get(LocationFieldsetInputFilter::class);
return new LocationFormInputFilter(
$locationFieldsetInputFilter,
$this->hydrator
);
}
}
So, that's all of the classes done. It's a complete setup. You might encounter some bugs as I modified my own code to remove getters/setters, code comments/hinting, error, property and variable checking without testing. But it should work ;)
However, we're nearly done. We still need:
config
usage in a Controller
print/use Form in a View
The config is easy:
'form_elements' => [
'factories' => [
AddressFieldset::class => AddressFieldsetFactory::class,
LocationFieldset::class => LocationFieldsetFactory::class,
LocationForm::class => LocationFormFactory::class,
],
],
'input_filters' => [
'factories' => [
AddressFieldsetInputFilter::class => AddressFieldsetInputFilterFactory::class,
LocationFieldsetInputFilter::class => LocationFieldsetInputFilterFactory::class,
LocationFormInputFilter::class => LocationFormInputFilterFactory::class,
],
],
That's it. That little bit ties all of the above classes together.
To get the Form into a Controller, you would do something like this in the Factory:
class EditControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$hydrator = new Reflection(); // or $container->get('hydrator') or $container->get(EntityManager::class), or whatever you use
/** #var FormElementManagerV3Polyfill $formElementManager */
$formElementManager = $container->get('FormElementManager');
/** #var LocationForm $form */
$form = $formElementManager->get(LocationForm::class); // See :) Easy, and re-usable
return new EditController($hydrator, $form);
}
}
A typical "Edit" action would be like this (mind, this one uses Doctrine's EntityManager as the hydrator):
public function editAction()
{
$id = $this->params()->fromRoute('id', null);
/** #var Location $entity */
$entity = $this->getObjectManager()->getRepository(Location::class)->find($id);
/** #var LocationForm $form */
$form = $this->form;
$form->bind($entity);
/** #var Request $request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
/** #var Location $object */
$object = $form->getObject();
$this->getObjectManager()->persist($object);
try {
$this->getObjectManager()->flush();
} catch (Exception $e) {
// exception handling
}
return $this->redirect()->toRoute('route/name', ['id' => $object->getId()]);
}
}
return [
'form' => $form,
'validationMessages' => $form->getMessages() ?: '',
];
}
And the View Partial would look like this (based on the return in the above action):
form($form) ?>
So, that's it. Fully fledged, re-usable classes. Single setup. And in the end just a single line in the Factory for the Controller.
Please take note though:
Form, Fieldset and InputFilter use "address" input name. Very important to keep these the same throughout as Zend does some magic based on the names to match Fieldset with InputFilter.
If you have any more questions about how this works, please read through the documentation in the repo's I linked first, before asking below this question. There's more there that should help you out more, for example for Collection handling.
I'm trying to implement my zend navigation from a container in ZF3. I have successfully created navigation with this quick start tutorial introducing navigation directly in config/autoload/global.php or config/module.config.php files:
https://docs.zendframework.com/zend-navigation/quick-start/
But now I need to make it work these with the helpers to allow navigation modifications from the controller, using the "Navigation setup used in examples" section:
https://docs.zendframework.com/zend-navigation/helpers/intro/
This is my Module.php
namespace Application;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\View\HelperPluginManager;
class Module implements ConfigProviderInterface
{
public function getViewHelperConfig()
{
return [
'factories' => [
// This will overwrite the native navigation helper
'navigation' => function(HelperPluginManager $pm) {
// Get an instance of the proxy helper
$navigation = $pm->get('Zend\View\Helper\Navigation');
// Return the new navigation helper instance
return $navigation;
}
]
];
}
public function getControllerConfig()
{
return [
'factories' => [
$this->getViewHelperConfig()
);
},
],
];
}
}
And this is my IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Navigation\Navigation;
use Zend\Navigation\Page\AbstractPage;
class IndexController extends AbstractActionController
{
private $navigationHelper;
public function __construct(
$navigationHelper
){
$this->navigationHelper = $navigationHelper;
}
public function indexAction()
{
$container = new Navigation();
$container->addPage(AbstractPage::factory([
'uri' => 'http://www.example.com/',
]));
$this->navigationHelper->plugin('navigation')->setContainer($container);
return new ViewModel([
]);
}
}
But then I get the following error:
Fatal error: Call to a member function plugin() on array in /var/www/html/zf3/module/Application/src/Controller/IndexController.php on line 50
In the tutorial they use the following statement:
// Store the container in the proxy helper:
$view->plugin('navigation')->setContainer($container);
// ...or simply:
$view->navigation($container);
But I don´t know what this $view is, so I assume is my $navigation from my Module.php. The problem is that, because is an array, it throws the error. The questions are:
What am I doing wrong?
Where this $view of the tutorial come from?
What I should pass from my Module.php to get it work?
Thanks in advance!
Add into module.config.php
'service_manager' => [
'factories' => [
Service\NavManager::class => Service\Factory\NavManagerFactory::class,
],
],
'view_helpers' => [
'factories' => [
View\Helper\Menu::class => View\Helper\Factory\MenuFactory::class,
],
'aliases' => [
'mainMenu' => View\Helper\Menu::class,
],
],
Create Factory in Service Directory:
namespace Application\Service\Factory;
use Interop\Container\ContainerInterface;
use Application\Service\NavManager;
class NavManagerFactory {
/**
* This method creates the NavManager service and returns its instance.
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$authService = $container->get(\Zend\Authentication\AuthenticationService::class);
$viewHelperManager = $container->get('ViewHelperManager');
$urlHelper = $viewHelperManager->get('url');
return new NavManager($authService, $urlHelper);
}
}
Create NavManager file :
namespace Application\Service;
class NavManager {
/**
* Auth service.
* #var Zend\Authentication\Authentication
*/
private $authService;
/**
* Url view helper.
* #var Zend\View\Helper\Url
*/
private $urlHelper;
/**
* Constructs the service.
*/
public function __construct($authService, $urlHelper) {
$this->authService = $authService;
$this->urlHelper = $urlHelper;
}
/**
* Menu render based on user role
*
* #return array
*/
public function getMenuItems() {
$navItem = array();
$url = $this->urlHelper;
$items = [];
$items[] = [
'label' => 'Dashboard',
'icon' => 'dashboard',
'link' => $url('home'),
'route' => ['home'],
];
$items[] = [
'label' => 'About Us',
'icon' => 'business',
'link' => $url('about', ['action'=>'index']),
'route' => ['about'],
];
$items[] = [
'label' => 'Service',
'icon' => 'service',
'link' => $url('service', ['action'=>'index']),
'route' => ['service'],
];
return $items;
}
Create Helper Factory
namespace Application\View\Helper\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\View\Helper\Menu;
use Application\Service\NavManager;
/**
* This is the factory for Menu view helper. Its purpose is to
instantiate the helper and init menu items. */
class MenuFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$navManager = $container->get(NavManager::class);
// Get menu items.
$items = $navManager->getMenuItems();
// Instantiate the helper.
return new Menu($items);
}
}
Create Helper :
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
/**
* This view helper class displays a menu bar.
*/
class Menu extends AbstractHelper {
/**
* Menu items array.
* #var array
*/
protected $items = [];
/**
* Active item's ID.
* #var string
*/
protected $activeItemId = '';
/**
* Constructor.
* #param array $items Menu items.
*/
public function __construct($items=[]) {
$this->items = $items;
}
/**
* Sets menu items.
* #param array $items Menu items.
*/
public function setItems($items) {
$this->items = $items;
}
/**
* Sets ID of the active items.
* #param string $activeItemId
*/
public function setActiveItemId($activeItemId) {
$this->activeItemId = $activeItemId;
}
/**
* Renders the menu.
* #return string HTML code of the menu.
*/
public function render() {
if (count($this->items)==0) {
return ''; // Do nothing if there are no items.
}
// Render items
foreach ($this->items as $item) {
$result .= $this->renderItem($item);
}
return $result;
}
protected function renderItem($item) {
// here you can integrate with HTML
return $item;
}
}
After add above codes,just add below code in your layout file :
echo $this->mainMenu()->render();
Can implement dynamic validation on element level? I used this example to implement validation of one element dependent on the value of the other element. But for this purpose I'll need to implement this for every single form where I use this element (comment) with this validation. I have many forms like that. Is there way to do the following:
to take this filter/validation logic to the element level using some kind of "data-comment-for" attribute and retrieving the value of the element on which it depends from the parent form.
This is my current code (but I need to have it for every form now. It does not look elegant at all) :
class CompetencyAdvanceHumanRightsAndJusticeFormFilter extends InputFilter
{
public function isValid($context = null)
{
$figradeCommentName = 'applJusticeFIGrade'.'Comment';
$forGrade = $this->get('applJusticeFIGrade');
$gradeComment = $this->get($figradeCommentName);
$applJusticeFIGradeRawValue = $forGrade->getRawValue('applJusticeFIGrade');
if(is_numeric($applJusticeFIGradeRawValue)){
$gradeValue = intval($applJusticeFIGradeRawValue);
}else{
$gradeValue = $applJusticeFIGradeRawValue;
}
if ($gradeValue != 'na' && $gradeValue > 0) {
$gradeComment->setRequired(true);
$validatorChain = new Validator\ValidatorChain();
$validatorChain->attach(
new Validator\NotEmpty(),
true
);
$gradeComment->setValidatorChain($validatorChain);
}
return parent::isValid($context);
}
public function __construct(){
$this->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$this->add(array(
'name' => 'studEvalId',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
}
}
EDIT:
I added code for the custom element to the question. There are some "leftovers" of my attempts to place this logic to the element level.
Comment Element
class Comment extends Element implements InputProviderInterface
{
/**
* #var ValidatorInterface
*/
protected $validator;
// set its type
protected $attributes = array(
'type' => 'comment'
);
public function init()
{
if (null === $this->validator) {
$validator = new StringLength();
$validator->setMax(10);
$validator->setMessage('The comment should not exceed 1000 letters!', StringLength::INVALID);
$this->validator = $validator;
}
}
/**
* Get a validator if none has been set.
*
* #return ValidatorInterface
*/
public function getValidator()
{
return $this->validator;
}
/**
* #param ValidatorInterface $validator
* #return $this
*/
public function setValidator(ValidatorInterface $validator)
{
$this->validator = $validator;
return $this;
}
/**
* remove require and validator defaults because we have none
*
* #return array
*/
public function getInputSpecification()
{
// return array(
// 'name' => $this->getName(),
// 'required' => false,
// 'validators' => array(
// $this->getValidator(),
// ),
// 'filters' => array(
// new FIGradeCommentDynamicBufferFilter()
// ),
// );
return array(
'name' => $this->getName(),
'required' => false,
'filters' => array(
array('name' => 'Zend\Filter\StringTrim'),
),
'validators' => array(
$this->getValidator(),
),
);
}
// tell it where to find its view helper, so formRow and the like work correctly
public function getViewHelperConfig()
{
return array('type' => '\OnlineFieldEvaluation\View\Helper\FormComment');
}
}
You could make a base abstract input-filter class and an interface and make all your form filters extend the base class that implements the interface with the methods you expect inside your form classes to make the thing work correctly.
Make an interface with the methods:
interface GradeCommentFormFilterInterface()
{
protected function getGradeInput();
protected function getCommentInput();
}
Then you move the common code to your base class:
abstract class BaseGradeCommentFormFilter extends InputFilter implements GradeCommentFormFilterInterface
{
protected function getGradeInput()
{
return $this->get(static::GRADE_NAME);
}
protected function getCommentInput()
{
return $this->get(static::GRADE_NAME . 'Comment');
}
public function isValid($context = null)
{
$gradeInput = $this->getGradeInput();
$commentInput = $this->getCommentInput();
$rawValue = $this->getRawValue($gradeInput);
if(is_numeric($rawValue))
{
$gradeValue = intval($rawValue);
}
else
$gradeValue = $rawValue;
if ($gradeValue != 'na' && $gradeValue > 0) {
$commentInput->setRequired(true);
$validatorChain = new Validator\ValidatorChain();
$validatorChain->attach(
new Validator\NotEmpty(),
true
);
$commentInput->setValidatorChain($validatorChain);
}
return parent::isValid($context);
}
}
Now you can use your abstract class like this:
class CompetencyAdvanceHumanRightsAndJusticeFormFilter extends BaseGradeCommentFormFilter
{
const GRADE_NAME = 'applJusticeFIGrade';
//... other code
}
I quickly tried to make it work for your case, but this isn't tested, and probably there are ways to optimize this, but it gives you an idea of what you can do.
I setup a basic proof of concept involving Musicians and Albums for binding a form with a form collection in Zend Framework.
Here is the Musician Class:
<?php
namespace Application\Entity;
class Musician {
protected $name;
protected $albums;
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setAlbums($album)
{
$this->album = $album;
return $this;
}
public function getAlbums()
{
return $this->albums;
}
Here is the Album Class:
<?php
namespace Application\Entity;
class Album {
protected $name;
protected $releaseYear;
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setReleaseYear($releaseYear)
{
$this->releaseYear = $releaseYear;
return $this;
}
public function getReleaseYear()
{
return $this->releaseYear;
}
}
Album Fieldset:
Album Field Set:
<?php
namespace Application\Form\Music;
use Zend\Form\Fieldset;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Validator;
use Zend\Form\Element;
use Application\Entity\Album;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\InputFilter\InputFilterProviderInterface;
class AlbumFieldSet extends Fieldset implements InputFilterProviderInterface, ServiceLocatorAwareInterface
{
public function __construct()
{
parent::__construct('album');
$this->setObject(new Album());
$this->setHydrator(new ClassMethods());
$this->add(array(
'type' => 'Text',
'name' => 'name',
'options' => [
]
));
$this->add(array(
'type' => 'Text',
'name' => 'releaseYear',
'options' => [
]
));
}
public function init()
{
}
/**
* Should return an array specification compatible with
* {#link Zend\InputFilter\Factory::createInputFilter()}.
*
* #return array
*/
public function getInputFilterSpecification()
{
return [
'name' => array(
'required' => true,
'validators' => array(
)
),
];
}
/**
* Set service locator
*
* #param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->sl = $serviceLocator;
}
/**
* Get service locator
*
* #return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->sl;
}
}
Here is the Musician Form
<?php
namespace Application\Form\Music;
use Application\Entity\Album;
use Zend\Form\Form;
use Zend\Form\Element\Collection;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Stdlib\Hydrator\ObjectProperty;
use Zend\Validator;
use Zend\Form\Element;
use Application\Form\Music\AlbumFieldset;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\InputFilter\InputFilterProviderInterface;
class MusicianForm extends Form implements InputFilterProviderInterface, ServiceLocatorAwareInterface
{
public function __construct()
{
parent::__construct('');
}
public function init()
{
}
public function setMusician($musician) {
$this->setHydrator(new ClassMethods());
$this->add(array(
'type' => 'Text',
'name' => 'name',
'options' => [
]
));
$this->buildFields();
$this->bind($musician);
}
public function buildFields() {
$fs = new AlbumFieldSet();
$fs->setHydrator(new ObjectProperty());
$fs->setObject(new Album());
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'albums',
'options' => array(
'label' => 'Form Values',
'count' => 2,
'allow_add' => false,
'allow_remove' => false,
'should_create_template' => false,
'target_element' => $fs,
'use_as_base_fieldset' => true,
),
));
}
/**
* Should return an array specification compatible with
* {#link Zend\InputFilter\Factory::createInputFilter()}.
*
* #return array
*/
public function getInputFilterSpecification()
{
return [
'name' => array(
'required' => true,
'validators' => array(
)
),
];
}
/**
* Set service locator
*
* #param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->sl = $serviceLocator;
}
/**
* Get service locator
*
* #return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->sl;
}
}
Controller Code:
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\Musician as Musician;
use Application\Entity\Album as Album;
class MusiciansController extends AbstractActionController
{
public function createMusicianAction()
{
$musician = new Musician();
$albumOne = new Album();
$albumTwo = new Album();
$albumOne->setName('The White Album');
$albumTwo->setName('Sgt. Pepper');
$albumOne->setReleaseYear('1974');
$albumTwo->setReleaseYear('1967');
$albums = array(
$albumOne,
$albumTwo
);
$musician->setName('The Beatles');
$musician->setAlbums($albums);
$form = $this->getServiceLocator()->get('FormElementManager')->get('MusicianForm');
$form->setMusician($musician);
return new ViewModel([
]);
}
}
when I attempt to bind the form, I end up with the following error:
Zend\Form\Element\Collection::setObject expects an array or Traversable object argument; received "Application\Entity\Musician"
I attempted to implement iterator in the musician class, but the solution there seems to be complicated and isn't quite clear. How get this bind to work properly?
I figured it out!
The problem here was that Zend Framework requires all entities related to the form to have their own fieldset for bind to properly work!
In this specific example, I created a musician fieldset, set it as the base fieldset in the musician form, and created the album form collection within the musician fieldset. Voila! Everything populates quite nicely.
i am using zendframework 2 and doctrine 2. i want to populate the values of my MultiCheckbox from values in my database .
i got the technique from: https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md
namespace Users\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class addForm extends form implements ObjectManagerAwareInterface
{
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function getObjectManager()
{
return $this->objectManager;
}
public function __construct($name = null)
{
parent::__construct('add');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/formdata');
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'option',
'options' => array(
'label' => 'Options Véhicule',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Users\Entity\optionsvehicule',
'property' => 'property'
, )));
the error message i received:
No object manager was set.
I have tried and found similar error. After some search I found solution posted on https://github.com/doctrine/DoctrineModule/issues/175. Which works.
For implement you need to do some changes like that
In Module.php add method getFormElementConfig :
public function getFormElementConfig()
{
return array(
'invokables' => array(
'addForm' => 'Users\Form\addForm',
),
'initializers' => array(
'ObjectManagerInitializer' => function ($element, $formElements) {
if ($element instanceof ObjectManagerAwareInterface) {
$services = $formElements->getServiceLocator();
$entityManager = $services->get('Doctrine\ORM\EntityManager');
$element->setObjectManager($entityManager);
}
},
),
);
}
In Your Form Class addForm.php, replace constructor with init method :
namespace Users\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class addForm extends form implements ObjectManagerAwareInterface
{
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function getObjectManager()
{
return $this->objectManager;
}
//public function __construct($name = null)
public function init()
{
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/formdata');
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'option',
'options' => array(
'label' => 'Options Véhicule',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Users\Entity\optionsvehicule',
'property' => 'property'
, )));
In Your Controller Class, Call form obejct through Service Locator :
//$form = new addForm();
$forms = $this->getServiceLocator()->get('FormElementManager');
$form = $forms->get('addForm');
The $objectManager property is undefined.
This is because you call the $this->getObjectManager() method immediately within the __construct() and before you set the variable.
The form depends on the object manager; so you could just add it as a constructor argument which would ensure it is set before the class is used.
Also, the constructor should only really be used for setting up the object's initial properties and state, use init() for modifying form elements.
class addForm extends Form
{
protected $objectManager;
public function __construct(ObjectManager $objectManager)
{
parent::__construct('add-form');
$this->objectManager = $objectManager;
}
// The form element manager will call `init()`
// on the form so we can add the elements in this method
public function init() {
//....
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/formdata');
// $this->add(....
// more elements added here
}
}
Last thing is to register a factory that actually does the injection
class Module {
public function getFormElementConfig() {
return array(
'factories' => array(
'ModuleName\Form\FooForm' => function($formElementManager) {
$serviceManager = $formElementManager->getServiceLocator();
$objectManager = $serviceManager->get('ObjectManager');
$form = new Form\FooForm($objectManager);
return $form;
},
),
);
}
}