mercredi 13 décembre 2017

How to set custom template for checkbox ActiveField on Yii2

i have some problems with checkbox in Yii2. I've read this question and i have created a global custom template for all ActiveField in this way:

namespace common\widgets;

use yii\helpers\ArrayHelper;
use yii\widgets\ActiveField;

class SaCustomFields extends ActiveField{

    public $labelOptions = [ 'class' => 'col-sm-12 control-label text-left'];

    public $inputOptions = ['class' => 'form-control input-xs']; 

    public function init(){
        $this->template ="{label}
                            <div class='col-md-12 xs-pt-5'>{input}</div>
                            <div class='col-md-12 xs-pt-5'>{hint}\n{error}</div>";

        $opt = ['class' => 'form-control input-xs-123'];
        $this->textInput($opt);
        parent::init();
    }
}

and apply it like this

$form = ActiveForm::begin([
    //other options
    'fieldClass' => 'common\widgets\SaCustomFields'
}

For input fields and select fields works fine. My Question is: how to set a custom template for checkbox?

I try something like this

$form->field($model, 'test',
    [ 'template' => '<div class="be-checkbox">{input} {label} </div>'])->checkbox();

and this

$form->field($model, 'test')->checkbox([ 'template'=> '<div class="be-checkbox">{input} {label} </div>']);

but nothing change.

My idea was also to rewrite this method

public function checkbox($options = [], $enclosedByLabel = true){
       }

but i think it's not a good idea, and i don't know how to rewrite it.

So how i can set a custom template for checkbox?




Aucun commentaire:

Enregistrer un commentaire