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',
),
)
);
Related
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.
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!'
),
),
),
),
),
));
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.
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(
),
),
),
));
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"
),
),
),
));