Zend Framework 2 form radiobutton rendering exception - zend-framework2

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.

Related

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 Framework 2: How to properly replace Figlet with reCaptcha on zfcUser

I am trying to replace Figlet with reCaptcha on a zfcUser registration form. Partial instruction on how to accomplish this can be found on https://github.com/ZF-Commons/ZfcUser#changing-registration-captcha-element but no complete instruction exists.
Checking the README.md file has a two-step instruction on how to accomplish this but still the CAPTCHA uses Figlet when rendered on the form.
Has anyone successfully implemented this? I really need a hand on this one.
Thanks in advance.
EDIT: Here is a proven working solution I developed:
1. Add to composer.json
// Add the lines below under the "require" element:
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">2.2.0rc1",
"zendframework/zendservice-recaptcha": "2.*"
}
2. Goto to your project's ZF2 installation directory and execute this command:
php composer.phar update
3. Replace or Create config/autoload/database.global.php with:
<?php
$config = array(
'dbdriver' => 'pdo',
'dbhost' => 'localhost',
'dbport' => '3306',
'dbname' => 'CHANGEME',
'dbuser' => 'CHANGEME',
'dbpass' => 'CHANGEME',
);
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$config['dbname'].';host='.$config['dbhost'],
'username' => $config['dbuser'],
'password' => $config['dbpass'],
),
);
4: Execute this on your mySQL server:
CREATE TABLE `user`
(
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(255) DEFAULT NULL UNIQUE,
`email` VARCHAR(255) DEFAULT NULL UNIQUE,
`display_name` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(128) NOT NULL,
`state` SMALLINT UNSIGNED
) ENGINE=InnoDB CHARSET="utf8";
5. Create/Replace config/autoload/recaptcha.global.php with:
<?php
define('RECAPTCHA_PRIVATE_KEY','CHANGEME');
define('RECAPTCHA_PUBLIC_KEY','CHANGEME');
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
'di'=> array(
'instance'=>array(
'alias'=>array(
'recaptcha_element' => 'Zend\Form\Element\Captcha',
),
'ZfcUser\Form\Register' => array(
'parameters' => array(
'captcha_element'=>'recaptcha_element',
),
),
),
),
);
6. Create/Replace config/autoload/zfcuser.global.php with:
<?php
$settings = array(
'enable_registration' => true,
'enable_username' => true,
'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
'enable_display_name' => false,
'auth_identity_fields' => array( 'email' ),
'use_registration_form_captcha' => true,
'user_login_widget_view_template' => 'zfc-user/user/login.phtml',
);
return array(
'zfcuser' => $settings,
'service_manager' => array(
'aliases' => array(
'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter',
),
),
);
7. Navigate to http://yourdomain.com/user
8. Enjoy! :)
This is how I did it, it might not be the best or correct way but it worked for me:
Add the recaptcha service to your composer.json file:
"require": {
"Zendframework/zendservice-recaptcha": "2.*"
}
Run composer to get the service. Then you need to specify the ReCaptcha config.
I created a separate config file to store the ReCaptcha keys:
//zfcuser.local.php
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
);
Then ZfcUser captcha config looks like so, telling it to use the ReCaptcha service:
//zfcuser.global.php
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'wordLen' => 6,
'expiration' => 300,
'timeout' => 300,
),
),
Edit:
You don't need the recaptcha.global.php. You can call the config file whatever you like aslong as it ends with .global.php or .local.php. You usually name things .local.php when you don't want them in version control.
In this case I named the file zfcuser.local.php because all it does is store the ReCaptcha keys and I didn't want them in version control.
All the config files get merged in to one array when the application is started. So basically, ignore the ZfcUser documentation. Or maybe someone else can explain how to get it working that way.
The third block of code is the zfcuser.global.php.

zend framework2 How to echo out multiple checkbox separately

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">';

zf2 formFilter regex howto

Using zendframework v2, I have run into a problem with a regex validator on a field created by the Form factory. All other fields (using the same pattern) work without a problem.
Any tips, or pointers is appreciated.
$inputFilter->add($factory->createInput([
'name' => 'organizationName',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Organization name field is empty',
),
),
),
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[a-z0-9 &-_\.,#]{3,25}$/i',
'messages' => array(
\Zend\Validator\Regex::INVALID => 'Invalid input, only a-z, 0-9 & - _ . characters allowed',
),
),
),
array (
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => '2',
'max' => '25',
'messages' => array(
\Zend\Validator\StringLength::TOO_SHORT => 'Organization name field must be at least 8 characters in length',
\Zend\Validator\StringLength::TOO_LONG => 'Organization name field must be no longer than 25 characters in length',
),
),
),
),
]));
Additional details:
I am using the ZF2 to generate a form, I also create a validation filter and then use the controller to handle proper form submissions.
The problem I am having is with the above inputFilter object that handles the "organizationName" regex filter.
It seems that although the regex patter I use both in the form definition and the input filter of [a-z0-9 &-_.,#]{3,25} does not handle the string Intl. Widgets Inc. even though I do not get an error message from $form->getMessages() etc.
Color me stumpted
That's because your regexp does match Intl. Widgets Inc. : http://rubular.com/r/oPbk2cdarB

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