Setting form element attributes - php

I'm new to ZF2, I am rewriting a simple application previously writen with ZF1. I have a form wich I want to customize, because the default input/textearea width and heigth are not enought. Here my code for some fields of my form:
$this->add(array(
'name' => 'page',
'type' => 'Number',
'options' => array(
'label' => 'Page',
),
));
$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'notes',
'type' => 'Textarea',
'options' => array(
'label' => 'Notes',
),
));
How can I specify a width for my inputs elements, and the attributes cols and rows for my textarea ?

$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
'attributes' => array
(
'maxlength' => 250,
'type' => 'text',
'class' => 'class-name'
),
));
You can add an attributes array, and specify a class which can be used to edit things like width for your input elements. I believe you can also add 'rows' and 'cols' to this as well.

Related

Set NotEmpty validator error message

I am trying to set a custom error message with ZF2 form and the NotEmpty validaor.
In my form I have the following radio element.
$this->add(array(
'name' => 'paymentMethod',
'type' => 'Radio',
'options' => array(
'label' => _('Payment Methods:'),
'value_options' => $paymentMethods,
'disable_inarray_validator' => TRUE,
),
));
and in my input filter I have
$inputFilter->add(array(
'name' => 'paymentMethod',
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => _("You must select the payment method"),
),
),
),
array(
'name' => 'InArray',
'options' => array(
'haystacK' => $this->getPaymentMethods(),
'messages' => array(
InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
),
),
),
),
));
As can be seen in my input filter I have set a custom message for my NotEmpty validator. The problem I am having is that the form outputs the default error message 'Value is required and can't be empty' and not my custom one.
I assume that this has something to do with the 'required' => TRUE automatically setting a NotEmpty validator but I don't know how to disable this.
I have other text elements with this validator and they display the custom error message fine, this radio element just does not. I also have a multicheckbox element that has the same problem.
Does anyone know as to why this is happening?
Many thanks in advance.
EDIT
I am now having the same problem with another element, in this case its DoctrineModule\Form\Element\ObjectMultiCheckbox.
The element is defined
$this->add(array(
'name' => 'categories',
'type' => 'Admin\DoctrineModule\Form\Element\ObjectMultiCheckbox',
'options' => array(
'label' => _('Categories:'),
'label_attributes' => array('class' => 'required'),
'object_manager' => $this->getEntityManager(),
'target_class' => 'Application\Entity\Categories',
'property' => 'category',
'is_method' => true,
'find_method' => array(
'name' => 'FindAll',
),
'disable_inarray_validator' => TRUE,
),
));
and in my getInputFilterSpecification() method I have
...
'categories' =>
array(
'required' => TRUE,
'allow_empty' => TRUE,
'continue_if_empty' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => _("You must select the items categories"),
),
),
),
array(
'name' => 'Freedom\Zend\Validator\Doctrine\Db\DoctrineRecordExists',
'options' => array(
'entityManager' => $this->getEntityManager(),
'entity' => 'Application\Entity\Categories',
'identifier' => 'catagoryId',
'messages' => array(
DoctrineRecordExists::ERROR_NO_RECORD_FOUND => _("This category does not exist"),
),
),
),
),
)
...
As you can see I have tried the 'allow_empty' and 'continue_if_empty' options but with no luck. I have also tried 'required' => FALSE, but the element simply passes validation.
The default message shows when NotEmpty validation fails.
Modify your input filter change the 'required' => TRUE, option to 'required' => false
$inputFilter->add(array(
'name' => 'paymentMethod',
'required' => false,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => _("You must select the payment method"),
),
),
),
array(
'name' => 'InArray',
'options' => array(
'haystacK' => $this->getPaymentMethods(),
'messages' => array(
InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
),
),
),
),
));
I made some changes in your code but i did not test. Try and let me know if it worked. Good luck
$inputFilter->add(array(
'name' => 'paymentMethod',
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => "You must select the payment method",
),
),
'break_chain_on_failure' => true
),
array(
'name' => 'InArray',
'options' => array(
'haystacK' => $this->getPaymentMethods(),
'messages' => array(
InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
),
),
),
),
));
I have found the solution to my problem.
i changed the 'required' => TRUE, option to 'allow_empty' => TRUE, and now my error message is showing correctly.
$inputFilter->add(array(
'name' => 'paymentMethod',
'allow_empty' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => _("You must select the payment method"),
),
),
'break_chain_on_failure' => true,
),
array(
'name' => 'InArray',
'options' => array(
'haystacK' => $this->getPaymentMethods(),
'messages' => array(
InArray::NOT_IN_ARRAY => _("This payment method does not exist"),
),
),
),
),
));
I assume not setting the required option to true stops the auto creation of a NotEmpty validator.

Create Textarea input box in Zend 2 Form

I'm trying to set a textarea in a Zend Form but it always creates an input type text box.
I read some code using the Zend\Form\Element\Textarea but still no luck
This is how I am doing it in my ProjectForm.php:
$this->add(array(
'name' => 'summary',
'type' => 'Zend\Form\Element\Textarea',
'options' => array(
'label' => 'Resumen',
),
));
And in Project.php I have this
$inputFilter->add($factory->createInput(array(
'name' => 'summary',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
),
),
),
)));
Thanks
Nevermind, I was calling echo $this->formInput instead of echo $this->formTextarea viewHelper.
You have to mention the type in attributes:
$this->add(array(
'name' => 'summary',
'attributes' => array(
'id' => 'summary'
'type' => 'textarea',
'class' => '',
),
'options' => array(
'label' => 'Resumen',
),
)
);

ZF2 - Control/customise individual radio/checkboxes

I'm attempting to add data- values on radio buttons within ZF2. Is it possible to control each of the inputs specified with value_options?
A typical radio button added to a form:
$this->add(array(
'type' => 'radio',
'name' => 'duration',
'options' => array(
'value_options' => array(
'daily' => 'Daily',
'weekly' => 'Weekly',
'monthly' => 'Monthly',
),
),
));
Ultimately, I would like something like the following, so I can specify individual parameters/options for each radio item:
$this->add(array(
'type' => 'radio',
'name' => 'duration',
'options' => array(
'value_options' => array(
array(
'attributes' => array(
'value' => 'daily',
'data-item' => 'apple'
),
'options' => array(
'label' => 'Daily'
)
),
array(
'attributes' => array(
'value' => 'weekly',
'data-item' => 'orange'
),
'options' => array(
'label' => 'Weekly'
)
),
array(
'attributes' => array(
'value' => 'monthly',
'data-item' => 'pear'
),
'options' => array(
'label' => 'Monthly'
)
),
),
),
));
My reason for wanting the above, is that I want to use JavaScript to change something upon selecting a radio button, so it needs to hold data attributes.
Is anything like this possible yet?
It can be done by providing an array (or an object which implements ArrayAccess) instead of a single value (almost as you wrote in your example).
$this->add(array(
'type' => 'radio',
'name' => 'duration',
'options' => array(
'value_options' => array(
'daily' => array(
'label' => 'Daily',
'value' => 'daily',
'attributes' => array(
'data-item' => 'apple',
),
),
'weekly' => array(
'label' => 'Weekly',
'value' => 'weekly',
'attributes' => array(
'data-item' => 'orange',
),
),
'monthly' => array(
'label' => 'Monthly',
'value' => 'monthly',
'attributes' => array(
'data-item' => 'pear',
),
),
),
),
));
https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormMultiCheckbox.php#L177
This should work on radios, multi-checkboxes & selects too.

How to set filters and validators in ZF2 fieldsets using Zend\Form\Factory?

I use Zend\Form\Factory to create forms in zend framework2
$factory = new Zend\Form\Factory();
$factory->createForm(array(
'elements' => array(
array(
'spec' => array(
'name' => 'name',
),
),
),
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
));
You can see that there are filters and validators for field "name". It works.
I have the problem if I use fieldsets:
$factory->createForm(array(
'fieldsets' => array(
array(
'spec' => array(
'name' => 'common',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
),
),
),
),
),
),
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
));
In this example input filter doesn`t work. I don't know how to set filters and validators to field "name" in fieldset "common"
This example does not work too:
$factory->createForm(array(
'fieldsets' => array(
array(
'spec' => array(
'name' => 'common',
'elements' => array(
array(
'spec' => array(
'name' => 'name',
),
),
),
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
),
),
),
));
You need to specify 'type' key in input filter when you used fieldset.
$factory = new \Zend\Form\Factory();
$form = $factory->createForm(array(
'hydrator' => 'Zend\Stdlib\Hydrator\ArraySerializable',
'elements' => array(
array(
'spec' => array(
'name' => 'email1',
),
),
),
'fieldsets' => array(
array(
'spec' => array(
'name' => 'common',
'elements' => array(
array(
'spec' => array(
'name' => 'email2',
),
),
),
),
),
),
'input_filter' => array(
'email1' => array(
'validators' => array(
// validators for field "name"
new \Zend\Validator\EmailAddress(),
),
'filters' => array(
// filters for field "name"
array('name' => 'Zend\Filter\StringTrim'),
),
),
'common' => array(
'email2' => array(
'validators' => array(
// validators for field "name"
new \Zend\Validator\EmailAddress(),
),
'filters' => array(
// filters for field "name"
array('name' => 'Zend\Filter\StringTrim'),
),
),
'type' => 'Zend\InputFilter\InputFilter',
)
),
));
$form->setData(array('email1'=>'test#gmail.com','common'=>array('email2'=>'invalid-email')));
if(!$form->isValid()){
print_r($form->getMessages());
}
If you want to add dynamic validators in the Action (for example validators that are required only when some other fields have a specific value), it is quite a puzzle to apply this when using form collection.
In order to achieve this you should grab the validator chain from the specific element. For each fieldset however, you should first hook in it's own input filter. I would like to share this, because this took me 2 hours to understand ;)
Let's say you have a base form, the base form has a fieldset, and the fieldset has x-elements. The code to add a validator to one of the x-elements requires following chain:
$form->getInputFilter()
->get('base-form')
->get('fieldset-form')
->getInputFilter()
->get('element')
->getValidatorChain()
->addValidator($validator);
The 2 getInputFilter() can give you an headache.
You have your syntax incorrect, are common and spec supposed to be nested fieldsets or something? Not sure what you are doing there... Try removing the spec part
$factory = new Factory();
$form = $factory->createForm(array(
'fieldsets' => array(
array(
'name' => 'details',
/**
* Elements for the "details" form
*/
'elements' => array(
array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Full name',
),
),
array(
'type' => 'Zend\Form\Element\Email',
'name' => 'email',
'options' => array(
'label' => 'Email address',
),
),
),
),
array(
'name' => 'extra',
'elements' => array(
array(
'name' => 'address',
'type' => 'Text',
'options' => array(
'label' => 'Address',
),
),
array(
'name' => 'notes',
'type' => 'Zend\Form\Element\Textarea',
'options' => array(
'label' => 'Notes',
),
),
),
),
),
/**
* Elements on the form itself, not in the fieldsset
*/
'elements' => array(
array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'options' => array(
'captcha' => array(
'class' => 'Dumb',
),
),
),
array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'security',
),
array(
'name' => 'send',
'type' => 'Submit',
'attributes' => array(
'value' => 'Submit',
),
),
),
/*/
* Input Filters Spec here
*/
'input_filter' => array(
'name' => array(
'validators' => array(
// validators for field "name"
),
'filters' => array(
// filters for field "name"
),
),
),
));

I want to customise the markup of zend form

I have a form and I would like to customise the markup for it. Im using the decorators at the bottom of the class. However, I keep getting the error:
Exception caught by form: No file decorator found.
I was wondering if someone would be able to show me an example. I basically want to output the form as and do away with the standard layout. Maybe add a few custom classes also.
Updated
class Application_Form_Admin extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
// set the data format
$this->setAttrib('enctype', 'multipart/form-data');
// Add the entry id
$this->addElement('hidden', 'id', array(
'required' => false
));
// Add the title
$this->addElement('text', 'title', array(
'label' => 'Project Title',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('text', 'appid', array(
'label' => 'App Id',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('text', 'appsecret', array(
'label' => 'App Secret',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('text', 'namespace', array(
'label' => 'Namespace',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('text', 'applink', array(
'label' => 'App Link',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 255))
)
));
// Facebook Only?
$this->addElement('checkbox', 'fbonly', array(
'label' => 'Facebook Only',
'required' => false,
'value' => 1
));
// general app data group
$this->addDisplayGroup(array('title', 'appid', 'appsecret', 'namespace', 'applink', 'fbonly'), 'general', array('legend' => 'General Optons'));
// Facebook Only?
$this->addElement('checkbox', 'fangate', array(
'label' => 'Fangate Page',
'required' => false,
'value' => 1
));
// Facebook Only?
$this->addElement('checkbox', 'welcome', array(
'label' => 'Welcome Page',
'required' => false,
'value' => 1
));
// Facebook Only?
$this->addElement('checkbox', 'help', array(
'label' => 'Help Page',
'required' => false,
'value' => 1
));
// Facebook Only?
$this->addElement('checkbox', 'entries', array(
'label' => 'Entries Page',
'required' => false,
'value' => 1
));
// pages enabled group
$this->addDisplayGroup(array('fangate', 'welcome', 'help', 'entries'), 'page', array('legend' => 'Page Optons'));
// Add the title
$this->addElement('text', 'ogtype', array(
'label' => 'Default Open Graph Type',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('text', 'ogtitle', array(
'label' => 'Default Open Graph Title',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('text', 'ogdesc', array(
'label' => 'Default Open Graph Description',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the title
$this->addElement('file', 'ogimage', array(
'label' => 'Default App Image',
'required' => false
));
// generic open graph data
$this->addDisplayGroup(array('ogtype', 'ogtitle', 'ogdesc', 'ogimage'), 'og', array('legend' => 'Generic Open Graph data'));
// Add the wall title
$this->addElement('text', 'apptitle', array(
'label' => 'App Title',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the wall caption
$this->addElement('text', 'appcaption', array(
'label' => 'App Caption',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 60))
)
));
// Add the wall message
$this->addElement('text', 'appdesc', array(
'label' => 'App Description',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 255))
)
));
// app message details
$this->addDisplayGroup(array('apptitle', 'appcaption', 'appdesc'), 'appdetails', array('legend' => 'App deatils (Used when sharing)'));
// Add the wall title
$this->addElement('text', 'wallmsg', array(
'label' => 'Wall Message',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 255))
)
));
// Add the wall title
$this->addElement('text', 'friendmsg', array(
'label' => 'Friends Wall Message',
'required' => false,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 255))
)
));
// app message details
$this->addDisplayGroup(array('wallmsg', 'friendmsg'), 'wallmessages', array('legend' => 'Wall post messages'));
// Add the submit button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Submit',
));
// And finally add some CSRF protection
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
// change markup of elements
$this->setElementDecorators(array(
'ViewHelper',
array(
'Description',
array(
'tag' => 'div',
'class' => 'submit-button',
'escape' => false
)
),
array(
array(
'data' => 'HtmlTag'
),
array(
'tag' => 'span',
'class' => 'element'
)
),
array(
'Label',
array(
'tag' => 'label',
'class' => 'elementLabel',
'escape' => false
)
),
'File',
array(
array(
'data' => 'HtmlTag'
),
array(
'tag' => 'span',
'class' => 'element'
)
),
array(
array(
'row' => 'HtmlTag'
),
array(
'tag' => 'li',
'class' => 'element-row'
),
),
'Errors'
));
// change markup of form
$this->setDecorators(array(
'FormElements',
array(
'HtmlTag',
array(
'tag' => 'div',
'id' => $this->_containerId
)
),
'Form',
'Errors'
));
}
}
You have at least one "File" element in your form, you called setDecorators and did not include the "File" decorator, which is a required decoratotr for a 'File' element:
35.6.4. Zend_Form_Element_File
The File form element provides a mechanism for supplying file upload fields to your form. It utilizes
Zend_File_Transfer internally to provide this functionality, and the
FormFile view helper as also the File decorator to display the form
element.

Categories