I want to customise the markup of zend form - php

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.

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

zend 2.0 custom error message?

Hi i want to add custom messages to error messages. Here is my code . it is not at all working could you please how to check this code once.
$this->add(array(
'name' => 'toname',
'type' => 'Zend\Form\Element\Text',
'attributes' => array(
'id' => 'toname',
'class' =>'toname',
'required' =>true,
'validators' => array(
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Please enter User Name!'
),
),
),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 4,
'max' => 20,
'messages' => array(
'stringLengthTooShort' => 'Please enter User Name between 4 to 20 character!',
'stringLengthTooLong' => 'Please enter User Name between 4 to 20 character!'
),
),
),
),
),
));

ZF2 : Priority for Validators in InputFilter

I have a validator on a form entry like this :
$this->add(array(
'name' => 'email',
'required' => true,
'filter' => array(
'name' => 'StripTags',
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Veuillez renseigner une adresse e-mail.',
),
),
),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'EmailAddress',
'options' => array(
),
),
),
));
There's basicaly 3 validators on my input.
The NotEmpty, the StringLength, and the EmailAdress.
Is there any way to set a kind of priority between them ?
Right now, if I submit an empty form, I get messages relative to those 3 validators, ie. :
My input is empty.
My string length is too short (thanks...)
My input is not an email (thanks again...)
Is there anyway to tell my validator to stop at the first failure ? (or at least to only print the 1st message).
Use the 'break_chain_on_failure' key in your validator spec with a value of true, ie
$this->add(array(
'name' => 'email',
'required' => true,
'filter' => array(
'name' => 'StripTags',
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Veuillez renseigner une adresse e-mail.',
),
),
),
array(
'name' => 'StringLength',
'break_chain_on_failure' => true,
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'EmailAddress',
'options' => array(
),
),
),
));

#ZEND: How to make form label and element horizontally without decorators

How to create form elements and labels horizontally without decorators help.
// Add the comment element
$this->addElement('text', 'txt_password', array(
'label' => 'Password:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
Tray its
$this->addElement('text', 'txt_password', array(
'label' => 'Password:',
'required' => true,
'decorators' => array(
'ViewHelper',
'Description',
'Errors',
array(array('elementDiv' => 'HtmlTag'), array('tag' => 'span')),
array('Label', array('tag' => 'span')),
),
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));

Categories