0

Since i've to position checkboxes in design I'm not using MultiCheckbox and using Checkbox instead in zend. I've seen some zf1 solutions but didnt found any zf2 or zf3 solutions.

My Php Code

$languages = new \Zend\Form\Element\Checkbox('languages[]');
$languages->setLabel('English');
$languages->setValue('1');

This produce the following output

<?php 
    echo $this->formRow($form->get('languages[]')); 
    //It produce the following
?>
<input type="checkbox" name="languages[]" value="1" checked="checked">

How can I add more items with name "languages[]" without writing direct HTML code ?

3
  • Hope, this answer would help you stackoverflow.com/a/47158149/7935051 Commented Nov 9, 2017 at 10:54
  • Just render the form row multiple times: $this->formRow($form->get('languages[]')); Commented Nov 11, 2017 at 23:09
  • @JannesBotis but its value is different for each checkbox. Commented Nov 14, 2017 at 11:42

3 Answers 3

1

You can use a Form collection for this:

1) In your form:

use Zend\Form\Element\Collection;

...

$languages = new \Zend\Form\Element\Checkbox();
$languages->setLabel('English');
$languages->setValue('1');
$languages->setName('language');

$collection = new Collection();
$collection->setName('languages');
$collection->setLabel('Language Collection');
$collection->setCount(2);
$collection->setTargetElement($languages);
$collection->populateValues(array(
        1, 0
));

$this->add($collection);

2) Do not forget to prepare your form in your controller action:

$form->prepare();

3) Finally, in your view, get all elements of the collection and render them separately:

<?php $elements = $form->get('languages')->getElements(); ?>
<?php //echo $this->formCollection($form->get('languages')); ?>
<?php echo $this->formRow($elements[0]); ?>
<?php echo $this->formRow($elements[1]); ?>
Sign up to request clarification or add additional context in comments.

3 Comments

can u please tell me how to add next language ?
If you want to use different labels for each checkbox, add $collection->getElements()[1]->setLabel('Spanish'); in your Form.
It didn't worked, also it look like when we call "$collection->getElements()[1]" we have retrieved the element object at that position, so setLabel might not update the variable $collection itself.
0

You can use MultiChebox

https://framework.zend.com/manual/2.2/en/modules/zend.form.element.multicheckbox.html

It works similar to radio form element. Your example implementation

use Zend\Form\Element;

$languages = new Element\MultiCheckbox('languages');
$languages->setLabel('Used languages');
$languages->setValueOptions([
    'en_US' => 'english',
    'de_DE' => 'german',
    'pl_PL' => 'polish',
]);
$languages->setValue('en_US'); //default value; you can use array

Comments

0

My solution (Method 2)

Method 1: Give name as "language[1]" instead of "language[]". By using this I can call the elements in view seperately.

Creating form.

$language1 = new \Zend\Form\Element\Checkbox('language[1]');
$language2 = new \Zend\Form\Element\Checkbox('language[2]');

$form = new Form('Set');
$form->add($name)
        ->add($language1)
        ->add($language2)
        ->add($submit);

In view file

<div><?php echo $form->get('language[1]');?></div>
<div><?php echo $form->get('language[2]');?></div>

Edit: Method 2 Using fieldset

//Create form
$languages = [
    ['id' => 1, 'language' => 'English'],
    ['id' => 2, 'language' => 'Malayalam'],
] ;
$fieldSet = new \Zend\Form\Fieldset('languages') ;
foreach( $languages as $one ) {
    $c = new \Zend\Form\Element\Checkbox($one['id']);
    $c->setLabel($one['language']) ;            
    $fieldSet->add($c) ;
}
//Add collection of checkboxes to form
$form->add($fieldSet) ;        

In view file

<?php
$language = $form->get('languages') ;
?>        
<div class="form-group row">
    <label class="control-label col-sm-2" >Choose Languages</label>
    <div class="col-sm-10"> 
        <?php foreach($language as $one ) { ?>
        <?php echo $this->formCheckbox($one); ?> <span> <?php echo $this->formLabel( $one ) ; ?> </span>
        <?php echo $this->formElementErrors($one); ?>
        <?php } ?>
    </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.