0

I'm searching for a way to submit the statsus of a checkbox from a symfony controller to the twig template. So that the User doesn't need to check again if the form was sumitted incomplete and some further input is needed.

For the reason that there will be more than one checkbox as an array, I'm stucking: There will be submitted an id ({{ usergroup.id }}) and I thought about using this number to check the status by this id like:

    {% for usergroup in userGroups %}   
    <input type="checkbox" name="staffdata" value="{{ usergroup.id }}"{%if isChecked{{ usergroup.id }} %}checked="checked" >{& endif %}>{{ usergroup.name }}
    {% endfor %}

Of course this is not working - like I expect :)

Can anyone give me a hint how to get this working?

EDIT: Here is the actual state: The outputt looks like the script here: http://www.1stwebdesigns.com/blog/development/multiple-select-with-checkboxes-and-jquery . A selectbox with nested checkboxes.

Therefor, I need to get all values as a checkbox, not as a choice. Actually, I m doing this by giving the block_choice_widget a new layout :)

{% block choice_widget_collapsed -%}
//.....

    {% if empty_value is not none -%}
        <li><label><input type="checkbox" {% if required and value is empty %} checked="checked"{% endif %} name="staff-member-usergroup" value="{{ usergroup.id }}">{{ empty_value|trans({}, translation_domain) }}</label></li>

    {%- endif %}
   //......

{%- endblock choice_widget_collapsed  %}

This is working but to me it seems not the best solutions. But as I'm new in symfony, it is a working solution:) Is there a better way or a possibility to get multiple checkboxes?

1 Answer 1

2

you need to use data option in form builder to set state of checkbox; here is a documentation.

sample code:

$builder->add('active', 'checkbox', array(
    'label'     => 'Is active?',
    'required'  => false,
    'data' => $entity->getActive()
))
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, i found out how to handle it. But there is one problem: I'm working with multiselect jquery ui. There will bee selectable checkboxes inside a selectfield. Theese Field has to be filled like taht:$em = $this->getDoctrine()->getManager(); //get the database manager $userGroups = $em->getRepository('RrUserBundle:Group')->findAll(); // get the profiles and after taht i placed it into the template with the code above
I didn't understand what you mean, but you should know that checkbox is a boolean type, select (entity, choice) field is different type, they are depend on doctrine's schema (field type). So, if you define field type as boolean in schema then you must create checkbox type in form builder, if field type is some association or any other type (not boolean) then you can't create a single checkbox type for that type.
It is a bit mor complicative. I have a list of values which i will place as checkboxes inside a select (jquery multiselect). But its not multiple choice as it is used in the follwing post stackoverflow.com/questions/11466287/…. See my edit above
Where does $entity come from? Must I use form events in order to get access to the object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.