I would like to create a form with a label that would contain two different styles, for example:
"Email (enter your email):"
How can I do that? Right now I am using the label attribute of the addElement() method:
$form->addElement('text', 'email', array(
'label' => 'Email',
'required' => true,
));
With this method I only get a label attribute to the form tag, and can't use different styling inside it.
Thank you!
Do you already use the Label decorator ?
If yes, just use this code :
$form->addElement('text', 'email', array(
'label' => '<strong>Email</strong> (enter your email)',
'required' => true,
));
/* Here some code to add the Label decorator */
$form->getElement('email')->getDecorator('Label')->setOption('escape', false);
And this is done. I just added the <strong> tag to the label, and asked the Label decorator not to escape this tag.
It's also working for any html content. This is how I could put images in some checkboxes labels :) (country flags to choose the language)
Related
I've got an Integer field in Symfony form, which represents quantity - ofcourse it should be unable to pass the value equal or less than 0, so I've validated form in Entity and Form.
But - It's validated only when form is already sent. Is there any built-in method to validate form on input? I know that propably i would need to write own JS function to provide this, but I don't want to repeat someone others job :)
Found the solution.
To validate Symfony form on HTML side, you can just add an attribute to input field, reference: https://www.w3schools.com/htmL/html_form_attributes.asp
So, you form field should look like:
->add('quantity', IntegerType::class, ['label' => false,
'constraints' => new GreaterThan(array('value' => 0)),
'attr' => array(
'min' => '0',
)
])
Use pattern and min on your input. HTML should prevent form submit as long as the pattern isn't matched. (Careful, isn't doens't prevent submit if you're usin Javascript/Ajax to submit)
$builder->add('input_name', TextType::class, array(
'attr'=>array(
'pattern'=>'^[1-9][0-9]*' // 1 and above
'min'=>'1'
)
));
I'm using a TextType to remove the arrows in the input.
If you want to keep them, use NumberType.
A little addition, if you want to make sure a user can only enter a number you can add this bit of js to your code.
->add('quantity', NumberType::class, [ 'attr' =>
[
'oninput' => "this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');"
] ])
I have the following line of code on a CakePHP view:
<?php
echo $this->Form->input(
'person_id',
array(
'label' => false,
'div' => false,
'class' => 'form-control search-person'
)
);
?>
I want to create a text input with this line of code, but if the field name has the suffix _id, the rendered HTML changes from a text field to a drop-down select.
If I change the prefix to anything else, for example person_idd or abc_idd, it renders a text input, but if the field name ends with _id suffix, it renders a drop-down select, which doesn't allow me to write anything.
Is this some CakePHP feature? How can I avoid this behavior and produce a text input with a field ending with the _id suffix?
It's a CakePHP feature:
This method will automatically inspect the model field it has been supplied in order to create an appropriate input for that field.
Taken from Cookbook 2.x: FormHelper: Creating form elements.
To get a text input, add 'type' => 'text' to the options array:
<?php echo $this->Form->input('person_id', array(
'type' => 'text',
'label' => false,
'div' => false,
'class' => 'form-control search-person'
)); ?>
I have some custom attributes for my project.
Attribute1 : Use in home page sidebar(yes/no)
if it is yes show the below attribute.
Attribute2 : Browse image
I want to add attribute2 based on the attribute1. Only when the Use in Home Page Sidebar is enabled , my new attribute will be shown below of the current. I.e., it will be a dependent attribute. Does somebody know the script for adding dependent attributes in Magento?
Previously i added custom attributes by
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'use_home_page_side_bar', array(
'group' => 'General',
'input' => 'select',
'type' => 'int',
'label' => 'Use in Home Page Sidebar',
'backend' => '',
'source' => 'eav/entity_attribute_source_boolean',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Like this script, is there any script for adding dependent attributes?
If you are working on admin forms, then a class exists for automatically hiding elements when the value of fields change.
Below is an example which shows field dependency.
$form = new Varien_Data_Form();
$form->addField('yesno', 'select', array(
'label' => $this->__('Yes or No?'),
'values' => Mage::model('adminhtml/system_config_source_yesnocustom')
->toOptionArray(),
));
$form->addField('custom_value', text, array(
'label' => $this->__('Other'),
));
// Append dependency javascript
$this->setChild('form_after', $this->getLayout()
->createBlock('adminhtml/widget_form_element_dependence')
->addFieldMap('yesno', 'yesno')
->addFieldMap('custom_value', 'custom_value')
->addFieldDependence('custom_value', 'yesno', 2) // 2 = 'Specified'
);
You need to map every field name to an element ID. You can add as many field mappings and field dependencies in this way as you wish.
I have created simple category attribute dependency by adding new input renderer for attribute. It is working this way:
You have several attributes:
– my_attribute
– my_attribute_text
– my_attribute_select
Note that they all start from my_attribute.
First attribute has boolean type. When it is set to true – other attributes that start from my_attribute is visible.
Source - https://github.com/elpas0/category_dependence
Description - http://nwdthemes.com/2015/02/20/magento-category-attributes-dependency/
I am using Cakephp 2.2.3 version. When I create form using Cake form helpers, it automatically appends a div and label to the input type field. How to avoid it?
Following is the code:
<?php echo $this->Form->input('username', array('id' => 'username', 'class' => 'login-inp', 'type' => 'text')); ?>
You can use options array of input to avoid form appending div and label automatically. Set div and label of options array to false.
echo $this->Form->input('username',
array('id' => 'username', 'class' => 'login-inp',
'div' => false, 'label' => false
)
);
That's what FormHelper::input() is supposed to do. If you don't want the label and wrapping div just use the functions to generate specific input elements only like FormHelper::text(), FormHelper::select(), FormHelper::radio(), etc.
Consider I would like to display
Checkbox, Checkbox label and an image there after.
How can I create a view for the same using Zend Engine Form
I tried as follows
$this->addElement(
'Checkbox',
"$key",
array(
'label'=>$value,
'checkedValue' => "$key",
'uncheckedValue' => '',
'checked' => true,
'disabled' => false,
)
);
$element = $this->getElement($key);
$element->setDecorators(
array(
'ViewHelper',
array('Label', array('tag' => 'dt', 'class'=>'hidden')),
array('Description',array('escape'=>false,'tag'=>' span')), //escape false because I want html output
)
);
$element->setDescription('<img name="help_'.$key.'" id="help_'.$key.'" src="/application/modules/User/externals/images/what-is-this.gif"/>');
$element->setDescription('<img name="help_'.$key.'" id="help_'.$key.'" src="/application/modules/User/externals/images/what-is-this.gif"/>');
But which showing checkbox, description image and checkbox label as superscript.
Any help please
$element->getDecorator('Description')->setEscape(false);
This will prevent escaping description content.
Check out the AnyMarkup Decorator.
You can add that decorator into your element decorator list, specifying the markup for the image with an APPEND position.