I am setting up a test site with symfony and I built a datagrid with pagination that works correctly.
One standard feature I would like to add to pagination is the number of displayed items to be set for a dropdown menu:
I have a choice field (for the moment in a controller, but later I will put it in a form class.)
Form with the choice field in built using this code:
$form = $this->createFormBuilder()
// ->setAction($this->generateUrl('userlist2'))
//->setMethod('GET')
->add('numRecords', 'choice', array(
'choices' => array('5' => '5', '10' => '10', '20' => '20'),
'required' => false,
'empty_value' => false,
'attr' => array(
'class' => 'form-control',
'style' => 'width:80px',
)
)
)
->getForm();
I have googled to check if symfony has a way to get changes in choice field but could not find it.
What I need is to intercept the change event of the choice field and get its value, so I can put the item number value in a variable and use it for pagination.
Any help appreciated
Thanks
Paolo
Related
In a Symfony3 project, i have a SonataAdminBundle form like this :
protected function configureFormFields(FormMapper $formMapper) {
$formMapper->add('rentalTime', 'choice', array(
'label' => 'Durée de location',
'multiple' => false,
'choices' => array(
'3H' => '3',
'24H' => '24',
)
));
$formMapper->add('rentalTimeType', 'choice', array(
'label' => 'Type de durée de location',
'multiple' => false,
'choices' => array(
'Heure(s)' => 'H',
'Plage de jours' => 'R'
)
));
.
.
.
When i change the value of rentalTime i want to change the value of rentaltimeType.
Example: If i select 24H in rentalTime field and automatically the rentalTimeType change for 'Plage de jours'.
I've read a lot of topic with the use of $subject but it's not exactly what i need.
change row color in sonata admin bundle dependent on a value of a field
Correct way to use FormEvents to customise fields in SonataAdmin
Sonata User Admin - Custom field dependency
So is it possible to do what i need ?
You can customize the form specific to the data that was submitted by the user.
Dynamic Generation for Submitted Forms
how I can get value from form choice widget of submitted form?
Now it returns only integer index of selected option, but I need a value.
Below is my code:
$this->setWidget('emails',new sfWidgetFormChoice([
'label' => __('Emails'),
'expanded' => true,
'multiple' => true,
'choices' => ['email#gmail.com', 'email2#gmail.com'],
]));
$this->setValidators([
'emails' => new sfValidatorChoice(
['choices' => array_keys($this->getDefault('emails')), 'multiple' => true],
['required' => __('Required')]),
The choice widget can take a choices parameter with an associative array to define both the value and the displayed text of each option.
'choices' => array('email#gmail.com' => 'email#gmail.com', 'email2#gmail.com' => 'email2#gmail.com'),
This will submit the email as the value, however now you have potential security issues. Make sure you are enabling CSRF protection and properly validating the input at a minimum.
I have a select field like this in my costum magento admin form
$fieldset->addField(
'category_id',
'select',
array(
'id' => 'lazadaCategory',
'label' => Mage::helper('test_sellercenter')->__('Category '),
'name' => 'status',
'values' => Mage::helper('test_sellercenter/dropdown')->getLazadaCategories(),
'class' => 'required-entry',
)
);
and when the select value change i need to add another fields below this select field (not like toogle show/hide just add), for example like this field
$fieldset->addField(
'listing_name',
'text',
array(
'label' => Mage::helper('test_sellercenter')->__('Listing Name'),
'name' => 'listing_name',
'required' => true,
'class' => 'required-entry',
)
);
is there a way to do this in magento?
if you don't want to use show/hide then you can just add the field from frontend using javascript, if you need values from magento(backend) then make an ajax call on the change of the first field to a custom controller action for obtaining needed data and then adding the second field, you can do this in your custom form template
im pretty new to ZF2 and i have a pretty strange problem. I´ve created a simple checkbox using arrays,
and set checked value to good and unchecked value to bad. But when i submit my form, the URL shows that when the checkbox is checked, it sends .....checkbox=bad&checkbox=good... I don´t know why.
class SearchForm extends Form {
public function __construct($name = null){
parent:: __construct('Search');
$this->setAttribute('method', 'get');
$this->setAttribute ( 'enctype', 'multipart/formdata' );
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
'options' => array(
'label' => 'A checkbox',
'checked_value' => 'good',
'unchecked_value' => 'bad',
),
));
Because by default Zend\Form\Element\Checkbox has use_hidden_element is true.
If set to true (which is default), the view helper will generate a
hidden element that contains the unchecked value. Therefore, when
using custom unchecked value, this option have to be set to true.
You use GET method. Of couse, you see two values in query string: for checkbox and for hidden elements.
See more carefully ZF2#Checkbox.
Not 100% sure this is the answer to the issue and not in a position to test right now but it is likely to do with the hidden element that the checkbox uses try:
$form->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'checkbox',
'options' => array(
'label' => 'A checkbox',
'use_hidden_element' => false,
'checked_value' => 'good',
'unchecked_value' => 'bad'
),
));
I have some immutable attributes on my entity to administrate with sonata-admin bundle.
I want to show them in the edit-view of the entity, but don't want to provide any mechanism to change it (e.g. the value shall not be inside a input field)
I couldn't find anything but this:
$formMapper
->add('post', 'entity',
array(
'label' => 'Some post',
'attr' => array(
'readonly' => true,
'disabled' => true
),
'class' => 'Acme\DemoBundle\Entity\Post'
)
)
;
I tried it out with read_only, readonly, disabled etc. all the stuff. It looks ok, it's now inside a dropdown (since it is an entity) and I can not modify it.
But I even don't want that. I really need it as text (the current one).
Especially this is annoying if you use DoctrineExtensions with softdeletable, timestampable, since every "save" saves also the form-data.
Changing the type to 'text' instead of 'entity' replaces the dropdown with a input-field.. So, what's the best approach here?
$formMapper
->add('post', 'entity',
array(
'label' => 'Some post',
'read_only' => true,
'disabled' => true,
'class' => 'Acme\DemoBundle\Entity\Post'
)
)
;
This answer tells how to customize list rendering. Maybe the same approach works with form rendering?
If not, then you can create your custom form type according to create custom field type documentation, and customizing the template.
This is a bit old but this might help someone.
Here is the code that resolves your issue.
$formMapper
->add('post', 'entity', array('label' => 'Some post','attr' => array(
'readonly' => 'readonly',
'disabled' => 'disabled',
),
'class' => 'Acme\DemoBundle\Entity\Post')
)