How to put a picture instead of text in radio element in ZF2? - zend-framework

Subject: How to put a picture instead of text in radio element in ZF2?
code:
Options field:
array(
'spec' => array(
'type' => 'radio',
'name' => 'fcaptcha',
'options' => array(
'label' => 'Капча',
'label_attributes' => array(
'class' => 'control-label',
),
),
'attributes' => array(
'required' => 'required',
),
),
)
Controller:
$temp[0] = 'Here you need to put a picture instead of text';
$temp[1] = 'Here you need to put a picture instead of text';
$form->get('fcaptcha')->setValueOptions($temp);

You can do that just with some CSS. Here's a simple example that you can adapt for your purpose :
Let say you have this Zend\Form\Element\Radio element :
array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'fcaptcha',
'options' => array(
'value_options' => array(
'0' => 'something1',
'1' => 'something2',
),
),
'attributes' => array(
'id'=>"fcaptcha-id"
)
)
CSS :
radio-option1 {background-image:url(option1.png);}
radio-option2 {background-image:url(option2.png);}
Some JS :
$(document).ready(function(){
$(':radio[value="0"]').addClass('radio-option1');
$(':radio[value="1"]').addClass('radio-option2');
});

Related

How to customize a BAD CAPTCHA message in Zend Framework 2 (ZF2)

I'm using an Image Captcha and I want to customize the "Captcha value is wrong" error message.
This is the definition of my Captcha:
$captcha = new ImageCaptcha();
$font = "./data/fonts/calibri.ttf";
$captcha->setFont($font);
$captcha->setDotNoiseLevel(55);
$captcha->setLineNoiseLevel(3);
In my form's constructor:
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => $this->captcha,
'messages' => array(
\Zend\Captcha\Image::BAD_CAPTCHA => 'super neat captcha message.',
),
),
));
I also tried:
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => $this->captcha,
'messages' => array(
\Zend\Captcha\AbstractWord::BAD_CAPTCHA => 'super neat captcha message.',
),
),
));
But I keep getting the "Captcha value is wrong" message.
The "message" part has to be in the captcha-options like this:
$this->add([
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => [
'label' => 'Please verify you are human.',
'captcha' => [
...
'messages' => [
\Zend\Captcha\Image::BAD_CAPTCHA => 'super neat captcha message.',
],
],
],
]);
At least it works for me with Zend Framework 3.
Adding captcha element:
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => $this->captcha
),
));
And error message:
$this->getInputFilter()->get('captcha')->setErrorMessage('super neat captcha message.');
It works for me.

Prestashop Module : Add multi select dropdown

I'm working on a module and I would like to know how to add multiple dropdown with fields_options.
$this->fields_options = array(
'Test' => array(
'title' => $this->l('Test'),
'icon' => 'delivery',
'fields' => array(
'IXY_GALLERY_CREATION_OCCASION' => array(
'title' => $this->l('DropdownList'),
'type' => 'select',
'multiple' => true , // not working
'identifier' => 'value',
'list' => array(
1 => array('value' => 1, 'name' => $this->l('Test 1 ')),
2 => array('value' => 2, 'name' => $this->l('Test 2)'))
)
),
),
'description' =>'',
'submit' => array('title' => $this->l('Save'))
)
);
This is how I'm doin if you're meaning that :
$combo = $this->getAddFieldsValues();
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Title'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'nom_matiere',
'options' => array(
'query' => $combo[0],
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'name',
'options' => array(
'query' => $combo[1],
'id' => 'id_option',
'name' => 'name'
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('updateData'),
)
),
);
the answer are not correctly .. due its not only dfined the field in the database, also must capture and stored in special way the values, in this example i demostrate to store as "1,2,3,6,8" using a single field
THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
here i put only the most important parts..
as mentioned int he previous link, added a new fiel in the model definition, class and the table sql
this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
then in the helper form list array of inputs define a select as:
at the begining dont foget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'select',
'label' => $this->l('Select and employee'),
'name' => 'id_employee_tech',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'options' => array(
'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
'id' => 'id_employee',
'name' => 'firstname',
'default' => array(
'value' => '',
'label' => $this->l('ninguno')
)
)
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"

Zend framework 2 how to add class to select input

How can I add a class to a select drop down list.
Below is my attempt to do it. I have tried to do it with the attributes but this doesn't appear to work with select tag.
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'jobId',
'options' => array(
'object_manager' => $this->getObjectManager(),
'target_class' => 'FormDependencies\Entity\JobList',
'property' => 'job',
'empty_option' => '--- please choose ---',
'attributes' => array(
'class' => 'testing',
)
),
)
);
Thank you in advance for your help.
below is the rendered input bar: you will see that the class has not been added;
<select name="Jobs[jobId]">
The attributes array should not be nested within options; but rather on the same level.
$this->add(array(
'name' => 'jobId',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'foo' => 'bar'
),
'attributes' => array(
'class' => 'testing',
),
));

Setting default checked value in ZendFramework2 to RADIO input (QuickStart form)

Unfortunately i don`t have the exact example here, but, is similar to this:
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender'
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
)
));
How can I set a default value to this element? I tried putting this, but didn't work:
'attributes' => array(
'value' => '0'
)
Thank's! And, sorry for my poor english! I need to improve it!
You should provide the value underoption, not attribute.
LIke this :
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender'
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
'value' => 10, // here
)
));
See this link:here
Radio is just an extension of MultiCheckbox
You will need to set the checked_value within the options array
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender', // <-- missing comma here also
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
'checked_value' => 10,
)
));
You can see this in the setOptions() method of Zend\Form\Element\Checkbox which the Radio element extends.
Your syntax is correct. value key in attributes is used to set the default checked value.
You just forgot to add a comma after 'name' => 'gender'
The following code will work for you
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender',
'attributes' => array(
'value' => '0',
),
'options' => array(
'label' => 'What is your gender?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
),
));

how to change the size property of a form text input?

1) I didn't find anything in CSS files in relation with the width of form input.
2) I tried this :
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
'size' => '14', <---this doesn't work!
),
'type' => 'Text',
),
),
without any result!
So, is it possible to change the width (size property) of a form text input in ZF2 ?
Thanks
Put size in attributes instead of options
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'size' => 14,
),
'type' => 'Text',
),
),
Thank you for your help but this way doesn't work for me.
Here is exactly my code :
$this->add(array(
'name' => 'name',
'attributes' =>array(
'type' => 'text',
'size' => '7', <--- this doesn't work
'maxlength'=> '7', <--- this works
'value' => 'Doe' <--- this works
),
'options' => array(
'label' => 'Your name',
),
));
However, "size", "maxlength" and "value" are equivalent : both are HTML attributes!
So I found in public/css/bootstrap.min.css :
input, textarea, .uneditable-input
{
width: 206px; <---change this value and it's ok
}
!!! Pay attention to refresh your browser !!!