adamwathan / bootforms

Rapid form generation with Bootstrap 3 and Laravel.
MIT License
417 stars 103 forks source link

Feature: Ability to set default for the checkbox field with parameter #12

Closed sintemaa closed 10 years ago

sintemaa commented 10 years ago

Currently you can set the value of the checkbox to checked on not with one of the following:

->defaultToChecked() ->defaultToUnchecked()

However these methods do not take a parameter. When creating an update from I don't now when writing the view whether the default should be checked or not, as it depends on the record being edited.

What I would like to be able to do is pass an optional boolean parameter to the methods above. If the parameter is ommited it defaults to true. However if you pass false it does the opposite. E.g. ->defaultToChecked(false) would default to not checked. This way I can write some little conditional code in the parameter.

For example:

BootForm::checkbox('Active', 'active')->defaultToChecked($user->active?true:false)

N.B. in the code I see there is a protected method setChecked which basically works as I want it to, but then itis protected and not for the default value.

adamwathan commented 10 years ago

Good idea, added a "defaultCheckedState" method (it's actually part of the Form package rather than Bootforms) that takes true or false, left the other ones to take no parameters.

You'll have to be using "dev-master" to see this change for now until I tag a new version. Let me know if it's working as you expect! Thanks.

sintemaa commented 10 years ago

When I tried to test it by changing it to "dev-master" in composer.json and running composer update it gotthe following error

  Problem 1
    - Installation request for adamwathan/bootforms dev-master -> satisfiable by adamwathan/bootforms[dev-master].
    - adamwathan/bootforms dev-master requires adamwathan/form dev-master -> no matching package found.

Not a expert on composer so no idea how or why this happens

I managed to force it my explicitly requiring adamwathan/form dev-master before bootforms in composer.json though.

Testing it was simple & it seems to work just fine, nicely reducing this from 5 lines (@if @else @endif) to a single line in my view definition