Yii2表单复选框模板

时间:2019-02-21 16:43:37

标签: forms templates checkbox yii2

我设置了表单参数:

<?
$form = ActiveForm::begin([
    'id' => 'activeForm',
    'action' => 'javascript://',
]);

$checkboxTemplate = '<div class="checkbox">{labelTitle}{beginLabel}{input}<span class="slider round"></span>{endLabel}{error}{hint}</div>';
echo $form->field($aclForm, tbl_RbacActions::IS_DEVELOPMENT)
    ->checkbox([
        'labelOptions' => ['class' => 'switch'],
        'template' => $checkboxTemplate
]);
?>

结果,它仍然变成带有标准类型的标准形式:

<form id="Index-form" class="row col-12 no-gutters" action="javascript://" method="post">
    <input type="hidden" name="_csrf-frontend" value="Lo7lVHTJ9wcN5rdfjK-b7AgW8L4OHEaqI9IsVofZPOl3yKkFBJqAQz-i-y7H3-mdMUmY-H1RcMRBhkU01-F2oA==">
    <div class="form-group field-aclform-is_development required">
        <input type="hidden" name="AclForm[is_development]" value="0">
        <label class="switch">
            <input type="checkbox" id="aclform-is_development" name="AclForm[is_development]" value="1" template="{input}{beginLabel}{labelTitle}{endLabel}{error}"> Is Development</label>
        <div class="help-block"></div>
    </div>
</form>

为什么要添加模板作为输入属性?

1 个答案:

答案 0 :(得分:0)

您尚未在复选框中提供要尝试遵循或集成的HTML模板,但是考虑将template添加为输入字段的attribute非常合理,因为您将其作为checkbox而不是field选项的传递。

您需要将template作为数组提供给option的第三个参数field,请参见下文

<?php
    $form = ActiveForm::begin(
        [
            'id' => 'activeForm',
            'action' => 'javascript://'
        ]
    );

    $checkboxTemplate = '<div class="checkbox">{labelTitle}{beginLabel}{input}<span class="slider round"></span>{endLabel}{error}{hint}</div>';

    echo $form->field(
        $aclForm, 
        tbl_RbacActions::IS_DEVELOPMENT, 
        [
            'template' => $checkboxTemplate
        ]
    )->checkbox(
        [
            'labelOptions' => ['class' => 'switch']

        ]
    );