zend framework2 How to echo out multiple checkbox separately - zend-framework2

I am using Zend MultiCheckbox form element
$this->add(array(
'type' => 'Zend\Form\Element\MultiCheckbox',
'name' => 'conferenceoption',
'options' => array(
'value_options' => array(
'conference Program' => 'Conference',
'Evening Program' => 'Evening Programme',
),
),
'attributes' => array(
'value' => '1' //set selected to '1'
)
));
When I echo them out in my view with,
echo $this->formMultiCheckbox($form->get('conferenceoption'));
Then, I getting something like this,
But What I would really like to have is,
THanks in advance...

I got the answer sorry for this.......
We can use,
echo '<input type="checkbox" name="conferenceoption[]" value="conference Program">';
echo '<br>';
echo '<input type="checkbox" name="conferenceoption[]" value="Evening Program">';

Related

Zend Framework 2 form radiobutton rendering exception

I have the following problem.
I have a ZF2 (Version 2.5.1) form including this:
// Gender
$this->add(array(
'name' => 'gender',
'type' => 'radio',
'options' => array(
'label' => 'Gender',
'value_options' => array(
'1' => 'Male',
'2' => 'Female'
)
)
));
And attempting to render it in a view like this:
echo "<div>";
echo $this->formLabel($form->get('gender')) . "\n";
echo $this->formRow($form->get('gender'));
echo "</div>";
Which gives me the following exception (thrown via the call to formRow):
Zend\Form\View\Helper\FormMultiCheckbox::render requires that the element is of type Zend\Form\Element\MultiCheckbox
With the exception being thrown at line #103 in the file zend-form\src\View\Helper\FormMultiCheckbox.php.
Is there anyone else that have had this problem? Am I doing something horribly wrong here?
Thanks in advance.

zf2 default validation messages not overriding

$inputFilter->add(array(
'name' => 'nightCharges',
'required' => FALSE,
'filters' => array(
array('name' => 'Digits'),
),
'validators' => array(
array(
'name' => 'GreaterThan',
'options' => array(
'min' => 1,
'messages' => array(
\Zend\Validator\GreaterThan::NOT_GREATER => 'My message',
),
),
),
),
));
Above is my code snippet for validation in this case for 'check box'. If I set 'required' to "true" and submit empty form, it shows me the default 'Value is required and can't be empty' message and if I set 'required' to 'false' it does't show me any error at all. Even if I submit non digit value it doesn't show me actual error message.
Where am I making mistake?
Actually I was working on 'Please accept the terms & conditions' check box.
Rather than using the GreaterThan validator, try using Identical to ensure the value can't be anything other than 1.
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => '1',
'messages' => array(
Identical::NOT_SAME => 'Please accept the terms & conditions.',
),
),
),
),

Zend framework 2 translate Image captcha messages

I have this Image captcha thats is working correctly, but I can't get the "badCaptcha" validation error translated.
I have the key Captcha value is wrong translated in my .po file with PoEdit.
This is my CAPTCHA form element:
$this->form->add(array(
'name' => 'captcha',
'type' => 'Zend\Form\Element\Captcha',
'options' => array(
'captcha' => new \Zend\Captcha\Image(array(
'imgDir' => './public/assets/images/captcha',
'ImgUrl' => '/assets/images/captcha',
'width' => 330,
'height' => 90,
'wordlen' => 3,
'dotNoiseLevel' => 30,
'lineNoiseLevel' => 3,
'font' => './data/captcha/font/monofont.ttf',
'fontSize' => 52,
'expiration' => 600,
)),
'messages' => array(
'badCaptcha' => $this->getTranslatorHelper()->translate('Captcha value is wrong', 'csnuser'),
),
),
));
PS: $this->getTranslatorHelper() retrieves the MvcTranslator service.
This is all wrong...ZF2 documentation in this topic is at least innacurate.
You may activate this very basic function this way:
In your Application (yes, I'm using Skeleton App) module config file (module.config.php)
...
...
'translator' => array(
'locale' => 'xx_XX', //Or whatever you want
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
// Add this new file pattern
array(
'type' => 'phparray',
//You get this translated files from vendor/zendframework/zendframework/resources/languages
'base_dir' => __DIR__ . '/../language/validation',
//You may rename it to xx_XX.php for the pattern to match!
'pattern' => '%s.php',
),
...
...
...
After that, you may create Application module onBootstrap event listener in Module.php file, like this:
public function onBootstrap(MvcEvent $event)
{
...
...
\Zend\Validator\AbstractValidator::setDefaultTranslator($event->getApplication()->getServiceManager()->get('MvcTranslator'));
...
...
}
This way I got Captcha value is wrong translated!
ZF2 has already translate all forms messages zend_validate.php and captacha_validate.php are already translated them
you can find them here :
vendor\zendframework\zendframework\resources\languages\fr
Copy this files into your application langage folder and call them into your config
'translator' => array(
'locale' => 'fr_FR',
'translation_files' => array(
array(
'type' => 'phpArray',
'filename' => 'resources/languages/fr.php'
),
),
),
for example
You can have both php translation file AND po file.
EDIT :
'translator' => array(
'locale' => 'fr_FR',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo', //<-%s is important
),
array(
'type' => 'phpArray',
'base_dir' => './module/Application/language/Zend_Validate/',
'pattern' => '%s-Zend_Validate.php',
),
),
),
When you have multiple lang to handle you have to load different file according to the lang choosen. %s take 'locale'.
Check if it's your problem :)
No renaming nescecary:
'pattern' => '%2.2s/Zend_Validate.php'

Zend\Navigation breadcrumb doesn't find active elements

i've got my navigation running and all links are working fine. Rendering the Navigation using $this->navigation($nav)->menu() will display an unordered list and all links are working.
Additionally the active link there is working, too. The active element has class="active" as an attribute.
Rendering the same navigation, only as a bradcrumb $this->navigation($nav)->breadcrumbs() doesn't render me anything. This MAY be due to my navigation only being one level deep for now, but imo the first level should still be rendered.
Using super modern die()-debugging i found out that nothing get's rendered because the findActive() of the viewHelper doesn't find an active element and therefore returns an empty string.
Any thoughts on where my error may be located? Any insight will be greatly appreciated. Here's my code so far:
'navigation' => array(
'default' => array(
'biete' => array(
'label' => 'Biete',
'route' => 'biete',
),
'suche' => array(
'label' => 'Suche',
'route' => 'suche',
),
'administration' => array(
'label' => 'Administration',
'route' => 'admin'
),
'dashboard' => array(
'label' => 'Meine Artikel',
'route' => 'dashboard'
),
'login' => array(
'label' => 'Anmelden',
'route' => 'duituser/login'
),
'logout' => array(
'label' => 'Abmelden',
'route' => 'duituser/logout'
)
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
And the view Parts located in my layout.phtml
<?php echo $this->navigation('navigation')->menu(); ?>
<?php echo $this->navigation('navigation')->breadcrumbs(); ?>
Thanks again in advance.
I had a play with the Navigation classes/helper - it seems that breadcrumbs are rendered (in the example below) when the 'playground' route is matched. However by adding 'active' => true to my default route means it will always be rendered by the breadcrumb helper.
'navigation' => array(
'default' => array(
'test' => array(
'label' => 'Home',
'route' => 'test',
'active' => true,
'pages' => array(
'playground' => array(
'label' => 'Playground',
'route' => 'playground',
),
),
),
),
),
In addition to the answer of DrBeza, here is what helped me. The problem was that the Breadcrumb-Navigation only got rendered once we hit the second level. But you can obviously change that setting by calling the following:
$this->navigation('navigation')->breadcrumbs()->setMinDepth(0);
To me, this is all that was needed. As DrBeza pointed out however in some cases this may not be enough.
In my case helps only this this:
$this->navigation('navigation')->breadcrumbs()->render('navigation');
The strange is, that it's required only if $this->navigation('navigation')->menu() is called before, because breadcrumbs are not rendered.

Zend Framework 2: Trying to add a selectbox to a form does not render values

I am trying to add a selectbox to one of my forms (which just with input type="text" elements are working pretty good) but all I get is just an empty selectbox with none tags in it. So this is the code I use:
Bla.php :: Bla->getInputFilter()
$inputFilter->add($factory->createInput(array(
'type' => 'Zend\InputFilter\Select',
'name' => 'payment_type',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
BlaForm.php :: BlaForm->__construct():
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'payment_type',
'options' => array(
'label' => 'Payment',
'value_options' => array(
0 => 'Nur Überweisung',
1 => 'Nur Paypal',
2 => 'Nur Barzahlung im Voraus',
),
),
'attributes' => array(
'value' => 0 //set selected to "Nur Überweisung"
)
));
bla.php (View)
<div class="control-group">
<?php
echo $this->formLabel($form->get('payment_type')->setLabelAttributes(array(
'class' => 'control-label'
)));
?>
<div class="controls">
<?=$this->formElement($form->get('payment_type'));?>
<span class="help-inline"><?=$this->formElementErrors($form->get('payment_type'));?></span>
</div>
</div>
I already tried using "options" instead of "value_options" and yesterday I learned that it is just an alias of "value_options". Also I tried formSelect() instead of formElement() in my view but that doesn't change anything either. I even removed the umlauts from the strings for testing purposes...
Did anybody experience the same problem or has any idea, what I am currently doing wrong?
I just tried your examples locally against current master (rev 9747bd01d), and they worked without issue -- using either formCollection() on the form, or formElement() or formSelect() on the individual element. In each case, I get the following markup:
<select name="payment_type"><option value="0" selected="selected">Nur Überweisung</option>
<option value="1">Nur Paypal</option>
<option value="2">Nur Barzahlung im Voraus</option></select>
What version of ZF2 are you using? Can you test against either 2.0.2 or current master, please?
I found the solution myself. In BlaForm.php the format of the selectbox element has to be as follows:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'payment_type',
'options' => array(
'label' => 'Bezahlung',
),
'attributes' => array(
'options' => array(
0 => 'Nurerweisung',
1 => 'NurPaypal',
2 => 'NurBarzahlung im Voraus',
3 => 'NurBarzahlung am Bus',
),
'value' => 2 //set selected to "public"
)
));
The "options" and "value" need to be nested unter "attributes"... well yeah, why not? I found out by looking deeper inter Zend\Form\Element\Select where a method "getOptionAttributeValues()" exists, which gave me the hint.

Resources