Asked  1 Year ago    Answers:  5   Viewed   3 times

in my form type i have this :

    $builder
        ->add('options', 'choice', [
            'choices'  => $choices,
            'multiple' => true,
            'expanded' => true,
            'label'    => false
        ])
    ;

choices is an array :

$choices = [
     'val1' => 'val1',
     'val2' => 'val2',
     'val3' => 'val3'
];

Great ! now i want to categorized my choices with an array like this:

$choices = [
     'label1' => [
        'val1' => 'val1',
        'val2' => 'val2',
     ],
     'label2' => [
        'val3' => 'val3',
        'val4' => 'val4',
     ],
     'label3' => [
        'val5' => 'val5',
        'val6' => 'val6',
     ],
];

So i want result like below

what is the best way to achieve this?

 Answers

2

You can override a widget for this choice field and manually render labels

Like this (in your form tamplate):

{% form_theme putYourFormNameHere _self %}

{% block _putYourFormNameHere_options_widget %}
    <div {{ block('widget_container_attributes') }}>
    {% for group_label, group in choices %}
        {%- if group is iterable -%}
        <div>
            <label><b>{{ group_label|trans({}, translation_domain) }}</b></label>
            {% for key, choice in group %}
                <div>
                    {{- form_widget(form[key]) -}}
                    {{- form_label(form[key]) -}}
                <div>
            {% endfor %}
        </div>
        {%- endif -%}
    {% endfor %}
{% endblock %}
Saturday, May 29, 2021
 
TMichel
 
4

Just use this part of code and it works

if($loginForm->isSubmitted() && $loginForm->isValid()){
  //code
  //don't forget the return statement
}

if($registerForm->isSubmitted() && $registerForm->isValid()){
  //code
  //don't forget the return statement
}
Saturday, May 29, 2021
 
tika
 
3

You have to pass an instance of SymfonyComponentFormFormView instead of SymfonyComponentFormForm to your view.

Fix this using ...

... ->getForm()->createView();

FormBuilder::getForm builds the Form object ... Form::createView then creates a FormView object.

Saturday, May 29, 2021
 
fret
 
2

I never ended up finding a better method than the one I am going to list below. I do not use this code much anymore, and if there is a better solution, I hope someone has it posted somewhere.

What I ended up doing was creating an ArrayList to hold the selected names. If the name is selected, the name is pushed into the ArrayList, and if it is unchecked, it is popped from the list. When afterTextChanged is selected, the list is iterated through and the names are checked if they are currently listed in the adapter. When you finish with the selection process and want to continue, I clear the EditText to clear the filter, therefore populating the complete list in the ListView and setting all of the contacts to selected if they exist in the ArrayList.

Note, I use a custom adapter for a list of contacts that only list names, so this solution may get more confusing if you use other types of data, and I look at this method as a hacked solution:

/** Used for filter **/
private TextWatcher filterTextWatcher = new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        ListView listview = getListView();

        SparseBooleanArray checked = listview.getCheckedItemPositions();
        for (int i = 0; i < ContactsList.length; i++) {
            if (checked.get(i) == true) {
                Object o = getListAdapter().getItem(i);
                String name = o.toString();
                // if the arraylist does not contain the name, add it
                if (selected.contains(name)){ 
                    // Do Nothing
                } else {
                    selected.add(name);
                }
            }
        }           
    } //<-- End of beforeTextChanged

    public void onTextChanged(CharSequence s, int start, int before, int count) {           
        adapter.getFilter().filter(s);              
    } //<-- End of onTextChanged

    public void afterTextChanged(Editable s) {
        ListView listview = getListView();
        // Uncheck everything:
        for (int i = 0; i < listview.getCount(); i++){
            listview.setItemChecked(i, false);                  
        }

        adapter.getFilter().filter(s, new Filter.FilterListener() {
            public void onFilterComplete(int count) {
                adapter.notifyDataSetChanged();
                ListView listview = getListView();
                for (int i = 0; i < adapter.getCount(); i ++) {
                    // if the current (filtered) 
                    // listview you are viewing has the name included in the list,
                    // check the box
                    Object o = getListAdapter().getItem(i);
                    String name = o.toString();
                    if (selected.contains(name)) {
                        listview.setItemChecked(i, true);
                    } else {
                        listview.setItemChecked(i, false);
                    }
                }

            }
        });         
    } //<-- End of afterTextChanged

}; //<-- End of TextWatcher

You don't want to use Indexes when using the filters because index 1 might be something in one ListView, and it might be another ListView item when you change the filter.

Thursday, August 5, 2021
 
UrbKr
 
2

In your ORM mapping definition, you have to define motion as a boolean instead of a smallint. And FYI, Symfony interprets TINYINT as boolean and any other integer SQL types as integers.

MySampleBundleEntitySubject:
    type: entity
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:

        // ...

        motion:
            type: boolean
Monday, December 20, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share