2

I have third-party CSS, and I want to use it in a Yii2 ActiveForm.

The problem here is: How should I give the classes to a form field? I tried the following but I am not able to get the desired output.

<div class="checkbox checkbox-success">
    <?= $form->field($model, 'is_sold')->checkbox(); ?>
</div>

I also tried the following:

<div class="checkbox">
    <?= $form->field($model, 'is_sold')->checkbox(['class'=>'checkbox-success']); ?>
</div>

But it renders the HTML code like this:

<div class="checkbox checkbox-success">
<div class="form-group field-mainads-is_sold has-success">

<input type="hidden" name="MainAds[is_sold]" value="0">
   <label>
     <input type="checkbox" id="mainads-is_sold" name="MainAds[is_sold]" value="1">
     Mark as Sold
   </label>
</div>        
</div>

And the desired output I want is something like the following:

<div class="checkbox checkbox-success">
    <input class="styled styled" id="checkbox10" type="checkbox">
    <label for="checkbox10">
        This too
    </label>
</div>

I have also tried this:

 <?= $form->field($model, 'is_sold', ['options' =>  ['class' => 'checkbox checkbox-success']])->checkbox([] ,false) ?>

But it didn't work as well, and it gave output like below:

<div class="checkbox checkbox-success field-mainads-is_sold has-success">
    <label class="control-label" for="mainads-is_sold">Mark as Sold</label>
    <input type="hidden" name="MainAds[is_sold]" value="0">
    <input type="checkbox" id="mainads-is_sold" name="MainAds[is_sold]" value="1">
</div>

The first checkbox in the below image is what I get if I write the HTML code directly, but I want to do it using an ActiveForm field. The second checkbox in the image is what I get with form field code.

enter image description here

3 Answers 3

3

I found a solution. Try this:

<?php $checkboxTemplate = '<div class="checkbox i-checks">{beginLabel}{input}{labelTitle}{endLabel}{error}{hint}</div>'; ?>
<?= $form->field($model, 'rememberMe')->checkbox(['template' => $checkboxTemplate]); ?>

And my result:

<div class="form-group field-loginform-rememberme">
    <div class="checkbox i-checks">
        <label for="loginform-rememberme">
            <input type="hidden" name="LoginForm[rememberMe]" value="0">     
            <input type="checkbox" id="loginform-rememberme" name="LoginForm[rememberMe]" value="1" checked="">
            Remember Me
        </label>
        <p class="help-block help-block-error"></p>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

If you are looking for materialize css design, this template route works.

enter image description here

<?
    $checkboxTemplate = '<label>Terms and Conditions</label><div class="switch">{beginLabel}No{input}{labelTitle}<span class="lever"></span>Yes{endLabel}{error}{hint}</div>';
    echo $form->field($model, 'terms', ['options' =>  ['class' => 'form-group required']])->checkbox(['template' => $checkboxTemplate])->label('');
?>

Comments

0

Try Something like : -

 <?= $form->field($model, 'is_sold' ,['options' =>  ['class' => 'checkbox checkbox-success']])->checkbox([] ,false) ?>

1 Comment

i meant about the look and feel of the checkbox. Its not what it should display using the css.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.