marvinlabs / laravel-html-bootstrap-4

Bootstrap 4 fluent HTML builder
MIT License
42 stars 15 forks source link

Unable to change checkbox input id #43

Closed benkelukas closed 6 years ago

benkelukas commented 6 years ago

Hi, when using the checkBox() method, I'm unable to change the checkbox id and leave the name different, eg. input name = permissions, id of input: permission-1;

Here is the code I've tried

@forelse($permissionsChunk as $permission)
    {!! bs()->checkBox()->id("permission-{$permission->id}")
->name('permissions[]')->description($permission->description) !!}
@empty
@endforelse

But id stays the same as input name. I think the problem is in this line (219): https://github.com/marvinlabs/laravel-html-bootstrap-4/blob/f7a63019881c60d48e782de9f3af1cfbd26c339a/src/Bootstrap/Traits/BuildsForms.php#L217-L221 It should somehow accept overriden id if the method id is called on the returned element object

Can you look into it please? Or am I doing something wrong? Thanks.

vpratfr commented 6 years ago

The ID is in fact overwritten when the control gets assembled (the checkbox is in fact a div + label + input)

https://github.com/marvinlabs/laravel-html-bootstrap-4/blob/f7a63019881c60d48e782de9f3af1cfbd26c339a/src/Bootstrap/Elements/CheckableButton.php#L111-L114

And that function always indeed transforms the name into an ID:

https://github.com/marvinlabs/laravel-html-bootstrap-4/blob/f7a63019881c60d48e782de9f3af1cfbd26c339a/src/Bootstrap/Elements/CheckableButton.php#L131-L134

So in current state, I don't see a way to change the ID unfortunatly. Would you be able to submit a PR (ignore the function call if an ID is already set on the control)?

Maybe you could also tryout the changes provided in https://github.com/marvinlabs/laravel-html-bootstrap-4/pull/42 as they target specifically input names containing [] (I guess you would need to name each box as "permissions[{$permission->id}]"

Thinking outside the box, why would you need custom IDs? Would a CSS class not be enough to target the checkbox?