ZF2: Extend a Form View Helper and make it available from view - zend-framework2

I need to render a Multicheck options like this:
<input type="checkbox" name="avoid_transport[voiture]" value="1">
<input type="checkbox" name="avoid_transport[plane]" value="1">
...
but I achieve to render like:
<input type="checkbox" name="avoid_transport[]" value="voiture">
<input type="checkbox" name="avoid_transport[]" value="plane">
Following appreciate guide, I need to create a custom view extending the Multicheck View Helper, but I am having problems.
This is my extended class:
namespace Multicity\Helper;
use Zend\Form\Element\MultiCheckbox as MultiCheckboxElement;
class FormMultiCheckboxArray extends MultiCheckboxElement
{
public function render()
{
/* do my own render here or render_options */
return;
}
}
I make it available through the view helper manager:
// module.config.php
'view_helper' => array(
'invokables' => array(
'formMultiCheckboxArray' => 'Multicity\Helper\FormMultiCheckboxArray'
)
),
I create the form element using the extended class:
$this->add(array(
'type' => 'Multicity\Helper\FormMultiCheckboxArray',
'name' => 'avoid_transport',
'options' => array(
'value_options' => array(
'0' => 'voiture',
'1' => 'avion',
),
),
'attributes' => array(
'value' => 1,
)
));
and at the view:
<?php echo $this->formMultiCheckboxArray($form->get('avoid_transport')); ?>
And I get this error message:
Zend\View\HelperPluginManager::get was unable to fetch or create an instance for formMultiCheckboxArray
Please, anybody could help me to figure out what I am missing?
I will appreciate any kind of guide. Thank for your time.
Regards.

Related

How to add class to fieldset?

I have a form in ZF2 with the following element being added:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));
And in my view script I have the folloing line:
<?php echo $this->formrow($form->get('animals')); ?>
Which is generating the following html:
<fieldset>
<legend>Favourite Animal</legend>
<label><input type="radio" name="animals" id="animals" class="form-control input-error" value="cat">Cat</label>
<label><input type="radio" name="animals" class="form-control input-error" value="dog">Dog</label>
<label><input type="radio" name="animals" class="form-control input-error" value="fish">Fish</label>
</fieldset>
How do I add a class to the fieldset?
I have tried adding the following to the options array, the attributes array, and as an option to the main array but it is not adding the class to the fieldset:
'fieldset_attributes' => array(
'class' => 'form-group',
),
[edit]
Looking into the code (\Zend\Form\View\Helper\FormRow::render) I've found this:
...
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox' || $type === 'radio' || $element instanceof MonthSelect ) {
$markup = sprintf('<fieldset><legend>%s</legend>%s</fieldset>', $label, $elementString);
}
...
Which means the only way to add a class to the fieldset (or legend if you wanted) is to extend the view helper.
I followed the answer as posted here (https://stackoverflow.com/a/27273068/351785).
From the answer (modified to suit my requirements):
Create the Application\Form\View\Helper\FormRow.php helper class like
below:
<?php
/**
* Extend zend form view helper formrow to allow class to be added to fieldset / legend
*/
namespace Application\Form\View\Helper;
use Zend\Form\View\Helper\FormRow as ZendFormRow;
class FormRow extends ZendFormRow
{
/**
* Utility form helper that renders a label (if it exists), an element and errors
*
* #param ElementInterface $element
* #throws \Zend\Form\Exception\DomainException
* #return string
*/
public function render(\Zend\Form\ElementInterface $element)
{
//... other code here
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox'
|| $type === 'radio'
|| $element instanceof MonthSelect
) {
$fieldset_class = $legend_class = '';
if($class = $element->getOption('fieldset_class')) {
$fieldset_class = sprintf(' class="%s"', $class);
}
if($class = $element->getOption('legend_class')) {
$legend_class = sprintf(' class="%s"', $class);
}
$markup = sprintf(
'<fieldset%s><legend%s>%s</legend>%s</fieldset>',
$fieldset_class,
$legend_class,
$label,
$elementString);
}
//... other code here
return $markup;
}
}
And override the factory in the onBootstrap() method of the Module.php
file like below:
namespace Application;
use Zend\Mvc\MvcEvent;
use Zend\View\HelperPluginManager;
class Module
{
/**
* On bootstrap for application module.
*
* #param MvcEvent $event
* #return void
*/
public function onBootstrap(MvcEvent $event)
{
$services = $event->getApplication()->getServiceManager();
// The magic happens here
$services->get('ViewHelperManager')->setFactory('formrow', function (HelperPluginManager $manager) {
return new \Application\Form\View\Helper\FormRow();
});
}
}
And add the classes as such:
$this->add(array(
'name' => 'animals',
'type' => 'radio',
'attributes' => array(
'id' => 'animals',
'class' => 'form-control',
),
'options' => array(
'label' => 'Favourite animal',
'fieldset_class' => 'form-group', //<== this
'legend_class' => 'form-legend', //<== and this
'options' => array(
'cat' => 'Cat',
'dog' => 'Dog',
'fish' => 'Fish',
),
),
));

extending zfcuser with custom radio fields

i am using ZFcuser with zendframework 2 and want to extend the registration form to enable me to add a radio field.
i followed the tutorial here
it works ok and i am able to render and obtain the values for imput tags. the problem comes when i try to render a radio button. it only renders one of the values.
here is my code:
public function onBootstrap(MVCEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$em = $eventManager->getSharedManager();
$em->attach(
'ZfcUser\Form\RegisterFilter',
'init',
function($e)
{
$filter = $e->getTarget();
$filter->add(array(
'name' => 'accept',
'required' => FALSE
));
}
);
// custom form fields
$em->attach(
'ZfcUser\Form\Register',
'init',
function($e)
{
/* #var $form \ZfcUser\Form\Register */
$form = $e->getTarget();
$form->add(
array(
'name' => 'username',
'options' => array(
'label' => 'Username',
),
'attributes' => array(
'type' => 'text',
),
)
);
$form->add(array(
'type' => 'Zend\Form\Element\radio',
'name' => 'terms',
'options' => array(
'label' => 'Accept Terms',
'value_options' => array(
'1' => 'Yes',
'0' => 'No',
),
),
));
}
);
$zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
$zfcServiceEvents->attach('register', function($e) {
$form = $e->getParam('form');
$user = $e->getParam('user');
the HTML
the html from this output only render 1 input tag; i.e
<input type="radio" value="" name="terms">
however, i need to render two radio buttons; one for the yes, and one for the no
does anyone have any ideas how to do this; i really appreciate your help
This is a limitation of the current ZfcUser(v. 1.x), but is something that will be fixed in the coming version(v. 2.x).
In the meantime: overwrite the standard view with your own.

zf2 Fieldset ignores naming

I have the following problem:
I created a fieldset and included it to the form using the following add statement:
// get dynamic additionals fields
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'additionals',
'options' => array(
'label' => 'Please choose categories for this product',
'count' => 3,
'should_create_template' => true,
'allow_add' => true,
'template_placeholder' => '__index__',
'use_as_base_fieldset' => true,
'target_element' => array(
'type' => 'Tool\Form\DeductionStepTwoAddForm'
)
)
));
Now what ZF2 does is generate the following:
<span data-template="<fieldset><legend>additional</legend><input type="hidden" name="additional_checked" value="0"><input type="checkbox" name="additional_checked" value="1"><input name="additional_name" type="text" class="validate" value=""><input name="additional_cost" type="text" class="validate" value=""><select name="additional_key"><option value="m2">m²</option>
<option value="mea">MEA</option>
<option value="pers">Pers.</option>
<option value="m3">m³ / Verbrauch</option>
<option value="units">Einheiten</option></select></fieldset>"></span>
This is how the Fieldset is created:
class DeductionStepTwoAddForm extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('additional');
$this->setLabel('additional');
$selectVal = array(
'm2' => 'm²',
'mea' => 'MEA',
'pers' => 'Pers.',
'm3' => 'm³ / Verbrauch',
'units' => 'Einheiten',
);
The problem: Check the name's of the form elements -> they are not indexed and will get overwritten. What am I doing wrong?
Solution: We forgot to prepare the form in the view before generating the output.

dynamic dropdown get in zf2?

I want to put a dropdown in my project which is made in zf2... I wasted all day but I only got a static dropdown, not dynamic. Can anyone help me with this problem??
UserForm.php
$this->add(array(
'name' => 'group_name',
'type' => 'select',
'attributes' => array(
'id'=>'group_name',
'class'=>'large',
'options' => array('1=>php','2'=>'java'),
),
'options' => array(
'label' => '',
),
));
Thanks in advance for your valuabe answer.
Try this:
$this->add(array(
'name' => 'group_name',
'type' => 'select',
'attributes' => array(
'id'=>'group_name',
'class'=>'large',
),
'options' => array(
'label' => '',
'value_options' => array(
'1' => 'php',
'2' => 'java'
),
),
));
This is what i did:
In my constructor for my form
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'color',
'options' => array(
'empty_option' => 'Select a Color',
'value_options' => self::getColors(),
'label' => 'Color',
),
));
In the form class yet, i created this method:
public static function getColors() {
// access database here
//example return
return array(
'blue' => 'Blue',
'red' => 'Red',
);
}
In my view script:
<div class="form_element">
<?php $element = $form->get('color'); ?>
<label>
<?php echo $element->getOption('label'); ?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
Think about it from a abstract level.
You have one Form
The Form needs Data from the outside
So ultimately your Form has a Dependency. Since we've learned from the official docs, there's two types of Dependency-Injection aka DI. Setter-Injection and Constructor-Injection. Personally(!) i use one or the other in those cases:
Constructor-Injection if the dependency is an absolute requirement for the functionality to work
Setter-Injection if the dependencies are more or less optional to extend already working stuff
In the case of your Form, it is a required dependency (because without it there is no populated Select-Element) hence i'll be giving you an example for Constructor-Injection.
Some action of your controller:
$sl = $this->getServiceLocator();
$dbA = $sl->get('Zend\Db\Adapter\Adapter');
$form = new SomeForm($dbA);
That's all for the form. The population now happens inside your Form. This is only an example and may need some fine-tuning, but you'll get the idea:
class SomeForm extends \Zend\Form
{
public function __construct(\Zend\Db\Adapter\Adapter $dbA)
{
parent::__construct('my-form-name');
// Create all the form elements and stuff
// Get Population data
$popData = array();
$result = $dbA->query('SELECT id, title FROM Categories', $dbA::QUERY_MODE_EXECUTE)->toArray();
foreach ($result as $cat) {
$popData[$cat['id'] = $cat['title'];
}
$selectElement = $this->getElement('select-element-name');
$selectElement->setValueOptions($popData);
}
}
Important: I HAVE NO CLUE ABOUT Zend\Db the above code is only for how i think it would work going by the docs! This is the part that would need some optimization probably. But all in all you'll get the idea of how it's done.
In your controller you can do something like below;
On my first example assuming that you have a Group Table. Then we're going to fetchAll the data in group table;
We need the id and name to be display in select options;
public function indexAction()
{
$groupTable = new GroupTable();
$groupList = $groupTable->fetchAll();
$groups = array();
foreach ($groupList as $list) {
$groups[$list->getId()] = $list->getName();
}
$form = new UserForm();
$form->get('group_name')->setAttributes(array(
'options' => $groups,
));
}
OR
in this example the grouplist is hardcoded;
public function indexAction()
{
$groupList = array('1' => 'PHP', '2' => 'JAVA', '3' => 'C#');
$groups = array();
foreach ($groupList as $id => $list) {
$groups[$id] = $list;
}
$form = new UserForm();
$form->get('group_name')->setAttributes(array(
'options' => $groups,
));
}
Then in your view script;
<?php
$form = $this->form;
echo $this->formRow($form->get('group_name'));
?>
Or you can right a controller helper, you may check this link http://www.resourcemode.com/me/?p=327
Just came across the same problem and had to take a look into zf2 source.
Here's a more OOP solution:
Inside the form constructor:
$this->add(array(
'name' => 'customer',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'options' => array(
0 => 'Kunde',
)
),
'options' => array(
'label' => 'Kunde'
)));
inside the controller:
$view->form = new SearchForm();
$customers = $view->form->get('customer')->getValueOptions();
$customers[] = 'Kunde1';
$customers[] = 'Kunde2';
$customers[] = 'Kunde3';
$customers[] = 'Kunde4';
$view->form->get('customer')->setValueOptions($customers);

How to change the behavior of sfWidgetFormSelectRadio in symfony?

new sfWidgetFormSelectRadio(
array('choices' => $images)));
The above will render each option something like:
<input type="radio" name="name" value="image_path">
How to make it render this way with minimal code:
<input type="radio" name="name" value="image_path"><img src="image_path" />
This is untested and straight from me reading the Symfony API docs for that widget. You'll need to extend the sfWidgetFormSelectRadio class, call it something like myWidgetFormSelectRadio and stick it in lib/widget/myWidgetFormSelectRadio.class.php in your project.
Override the formatChoices() method like so:
class myWidgetFormSelectRadio extends sfWidgetFormSelectRadio
{
protected function formatChoices($name, $value, $choices, $attributes)
{
$inputs = array();
foreach ($choices as $key => $option)
{
$baseAttributes = array(
'name' => substr($name, 0, -2),
'type' => 'radio',
'value' => self::escapeOnce($key),
'id' => $id = $this->generateId($name, self::escapeOnce($key)),
);
if (strval($key) == strval($value === false ? 0 : $value))
{
$baseAttributes['checked'] = 'checked';
}
$inputs[$id] = array(
'input' =>
$this->renderTag('input', array_merge($baseAttributes, $attributes))
. $this->renderTag('img', array('src' => self::escapeOnce($key))),
'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
);
}
return call_user_func($this->getOption('formatter'), $this, $inputs);
}
}
so you're basically appending the img tag to the input.
In your form's configure() method you'll then need to switch from using sfWidgetFormSelectRadio to using myWidgetFormSelectRadio to use the new widget.
Let me know if this works ;-)

Resources