I'm displaying a choice field in Symfony 2.0.4 with multiple checkboxes. Symfony always displays the first checkbox as checked and I would like to disable this behavior. I've tried setting the underlying entity's field to an empty array before building the form but this didn't have any effect. Is this a glitch in the Symfony version or is there a special way of achieving this?
EDIT:
By choice field with multiple checkboxes I mean a choice field having the expanded and multiple options set to true.
It seems that this was my stupid mistake. I was overriding the form theme and in the choice_widget_expanded block I was adding the checked flag for the first element of the choice array.
You need to add in your form class :
$builder->add('yourField', 'choice', array(
'choices' => $choices
'required' => false,
....
));
You can find more information in the ChoiceField type documentation http://symfony.com/doc/2.0/reference/forms/types/choice.html
Also check that you don't have any JavaScript which is checking the first value.
Related
I've made a custom page in backpack admin panel. This page is non-CRUD (not related to any model). There are several forms on it, with date pickers, select inputs, etc. So I'am trying to find a way to use backpack fields to create these date pickers and select inputs. Because it seems to be awkward to embed custom js-controls into the project, as Backpack already has appropriate fields.
The only solution I came up with, is to create a crud controller for random model, disable all operations except create, use create operation view as custom page (backpack fields are available this way), and finally override store() method - to prevent creating new model entry in DB.
So, is there a proper way to access backpack fields on custom (non-CRUD) page?
Backpack 4.x fields aren't meant to be used outside CRUDs, but you can do that.
Option A
At their core, Backpack fields are just Blade views, so you can load them using the Blade helper #include(). Just make sure to pass along all variables that the blade file needs. I believe in 99% of the fields that will be a $field and a $crud variable, so this will work:
#php
// set the CRUD model to something (anything)
// but ideally it'd be the model of the entity that has the form
$crud = app()->make('crud');
$crud->setModel(\App\Models\Monster::class);
#endphp
#include('crud::fields.number', [
'crud' => $crud,
'field' => [
'name' => 'price',
'label' => 'Price',
'prefix' => '$'
]
])
This way, you only load the bits you actually want (the inputs), without the overhead of a CrudController. You can point the form to your custom controller and do the saving yourself. What you need to pass for a $field above is a Backpack field definition in array form.
This way is super-simple, but it has a big downside if you ask me. The field definition has to be 100% correct and complete, you lose all the magic and assumption logic that Backpack usually does to make your life easier when you add field using addField(). That's why in most cases I think it's more convenient to go with Option B.
Option B
Instead of manually loading all each field Blade view, add them using addField(), then load all of them just like Backpack does it in the Create or Update operation:
#php
$crud = app()->make('crud');
$crud->setModel(\App\Models\Monster::class);
$crud->addField([
'name' => 'price',
'label' => 'Price',
'prefix' => '$'
]);
#endphp
<form method="post">
#include('crud::form_content', [ 'fields' => $crud->fields(), 'action' => 'create' ])
</form>
The above will produce an output like this:
The benefit of this second option is that you can "forget" to mention stuff in the field definition and Backpack will assume it, you can use the fluent syntax, you can use most Backpack features, really...
I have a form that contains a drop down and a checkbox. The checkbox controls whether or not the dropdown is "disabled" (grayed out) from the initial rendering of the form. I have JS that when you uncheck the box it enables the field and now allows a user to change the value. That all works.
When you uncheck the box AND change the value in the drop down Symfony only persists the value for the checkbox and ignores the new value in the dropdown. I assume that's because symfony knew the box was disabled when the form was initially rendered and ignores any new values posted from changing that value.
I checked the profiler and it is submitting both the new value for the checkbox (1) and also the new value for the dropdown in the post fields.
The doctrine tab of the profiler only shows the checkbox value being updated but nothing for the new dropdown value.
Hopefully someone can point me in the right direction. Hopefully some simple option that tells symfony to update everything whether or not it was initially rendered as a disabled field.
You will need to use readonly attribute, and use JS or jQuery to change the status of the field. I did this in Symfony 4
->add('someInput', TextType::class, array(
'attr' => array(
'readonly' => true,
),
));
then use JS to modify it dynamically based on some value
if( someCondition ){
$(input).prop('readonly', false).attr('required',true);
} else{
$(input).prop('readonly', true).attr('required',false);
}
I'm using HTML::FormHandler to create some forms, and I'd like to be able to validate any select fields on the form by making sure that whatever value submitted was actually a value given to the user. Right now this is how I have my select field set up:
has_field 'choice' => (
type => 'Select',
label => 'Choice',
options => [{value=>"1",label=>"One"},{value=>"2"=>label=>"Two"}],
empty_select => '---Choose an Option---',
apply => [{
check => ['1','2'],
message => 'Must be a value in the list.',
}],
);
Right now this works, but I was wondering if there was a more elegant way to do it? Since HTML::FormHandler already knows what options it has for the field, is there any way to just tell HTML::FormHandler to validate that the what the user choice is in fact one of those options? I've looked through the documentation and can't seem to find it anywhere, but it seems like something that would make sense to have for a field with predefined values. Thanks!
According to the code for HTML::FormHandler::Field::Select this check is already done. Have you tried it?
Is there some parameter or workaround I can use to have the form helper render a field as if it were required by the model even if the model does not actually require it?
(I'm looking for the cakephp required rendering type, not the actual 'required' form property making this a different question than Cakephp Form Helper)
All the "required" style is is a specific CSS class you can set yourself:
$this->Form->input('foo', array('div' => array('class' => 'input text required')));
I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.
Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's not how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?
I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.
The final and REALLY correct answer is to add an option to the element :
$this->addElement('checkbox', 'my_element', array(
'label' => 'My Element Label',
'name' => 'my_element_name',
'disableHidden' => true
));
Zend_Form_Element_MultiCheckbox is what you're looking for.
The standard Checkbox element is meant to represent "yes/no" scenarios.
You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.
I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is
if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {
What exactly are you trying to do?