How to make Textarea not resizable in Zend Form - textarea

I have a Zend Form and one of it's fields is a Textarea. I have set it's default size using 'rows' and 'cols' but I want the user not to be able to change it.
I have tried adding 'resize' => false, but it did not work.
public function __construct()
{
parent::__construct('form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
$this->add([
'name' => 'feedback',
'type' => Textarea::class,
'attributes' => [
'id' => 'feedback',
'class' => 'mdc-text-field__input',
'rows' => 3,
'cols' => 4,
'required' => false,
],
'options' => [
'label' => 'Feedback',
],
]);
}

You have the choice between
attributes => [
...,
'style' => 'resize:none'
]
or use a css class
attributes => [
...,
'class' => 'notresizable'
]
and define this css class in your css file.

Related

How add a class to a label element with Symfony forms?

I have a FormBuilderwith Symfony3 and I need you to add a class to the discount label.
How can you observe the third parameter of the add method is an array and a key is attr which has another array with the attributes of the input element, but not the label.
How can I add class to the label?
$builder
->add('note', TextareaType::class, array(
'label' => "Notes",
'required' => false
))
->add('discount', NumberType::class, array(
'required' => false,
'attr' => array(
'class' => "hidden"
)
))
You can see the label_attr parameter in symfony docs
'label'=>"Notes,array('label_attr' => array('class' => 'class_name'))"
This is what worked for me in Symfony 5.4 : 'label_attr' => ['class' => 'your-label-classes']
Example:
class YourFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email', EmailType::class, [
'label'=>'Your label name',
'label_attr' => ['class' => 'your-label-classes', 'for'=>"email"],
'attr' => [
'class' => "your-field-classes",
'type' => "email",
],
])
...
}
...
}

Incorporate custom validation error messages into form object by element

I have the following code which creates a specific text element:
$this->add([
'type' => 'text',
'name' => 'newpassword',
'attributes' => [
'id' => 'newpassword',
'class' => 'form-control'
],
'options' => [
'label' => 'Enter New User Password',
],
]);
And I have the following code which produces my input filter definitions:
$inputFilter->add([
'name' => 'newpassword',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 6,
'max' => 256
],
]
],
]);
What I want to accomplish is adding my custom messages. Here's the way they have it in the api documentation:
$validator = new Zend\Validator\StringLength(array('min' => 8, 'max' => 12));
$validator->setMessages( array(
Zend\Validator\StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
Zend\Validator\StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
));
How do I incorporate my custom validation messages into my already programmed code?
UPDATE:
I think this is where i will find success, but not sure how to do it:
$inputFilter->get('newpassword')->getValidatorChain()->
Use this-: its messageTemplates to set custom message
$inputFilter->add([
'name' => 'newpassword',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 6,
'max' => 256,
'messageTemplates'=>array(
\Zend\Validator\StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
\Zend\Validator\StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
)
],
]
],
]);

How to make MultiCheckBox Label bold or strong?

I have the following multicheckbox:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
**EDIT 1:**
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
How to make the following part bold?
Using label_attributes makes all labels bold, and I want just the main label for the multibox be bold.
'label' => 'A. Check all direct practice field education assignments',
EDIT 1: add label_attributes to "options"
When I add label_attributes as #itrascastro suggested, all labels become bold and I want only the top one to become bold: multicheckbox
Try to add this to your options:
'options' => array(
'label_attributes' => array(
'class' => 'myCssBoldClass'
),
// more options
),
Each value option accepts an array of options.
For example
'value_options' => [
[
'label' => 'Adults',
'label_attributes' => [
'class' => 'my-bold-class',
],
'attributes' => [],
'value' => 1,
'selected' => false,
'disabled' => false,
],
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
],
I've added in some other values you can define; they are obviously optional.
Since there is no build in option to do this just for the top label, I used jQuery to accomplish this:
$("label[for]").each(function(){
$(this).addClass("label-bold");
});

attribute min and max in input file Zend Framework 2

I'm trying to create a multiple uploads with ZF2. I would set the min and max attribute on the input file. I tried in every way but ZF2 ignores these 2 attributes for the input file.
How can I do?
namespace Admin\Form;
use Zend\Form\Form;
class Image extends Form
{
public function __construct()
{
parent::__construct('image');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'image',
'type' => 'Zend\Form\Element\File',
'attributes' => array(
'multiple' => true,
'accept' => '.jpg,.png',
'id' => 'image',
'min' => 1,
'max' => 5,
)
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'save',
'id' => 'submitbutton',
'class' => 'btn btn-success'
)
));
}
}

ZF2 Dynamic Form Filters/Validators

I'm setting up a controller that will create a form.
I can't use an extended to Form class, so i need to build up my form on my controller.
$form = new Form('example');
$fieldset = new Fieldset('default');
$fieldset->add(array('name' => 'example_field', 'attributes' => array('type' => 'text', 'id' => 'example_field'), 'options' => array('label' => 'Example Field',),));
$form->add($fieldset);
The main question here is, how do I define the filters and validators for each element/fieldset without required to create a class implementing InputFilterAwareInterface, so i can do everything from my controller?
Thanks in advance!
You could add/remove form Validators by handle InputFilter of form, here is my example:
$form = new \Zend\Form\Form();
$name = array(
'name' => 'username',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
);
$form->add($name);
$filter = $form->getInputFilter();
$filter->remove('username');
$filter->add(array(
'name' => 'username',
'required' => true,
'validators' => array (
'stringLength' => array (
'name' => 'StringLength',
'options' => array (
'max' => '3',
),
),
),
));
$form->setInputFilter($filter);
$form->setData(array(
'username' => 'longtext',
));
$form->prepare();
echo $form->isValid(); //false
print_r($form->getMessages()); //stringLengthTooLong error will show
Add validator dynamically:
$form->getInputFilter()->get('element_name')->getValidatorChain()->attach(new ValidatorClassName());
Add filter dynamically:
$form->getInputFilter()->get('element_name')->getFilterChain()->attach(new FilterClassName());

Resources