zendframework 2 form populating checkbox values from a database - forms

I am using zendframework 2 and doctrine 2. I want to populate the values of my checkboxes from values in my database (dependence injection).
I got the technique from: https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md
This is my element (it works for select elements but not for checkboxes):
$this->add(array(
'type' => 'Zend\Form\Element\MultiCheckbox',
'name' => 'timesId',
'options' => array(
'label' => 'Please Select Your Availablity',
'value_options' => array(
'object_manager' => $this->getObjectManager(),
'target_class' => 'FormDependencies\Entity\AvailablityTimeTableList',
'property' => 'job',
),
),
'attributes' => array(
'value' => '1' //set checked to '1'
)
));
public function getObjectManager()
{
return $this->objectManager;
}
I cannot find the native doctrine 2 method for checkboxes.
The error message:
Fatal error: Cannot use object of type Doctrine\ORM\EntityManager as array

i have resolved it;
under type i needed to specify that its a :
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
the complete code:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'timesId',
'options' => array(
'label' => 'Please Select Your Availablity',
'object_manager' => $this->getObjectManager(),
'target_class' => 'FormDependencies\Entity\AvailablityTimeTableList',
'property' => 'times',
'empty_option' => '--- please choose ---'
),

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.

How to build a single 'flag' checkbox in prestashop with form helpers?

I can't figure out from official docs how to build a single checkbox element from the standard helpers. I already have the relevant boolean entity in database and I can build radios or selects as well for it, and they work.
But what I'd really like is to have a single checkbox to use as a boolean flag.
Anyone knows how?
Ok, the answer is to just use the 'switch' type: that will build a 'slider' switch on backoffice page. For future reference, I'm gonna report 3 different ways to accomplish the same task: radio, select and switch.
They have all been tested on AdminAddressesController and are bound to a custom DB boolean field called 'expo'.
//SELECT
$s_options = array(
array( 'expo' => 1, 'name' => 'Yes' ),
array( 'expo' => 0, 'name' => 'No' )
);
$temp_fields[] = array(
'type' => 'select',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'options' => array(
'query' => $s_options,
'id' => 'expo',
'name' => 'name'
)
);
//RADIO
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'radio',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => $s_options
);
//SWITCH
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'switch',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'is_bool' => true,
'values' => $s_options
);

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',
),
));

How to filter zf2 forms?

I'm trying to add filters for my form elements
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'required' => true,
),
'options' => array(
'label' => 'Name',
),
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
)
));
but I still can add some tags in this element, what am I doing wrong?
Create two classes one for form another for filter and set filter for your form.
$form = new Form\CommentForm();
$form->setInputFilter(new Form\CommentFilter());
return $form;
for more info see https://github.com/nsenkevich/comment/tree/master/Comment