Can we change the name field value in the formBuilder? - symfony

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 ?

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 :)

Symfony - checkboxtype

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
'...'
]
])

How to specify Symfony2 Bootstrap checkbox inline style?

The Symfony2 Boostrap template has a conditional switch on 'checkbox-inline'. How is this triggered?
{% if 'checkbox-inline' in parent_label_class %}
{{- form_label(form, null, { widget: parent() }) -}}
Since the conditional check is looking in parent_label_class, you can simply add to your form builder an option called label_attr, and there you can append your class.
Example:
$builder->add('checkbox', 'checkbox',
array(
'label_attr' => array(
'class' => 'checkbox-inline'
)
)
);
Which would give the following output:
<div class="checkbox">
<label class="checkbox-inline required">
<input type="checkbox" id="form_checkbox" name="form[checkbox]" required="required" value="1" />Checkbox
</label>
</div>

Formatting buttons in a form (CodeIgniter)

Just wondering how to go about giving a form in CodeIgniter a class? I've tried just formatting buttons, hoping that the submit button would change in the form, but it didn't.
echo form_open('User/feed' class='buttonClass');
echo form_submit('NF', 'News Feed');
echo form_close();
I couldn't find anything which seemed to help me online.
Thank you!
echo form_open('User/feed', array( 'class' => 'classname' ));
// will become:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/User/feed" class="classname" />
echo form_submit('NF', 'News Feed');
// will become:
<input type="submit" name="NF" value="News Feed" />
echo form_close();
// will become:
</form></div></div>
Now keep in mind, adding classes and other attributes via the array in the first line up there only adds them to the Form line. I would recomend, if you're doing this in view, writing pure html and adding in the information needed. More like:
<form method="post" accept-charset="utf-8" action="<?= base_url('User/feed'); ?>" class="classname">
<div>
Something here for the form
<input type="text" name="stuff" />
</div>
<input type="submit" name="NF" value="News Feed" class="myButton" />
</form>
Also, of note, you can create buttons using an array and assign class and other attributes that way. Such as:
$myButton = array(
'class' => 'myButton',
'name' => 'NF',
'value' => 'News Feed',
);
echo form_button($myButton);
Lastly, and I think this is what you're aiming for, you can do the same with form_submit:
$myButton = array(
'class' => 'mySubmitButton',
'name' => 'nfSubmit',
'value' => 'Submit',
);
echo form_submit($data);
Per the documentation, the form helper accepts an array as the second parameter, allowing one to apply various options, such as a class. For example:
$attributes = array(
'class'=>'myClass'
);
echo form_open('User/feed', $attributes);
Documentation
Form Helper - http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

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