Symfony - checkboxtype - symfony

i have problem with checkboxtype in symfony framework. My code for creation form is:
$form = $this->createFormBuilder()
->add('table_records', CheckboxType::class)
->add('save', SubmitType::class, array('label' => 'delete'))
->getForm();
My problem is array in checkbox, because i need checkboxname for example table_records[], for many checkboxes.

Use a ChoiceType with and set expanded to true.
$builder->add('table_records', ChoiceType::class, array(
'choices' => array(),
'expanded' => true
))

Something like this, using ChoiceType is the right way to do this.
$form = $this->createFormBuilder()
->add('table_records', ChoiceType::class, array())
->add('save', array('label' => 'delete'))
->getForm();

From your question it not clear if you need:
1. Group of Multiple Checkbox , will not be named as you ask
2. Collection of Checkbox , each will be named with a index number
1.
Result example:
<form>
<input type="checkbox" name="table_records[]" value="1"/>
<input type="checkbox" name="table_records[]" value="2"/>
...
</form>
You need:
->add('table_records',ChoiceType::class,[
'multiple'=>true,
'expanded'=>true,
'choices'=>[
'label1'=>'value1',
'label2'=>'value2'
]
])
Look at multiple + expanded both are true and this will make the ChoiceType render Checkbox inside the form.
2. Result Example:
<form>
<input type="checkbox" name="table_records[0]" value="1"/>
<input type="checkbox" name="table_records[1]" value="2"/>
...
</form>
Then you need:
->add('table_records',CollectionType::class,[
'entry_type'=>CheckboxType::class,
'entry_options'=>[ //Options for CheckboxType goes here
'...'
]
])

Related

Change Symfony Form Type element's form group CSS class

Briefly: I want to edit my form elements parent div class. In this code don't add or edit the css class to my target div (which is form-group) and the follow Form Type is an element of another Form Type.
I have an Form Type like the follow:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class, [
'empty_data' => '',
'label' => 'ID',
'attr' => [
'class' => 'col-sm-3'
],
]);
$builder->add('companyName', TextType::class, [
'empty_data' => '',
'label' => 'Şirket Adı',
'attr' => [
'class' => 'col-sm-3'
],
]);
}
So this form type renderin this HTML:
<div class="form-group field-member_earning_filter"><label class="control-label"> </label>
<div id="form_filters_member" css="col-sm-11">
<div class="form-group field-integer"><label class="control-label"
for="form_filters_member_id">ID</label><input type="number"
id="form_filters_member_id"
name="form_filters[member][id]"
class="form-control col-sm-3">
</div>
<div class="form-group field-text"><label class="control-label" for="form_filters_member_companyName">Ticari
Ünvan</label><input type="text" id="form_filters_member_companyName"
name="form_filters[member][companyName]" class="form-control col-sm-3"></div>
As you can see the CSS class which I want to add for each elements group are added to form-control element. But actually I want to do this:
<div class="form-group col-sm-3 field-integer"><label class="control-label"
for="form_filters_member_id">ID</label><input type="number"
id="form_filters_member_id"
name="form_filters[member][id]"
class="form-control">
I tried to refactor this challenge at view layer (on twig side) but actually doesn't have any real block to edit that "form group" section. Additionally that it's a form type in another form type for that reason the block don't affect my code. I mean this :
{% block form_row %}
<div class="form-group col-sm-3"> <--! but this form group actually my form type's form group -->
{{ form_label(field) }}
{{ form_errors(field) }}
{{ form_widget(field) }}
</div>
{% endblock %}
Do you have any idea?
I found a solution for my problem. It's little bit workaround or hacking means but solved my problem:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class, [
'empty_data' => '',
'label' => 'ID',
'block_prefix' => ' col-sm-3'
]);
$builder->add('companyName', TextType::class, [
'empty_data' => '',
'label' => 'Şirket Adı',
'block_prefix' => ' col-sm-3'
]);
}
This form-group's div is just affecting from block_ options. As you guess the block_prefix is a prefix of class but just adding a whitespace () then it's solve my problem :)

How can I store data from entity type selectbox (Symfony)?

I have a select box created with formbuilder:
<select id="form_type" name="form[type]" class="form-control select2 select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="1">text</option>
<option value="2">hidden</option>
<option value="3">password</option>
<option value="4" selected="selected">icon</option>
<option value="5">select</option>
</select>
This is how it is created in my controller:
$formBuilder->add('type', EntityType::class, array(
'attr' => array('class' => 'form-control select2'), 'label' => 'Type',
'class' => FieldTypes::class,
'choice_label' => function ($fieldTypes) {
return $fieldTypes->getName();
}
));
$formBuilder->add('cancel', ButtonType::class, array('label' => 'cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar')))
->add('save', SubmitType::class, array('label' => 'Save','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
But when I want to save a selection I get the error message:
Argument 1 passed to App\Entity\Fields::setType() must be an instance
of App\Entity\FieldTypes or null, string given, called in
/Users/work/project/src/Controller/PagesController.php on line 242
In my entity:
public function setType(?FieldTypes $type): self
{
$this->type = $type;
return $this;
}
This is how it it stored:
$entity = $this->getDoctrine()->getRepository($EntityName)->find($data['form[id]']);
$em = $this->getDoctrine()->getManager();
foreach ($fields as $field) {
$em = $this->getDoctrine()->getManager();
$func = 'set'.$field['fieldName'];
$args = $data['form['.$field['fieldName'].']'];
$entity->$func($args);
}
$em->flush();
Use the method $form->handleRequest($request). It will populate your form using its configuration (it will instanciate a FieldType using the posted id).
Here, you add manually the "raw" value of your select, which is string containing the id of your FieldType.
edit : see the docs
According to your code, its little bit diffucult to understand but
if $field is as a string.
foreach ($fields as $field) {
$obj = $this->getDoctrine()->getRepository($fieldEntity)->find(field);
$entiy->setType($obj);
}
Try after form submit block
$entity->setType($form->get('type'));
$form->get('type') will return as a FieldTypes class.
also if you use $field instead of $field['fieldName'],
will object return.
for example,
$func = 'set'.$field;

Can we change the name field value in the formBuilder?

All of my question are in the subject :)
It's possible de customize the attribute "name" of a field in the formBuilder ?
If this is possible, can you describe the process for make this ?
Thank you for your reply in advance
You can change "name" attribute with Twig code:
{{ form_row(form.myfield, {'full_name':'myname'}) }}
You need to use "full_name" because "name" only changes the "title" attribute.
But I think that Symfony Forms uses the name to bind the form fields to the model, and if you change could have problems.
You can define field label by additional parameter.
$form = $this->createFormBuilder($task)
->add('task', 'text')
->add('dueDate', 'date', array('label' => 'Dday'))
->add('save', 'submit', array('label' => 'Create Task'))
->getForm(); //^
//|by using 3rd parameter 'options'
//|you can change 'name' of the field
EDIT: This is my formBuilder class for this
$builder
->add('type', 'choice', [
'choices' => [
'1',
'2',
'3'
],
'multiple' => true,
'expanded' => true,
])
;
I try to describe better my problem
actually, i have this html code
<div class="control-group control-filter">
<label>
<input type="checkbox" class="checkbox checkbox-orange">Foo
</label>
</div>
<div class="control-group control-filter">
<label>
<input type="checkbox" class="checkbox checkbox-orange">Foo
</label>
</div>
<div class="control-group control-filter">
<label>
<input type="checkbox" class="checkbox checkbox-orange">Foo
</label>
</div>
But now, i need to convert this code into the formBuilder, i use the choice type and i need to group by name all checkbox of this choice and make a custom class on all input element of these, can i do this ?

<select> in Symfony

How do I can create a in symfony form from the controller?
$form = $this->createFormBuilder($fupv)
->add('idUsuario', 'text')
->add('permiso', 'text')//I want a select here
->add('save', 'submit')
->getForm();
You need to use a choice Field Type.
There are various options depending on how you are populating the select.
A simple example;
$form = $this->createFormBuilder($fupv)->add('gender', 'choice', array(
'permiso' => array('a' => 'Admin', 'u' => 'User')
));
Have a look at the symfony docs for more examples.
You have to use the choice field type
->add('myField', 'choice', array(
'choices'=> array('choice1'=>'printedvalueofchoice1','choice2'=>'printedvalueofchoice2'),
'multiple'=> false,
'expanded'=> false ))
expanded set to true will turn your select into radio options

WordPress Radio Boxes - selects only the last one

So I have a form as shown bellow. its a bit long. It contains three radio boxes. Every time I select one, doesn't matter which, and then hit submit, the last radio element shows up as selected instead of the one I clicked. I var dump the option (in this case aisis_core['display_rows']) and it will say the value of the radio element i selected instead of the current on selected.
So I select lists, it will show lists but the radio box selected is no_posts. Can some one tell me what I am doing wrong?
<form action="options.php" method="post">
<input type='hidden' name='option_page' value='aisis_options' /><input type="hidden"
name="action" value="update" /><input type="hidden" id="_wpnonce" name="_wpnonce"
value="f0385965c6" /><input type="hidden" name="_wp_http_referer" value=
"/WordPressDev/wp-admin/admin.php?page=aisis-core-options&settings-updated=true" />
<fieldset>
<div class="control-group">
<label class="radio"><input type="radio" id="rows" class="display" name=
"aisis_core[display_rows]" value="display_rows" checked="checked" /> Display
posts as rows. </label>
<div class="control-group">
<label class="radio"><input type="radio" class="display" name=
"aisis_core[display_rows]" value="list" checked="checked" /> Display posts a
list. </label>
</div>
<div class="control-group">
<label class="radio"><input type="radio" id="noDisplay" class="display" name=
"aisis_core[display_rows]" value="no_posts" checked="checked" /> Display no
posts.</label>
<div class="no_posts_section borderBottom">
<div class="well headLine">
<h1>Display No Rows</h1>
<p>If you choose to display no rows please give me a url of the page or
content you would like to display instead.</p>
<p class="text-info"><strong>Note:</strong> Formatting of said content is
up you. All we do is display it.</p>
</div>
<div class="control-group">
<div class="controls">
<input type="url" name="aisis_core[index_page_no_posts]" value=
"http://google.ca" placeholder="Url" />
</div>
</div>
</div>
<div class="control-group">
<div class="form-actions">
<input type="submit" class="btn btn-primary btn-large" />
</div>
</div>
</div>
</div>
</fieldset>
</form>
The function I am using from wordpress is:
checked('radio_box_value', isset($options['display_rows']), false)
Note: radio_box_value is replaced with what ever the value of the radio box is.
In this case only the last radio box has the "checked" in it's tag, when it should be which ever one I chose.
How are the elements being created?
The following is how I create the elements, they print out what you see above in the html for the radio buttons. These are done similar to, but not exactly, zend framework.
Its pretty straight forward what were doing, create the element, add the options to the element and then return it.
I hope this gives a better picture as to how these are being created.
protected function _radio_rows_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'display_rows',
'class' => 'display',
'id' => 'rows',
'checked' => checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'display_rows', false),
'label' => ' Display posts as rows. <a href="#radioRows" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->sub_section_rows_array());
return $radio;
}
protected function _radio_list_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'list',
'class' => 'display',
'checked' => checked('list', isset($options['display_rows']) && $options['display_rows'] == 'list', false),
'label' => ' Display posts a list. <a href="#radioLists" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element);
return $radio;
}
protected function _radio_no_posts_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'no_posts',
'class' => 'display',
'id' => 'noDisplay',
'checked' => checked('no_posts', isset($options['display_rows']) && $options['display_rows'] == 'no_posts', false),
'label' => ' Display no posts.</a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->_sub_section_now_posts_array());
return $radio;
}
"checked" function seems to need the value as second parameter as is explained here http://codex.wordpress.org/Function_Reference/checked
Try like this:
protected function _radio_rows_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'display_rows',
'class' => 'display',
'id' => 'rows',
'checked' => checked('display_rows', (isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display posts as rows. <a href="#radioRows" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->sub_section_rows_array());
return $radio;
}
protected function _radio_list_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'list',
'class' => 'display',
'checked' => checked('list',(isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display posts a list. <a href="#radioLists" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element);
return $radio;
}
protected function _radio_no_posts_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'no_posts',
'class' => 'display',
'id' => 'noDisplay',
'checked' => checked('no_posts', (isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display no posts.</a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->_sub_section_now_posts_array());
return $radio;
}
This will not give warning when isn't declared the variable $options['display_rows'] (that as you said is a possibility in your case) and will pass the value to the WordPress function to compare with.
You'll want to check the value in the checked condition, not you're just checking if any value is being selected, which is always true after a submit
change
checked('display_rows', isset($options['display_rows']), false)
to:
checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'display_rows', false),
and for the list to:
checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'list', false),
I did manage to write this:
public function set_element_checked($value, $option, $key){
$options = get_option($option);
if(isset($options[$key]) && $options[$key] == $value){
return 'checked';
}
}
which does exactly what I want. compare the element value to that of the $option[$key] and if they match return checked. can be called via:
'checked' => set_element_checked('display_rows', 'aisis_core', 'display_rows');

Resources