1

I have been able to dynamically populate checkboxes in my view. But I am having problem in reading the checked checkboxes in my controller. I want id, name and value of the checkboxes. For example, if I have two checkboxes, then I want each checkbox's id, name and value indicating whether checked or not. Right now all I am getting is an array that just gives values 1.

I have the view:

<div class="control-group">
            {{ Form::label('permission-lbl', 'Permissions') }}
            <div class="controls">
                            @foreach($permissions as $p)
                {{ Form::checkbox('permissions[]',$p->id,false,array('class'=>'permission')) }}&nbsp;{{Form::label('permissions[]',$p->name)}}
                            @endforeach
            </div>
        </div>

And the controller is:

public function store() {
        $validation = new CreateGroupValidator;

        if ($validation->passes()) {
            try {
                // Create the group
                $permissions = Input::get('permissions');
                if(is_array($permissions))
                {
                    //i need each checkbox's id, name and value here                    
                }

                $group = Sentry::createGroup(array(
                    'name'        => Input::get('name')
                )); 

                Notification::success('New group was saved.');
                return Redirect::route('admin.groups.index');
            } 
            catch (\Cartalyst\Sentry\Groups\GroupExistsException $e)
            {
                Notification::error('A group with same name already exists.');
            }
        }

        return Redirect::back()->withInput()->withErrors($validation->errors);
    }

Any suggestions please?

1 Answer 1

1

The permissions array that you'll receive from the form will be in this form:

// In the form [position] => Input `Value` Attribute
[permissions] => Array ( 
       [0] => 1,
       [1] => 2,
)

Where the key is the position of the element in the array (not-useful), and the value is the attribute value of all the inputs that were checked.

In your specific case the Input Value Attribute is the value of $p->id in each iteration of the loop.

Have in mind that you will only receive the checkbox that were checked in the form, if no one is checked, you will receive an empty array.

Sign up to request clarification or add additional context in comments.

3 Comments

That means no way out to get the texts associated with each checkbox? Then can this be achieved by putting each checkbox with different ids (that is, not grouping the checkboxes with name = "permissions[]")?
@sangam a HTML Form will only send to the server the information that is inside an Input/Select/Textarea element, the label tag doesn't get send over. For your specific case I see that you are receiving the $permission id that the user selected, this way you could get the permission from the persistency object. If you really really need the name, you could do a little hack (Not Recommended) and to the value attribute put the $p->id.'-'.$p->name, and in the server do a split of what you get.
thank you for the another way around. I think having the form $p->id.'-'.$p->name would save me some database hits. Still since I am getting ids, i have better fetch associated names from db. Thank you so much.

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.