Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Sticky checkbox
  • I am trying to use Form::checkbox()
    and was wondering if there is ( like for Form::select() ) a way to include a variable that will determine if the checkbox is checked or not.
    thank you.
  • I do it this way:
    $attributes = array(
        'name' => 'checkboxname',
        'value' => 1,
        'checked' => 'checked'
    );
    $checkbox = \Form::checkbox($attributes);
    
  • Hi Santiago,
    the way you have it , it will always give a checked box.
    what I was looking for instead is a way to give it a value to check against : something along the lines if( $condition ==true) {$checked='checked';} I know CI has a holder for a variable you can use for this purpose, this avoids repetitive conditional code.
  • Not the best way, but the only I could find.
    $attributes = array(
        'name' => 'checkboxname',
        'value' => 1,
    );
    
    if($dbvalue === 1)
    {
        $attributes['checked'] = 'checked';
    }
    
    $checkbox = \Form::checkbox($attributes);
    

    (Or you can extend the form class and do it the right way. Just like in CI)
  • thanks Santiago. that's exactly how i have it now. I was hoping that i missed something in the Docs... I could extend the class myself but i think this is something that should be built in. it isn't that much to add.. i know the guys are busy plenty making Fuel rock ...... but this is perhaps worth requesting :)
  • For a checkbox, only a 0/1 value is relevant and directly linked to "checked='checked'". Maybe some more intelligence is needed in the method?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion