Set NotEmpty validator error message - php

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.

Related

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

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