I have a problem with translating form (labels).
After searching hours on the internet, I can't find a decent explanation how it should be done.
Anybody who can give me a help here?
I'm using the formCollection($form) as written in the ZF2.3 manual
add.phtml
$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
AlbumForm.php
namespace Album\Form;
use Zend\Form\Form;
use Zend\I18n\Translator\Translator;
class AlbumForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('album');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'title',
'type' => 'Text',
'options' => array(
'label' => $this->getTranslator()->translate('Name'), //'Naam',
),
));
$this->add(array(
'name' => 'artist',
'type' => 'Text',
'options' => array(
'label' => 'Code: ',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
Error:
Fatal error: Call to undefined method Album\Form\AlbumForm::getTranslator() in /Applications/MAMP/htdocs/demo/module/Album/src/Album/Form/AlbumForm.php on line 24
The form has no knowledge of a translator by default. What you can do, is make it explicit and inject a translator. Therefore, define a factory for your form:
'service_manager' => [
'factories' => [
'Album\Form\AlbumForm' => 'Album\Factory\AlbumFormFactory',
],
],
Now you can create a factory for this form:
namespace Album\Factory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Album\Form\AlbumForm;
class AlbumFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $sl)
{
$translator = $this->get('MvcTranslator');
$form = new AlbumForm($translator);
return $form;
}
}
Now, finalize your form class:
namespace Album\Form;
use Zend\Form\Form;
use Zend\I18n\Translator\TranslatorInterface;
class AlbumForm extends Form
{
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
parent::__construct('album');
// here your methods
}
protected function getTranslator()
{
return $this->translator;
}
}
Related
I am developing a project with ZF2 and Doctrine. I am attempting to use Doctrine Hydrator in the form creation as shown in this tutorial. In this method, an ObjectManager object is created in the controller and passed to the new form when it is instantiated. Passing the ObjectManager object from the controller to the form creates a problem when I want to use ZF2's FormElementManager because ZF2 requires that I get an instance of the form class through the Zend\Form\FormElementManager instead of directly instantiating it. To work around this requirement, I have created form and fieldset factories based upon the answer to the question How to pass a Doctrine ObjectManager to a form through ZF2 FormElementManager. The method presented in the answer to the question works for typical fieldset elements, but I need to determine how to include a collection element. The tutorial uses the ObjectManager object in the collection element in the parent fieldset, and I need to figure out how to add the collection using a factory.
TagFieldset from the tutorial that I am trying to emulate:
namespace Application\Form;
use Application\Entity\Tag;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class TagFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('tag');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new Tag());
$this->add(array(
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'name',
'options' => array(
'label' => 'Tag'
)
));
}
public function getInputFilterSpecification()
{
return array(
'id' => array(
'required' => false
),
'name' => array(
'required' => true
)
);
}
}
new TagFieldsetFactory:
namespace Application\Form;
use Zend\Form\Fieldset;
use Application\Entity\Tag;
class TagFieldsetFactory
{
public function __invoke($formElementManager, $name, $requestedName)
{
$serviceManager = $formElementManager->getServiceLocator();
$hydrator = $serviceManager->get('HydratorManager')->get('DoctrineEntityHydrator');
$fieldset = new Fieldset('tags');
$fieldset->setHydrator($hydrator);
$fieldset->setObject(new Tag);
//... add fieldset elements.
$fieldset->add(['...']);
//...
return $fieldset;
}
}
BlogPostFieldset from the tutorial that I am trying to emulate:
namespace Application\Form;
use Application\Entity\BlogPost;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class BlogPostFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('blog-post');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new BlogPost());
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'title'
));
$tagFieldset = new TagFieldset($objectManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 2,
'target_element' => $tagFieldset
)
));
}
public function getInputFilterSpecification()
{
return array(
'title' => array(
'required' => true
),
);
}
}
new BlogPostFieldsetFactory:
namespace Application\Form;
use Zend\Form\Fieldset;
use Application\Entity\BlogPost;
class BlogPostFieldsetFactory
{
public function __invoke($formElementManager, $name, $requestedName)
{
$serviceManager = $formElementManager->getServiceLocator();
$hydrator = $serviceManager->get('HydratorManager')->get('DoctrineEntityHydrator');
$fieldset = new Fieldset('blog_post');
$fieldset->setHydrator($hydrator);
$fieldset->setObject(new BlogPost);
//... add fieldset elements.
$fieldset->add(['...']);
//...
return $fieldset;
}
}
in module.config.php:
'form_elements' => [
'factories' => [
'UpdateBlogPostForm' => 'Application\Form\UpdateBlogPostFormFactory',
'BlogPostFieldset' => 'Application\Form\BlogPostFieldsetFactory',
'TagFieldset' => 'Application\Form\TagFieldsetFactory',
],
],
When I add the fieldset elements In my new BlogPostFieldsetFactory I replace this code from the original fieldset:
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'title'
));
with this:
$fieldset->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'title'
));
How do I replace the collection element from the original fieldset:
$tagFieldset = new TagFieldset($objectManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 2,
'target_element' => $tagFieldset
)
));
maybe i'm getting your question wrong.... but if you replaced this
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'title'
));
whith this:
$fieldset->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'title'
));
then you probably can replace this:
$tagFieldset = new TagFieldset($objectManager);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 2,
'target_element' => $tagFieldset
)
));
with this:
$tagFieldset = new TagFieldset($objectManager);
$fieldset->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 2,
'target_element' => $tagFieldset
)
));
now, if you cant pass the $objectManger to the form... well if you look at the code you have this thing available $serviceManager, that thing looks like a DI container, im sure you can get the $objectManager instance from there, and if is not available, you can probably put an instance of it inside.
So de final code probably ending looks like this:
$objectManager = $serviceManager->get('DoctrineObjectManager') //or something like this
$tagFieldset = new TagFieldset($objectManager);
$fieldset->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 2,
'target_element' => $tagFieldset
)
));
I have a problem with Zend Framework 2 and Date element. The attribute I'm trying to store is a DateOfBirth, but this attribute maybe empty. For example the date is unknown. The column in the database allows NULL. The Doctrine class attached to it has a attribute that let's it know it allows null. But Zend Framework 2 still gives me this error:
"Value is required and can't be empty".
Even though I set the required attribute=false, also the allow_empty=true, but nothing works.
The attirbute it a member of a nested fieldset within a form. The nesting looks as follows:
UserManagementForm
User (fieldset)
Person (fieldset)
DateOfBirth (element)
Couple examples i tried:
Form not validating correctly zend framework 2
https://github.com/zendframework/zf2/issues/4302
Here is the code I am using at the moment. Hopefully you see something that I'm missing. I don't know if it due to the fact that it is nested, but the rest works perfect, only the date element is causing me trouble.
UserManagementForm
<?php
namespace Application\Form;
use Zend\Form\Form;
class UserManagementForm extends Form
{
public function __construct()
{
parent::__construct('usermanagementform');
$this->setAttribute('method', 'post');
$fieldset = new \Application\Form\Fieldset\User();
$fieldset
->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty(false))
->setObject(new \Application\Entity\User())
->setOptions(array('use_as_base_fieldset' => true))
;
$this->add($fieldset);
$this->add(array(
'name' => 'btnSubmit',
'type' => 'submit',
'attributes' => array(
'class' => 'btn-primary',
),
'options' => array(
'column-size' => 'sm-9 col-sm-offset-3',
'label' => 'Save changes',
),
));
}
}
?>
User (Fieldset)
<?php
namespace Application\Form\Fieldset;
use Zend\Form\Fieldset;
class User extends Fieldset
{
public function __construct()
{
parent::__construct('User');
$fieldset = new \Application\Form\Fieldset\EmailAddress();
$fieldset
->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty(false))
->setObject(new \Application\Entity\EmailAddress());
$this->add($fieldset);
$fieldset = new \Application\Form\Fieldset\Person();
$fieldset
->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty(false))
->setObject(new \Application\Entity\Person());
$this->add($fieldset);
}
}
?>
Person (fieldset)
<?php
namespace Application\Form\Fieldset;
use Zend\Form\Fieldset;
class Person extends Fieldset
{
public function __construct()
{
parent::__construct('Person');
$this->add(array(
'type' => 'date',
'name' => 'DateOfBirth',
'required' => false,
'allowEmpty' => true,
'options' => array(
'label' => 'Date of birth',
'column-size' => 'sm-9',
'label_attributes' => array(
'class' => 'col-sm-3',
),
'format' => 'd-m-Y',
),
));
}
}
?>
'required' isn't an attribute of element but an validator attribute.
The solution consist to implement Zend\InputFilter\InputFilterProviderInterface
use Zend\InputFilter\InputFilterProviderInterface;
class UserManagementForm extends AbstractSbmForm implements InputFilterProviderInterface {
public function __construct()
{
... without change
}
public function getInputFilterSpecification()
{
return array(
'DateOfBirth' => array(
'name' => 'DateOfBirth',
'required' => false,
);
);
}
}
I have made a form collection based on the one in the manual. Then I have made a select that is populated from a database and also based on the example from the manual. Both works by them self but now I want to use the select in the form collection and then I get the following error:
Catchable fatal error: Argument 1 passed to
Application\Form\ClientSelectFieldset::__construct() must be an instance of
Application\Model\ClientTable, none given, called in
/home/path/vendor/zendframework/zendframework/library/Zend/Form/FormElementManager.php
on line 174 and defined in
/home/path/module/Application/src/Application/Form/ClientSelectFieldset.php on line 9
It feels like the init() isn't run correctly or something, is there something special I need to do get my select to work in form collections?
Module.php
public function getFormElementConfig() {
return array(
'factories' => array(
'Application\Form\ClientSelectFieldset' => function($sm) {
$serviceLocator = $sm->getServiceLocator();
$clientTable = $serviceLocator->get('Application\Model\ClientTable');
$fieldset = new ClientSelectFieldset($clientTable);
return $fieldset;
}
)
);
}
src/Application/Form/ClientFieldset.php
<?php
namespace Application\Form;
use Application\Model\ClientTable;
use Zend\Form\Fieldset;
class ClientSelectFieldset extends Fieldset {
public function __construct(ClientTable $clientTable) {
parent::__construct('clientselectfieldset');
$options = array();
$options[] = array('value' => 0, 'label' => "Select client");
$options[] = array('value' => -1, 'label' => "---------------", 'disabled' => 'disabled');
foreach($clientTable->fetchAll() as $clientRow) {
$options[] = array('value' => $clientRow->id, 'label' => $clientRow->name);
}
$this->add(array(
'name' => 'id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Client',
'options' => $options,
),
));
}
public function getInputFilterSpecification() {
return array(
'id' => array(
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)
);
}
}
src/Application/Form/InvoiceFieldset.php
<?php
namespace Application\Form;
use Application\Model\Invoice;
use Zend\Form\Fieldset;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class InvoiceFieldset extends Fieldset {
... more code ...
public function init() {
$this->add(array(
'name' => 'client',
'type' => 'Application\Form\ClientSelectFieldset'
));
}
}
src/Application/Form/InvoiceForm.php
<?php
namespace Application\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class InvoiceForm extends Form {
public function __construct() {
parent::__construct('invoice-form');
... more code ...
$this->add(array(
'type' => 'Application\Form\InvoiceFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
... more code ...
}
}
The solution was simple, i had to move the declaration of Application\Form\InvoiceFieldset in the class InvoiceForm from the __construct() to the init() function.
src/Application/Form/InvoiceForm.php
<?php
namespace Application\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class InvoiceForm extends Form {
public function __construct() {
parent::__construct('invoice-form');
... more code ...
}
public function init() {
$this->add(array(
'type' => 'Application\Form\InvoiceFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
}
}
I'm trying to write my first form in ZF2 and my code is
namespace Frontend\Forms;
use Zend\Form\Form;
use Zend\Validator;
class Pagecontent extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('logo');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'content_yes_no',
'type'=>'text',
'required' => true,
'validators' => array(
'name' => 'Alnum',
'options'=>array(
'allowWhiteSpace'=>true,
),
),
));
}
}
I want to know can I set validators like this?
Please advice
You've got to surround validators by another array:
'validators' => array(
array(
'name' => 'Alnum',
'options' => array(
'allowWhiteSpace'=>true,
),
),
),
To setup filters and validators you need an inputFilter. Typically you will find the inputFilter defined in the form class or associated model class. Here is a template for a form.
<?php
/* All bracket enclosed items are to be replaced with information from your
* implementation.
*/
namespace {Module}\Form;
class {Entity}Form
{
public function __construct()
{
// Name the form
parent::__construct('{entity}_form');
// Typically there is an id field on the form
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
// Add a csrf field to help with security
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf'
));
// Add more form fields here
$this->add(array(
'name' => 'example',
'type' => 'Text',
'options' => array(
'label' => 'Example',
),
));
//Of course we need a submit button
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Submit',
'id' => 'submitbutton',
),
));
}
}
The form defines all of the elements that will be displayed in the form. Now, you can either create the inputFilter in the form's class or in a model that is associated with the form's class. Either way it would look like:
<?php
/* All bracket enclosed items are to be replaced with information from your
* implementation.
*/
namespace {Module}\Model;
/*
* Include these if you require input filtering.
*/
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class {Model} implements InputFilterAwareInterface
{
/*
* Add in model members as necessary
*/
public $id;
public $example;
/*
* Declare an inputFilter
*/
private $inputFilter;
/*
* You don't need a set function but the InputFilterAwareInterface makes
* you declare one
*/
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
/*
* Put all of your form's fields' filters and validators in here
*/
public function getInputFilter()
{
if (!$this->inputFilter)
{
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
// This example input cannot have html tags in it, is trimmed, and
// must be 1-32 characters long
$inputFilter->add($factory->createInput(array(
'name' => 'example',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 32,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Then when you are programming your controller's action you can bring it all together like this:
if($request->isPost())
{
$model = new Model;
$form->setInputFilter($model->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid())
{
// Do some database stuff
}
}
Notice that we get the inputFilter from the model and use the form's setInputFilter() method to attach it.
To summarize, You must create a form class to place all of your form elements in, then create an inputFilter to hold all of your filters and validators. Then you can grab the inputFilter in the controller and apply it to the form. Of course this is just a couple ways to skin a cat though.
You can use Input Filter component:
<?php
namespace Frontend\Forms;
use Zend\Form\Form;
use Zend\Validator;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
class Pagecontent extends Form
{
public function __construct($name = null)
{
...
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'content_yes_no',
'required' => true,
'filters' => array(),
'validators' => array(
array(
'name' => 'Alnum',
'options' => array(
'allowWhiteSpace' => true,
),
),
),
)));
$this->setInputFilter($inputFilter);
}
}
// your controller
$form = new \Frontend\Forms\Pagecontent();
$form->setData($request->getPost());
if ($form->isValid()) {
// your code
}
I know this sounds much more basic, still I want to post my question as it is related to Zend Framework 2. I know this form from the Zend example module
namespace Album\Form;
use Zend\Form\Form;
class AlbumForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('album');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'name' => 'artist',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Artist',
),
));
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Title',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
And this is called in this fashion
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
How can I add a drop down list for the artist field where the list is stored in an associative array. Since Im getting into Zend Framework 2, I wanted the suggestions from the experts. I have followed this previous post but it was somewhat unclear to me.
This is one way to do it for static options.
....
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'number'
'options' array(
'options' => array( '1' => 'one', '2', 'two' )
)
));
Be warned....
Because you are creating the form within a constructor you will not have access the ServiceManger. This could cause a problem if you want to populate from a database.
Lets try something like...
class AlbumForm extends Form implements ServiceManagerAwareInterface
{
public function __construct()
{
....
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'number'
));
....
}
....
public function initFormOptions()
{
$this->get('number')->setAttribute('options', $this->getNumberOptions());
}
protected function getNumberOptions()
{
// or however you want to load the data in
$mapper = $this->getServiceManager()->get('NumberMapper');
return $mapper->getList();
}
public function getServiceManager()
{
if ( is_null($this->serviceManager) ) {
throw new Exception('The ServiceManager has not been set.');
}
return $this->serviceManager;
}
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
}
But that's not great, rethink...
Extending the Form so that you can create a form isn't quite right. We are not creating a new type of form, we are just setting up a form. This calls for a factory. Also, the advantages of using a factory here are that we can set it up in a way in which we can use the service manager to serve it up, that way the service manager can inject itself instead of us doing in manually from the controller. Another advantage is that we can invoke this form whenever we have the service manager.
Another point worth making is that where it makes sense, I think it's better to take code out of the controller. The controller is not a script dump so it's nice to have objects look after themselves. What I'm trying to say is that it's good to inject an object with objects it needs, but it's not okay to just hand it the data from the controller because it creates too much of a dependency. Don't spoon feed objects from the controller, inject the spoon.
Anyway, too much rant more code...
class MySpankingFormService implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceManager )
{
$mySpankingNewForm = new Form;
// build that form baby,
// you have a service manager,
// inject it if you need to,
// otherwise just use it.
return $mySpankingNewForm;
}
}
controller
<?php
class FooController
{
...
protected function getForm()
{
if ( is_null($this->form) ) {
$this->form =
$this->getServiceManager()->get('MySpankingFormService');
}
return $this->form;
}
...
}
module.config.php
...
'service_manager' => array (
'factories' => array (
...
'MySpankingFormService'
=> 'MyNameSpacing\Foo\MySpankingFormService',
...