-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render a Checkbox with enclosed Label in BS3 (reopen) #116
Comments
still i couldn't figure out a way how i can render a single checkbox with the control wrappers and a label |
@Bhoft you can try using |
@kartik-v as i have written in my first post i have used your kartik\form\ActiveForm,
I have mixed something you are extending from yii\widgets\ActiveField and not from yiisoft\yii2-bootstrap\ActiveField therefore your issue isn't related to the one from yii2-bootstrap and is independent. |
@Bhoft it works and behaves as expected with Since your query is on Bootstrap 3.x here is how it looks for both cases Check the Bootstrap 3.x Active Form Demo where it was tested. That demo is without enclosed label. Replacing it with enclosed label also works like below: <?= $form->field($model, 'rememberMe')->checkbox(['id' => 'remember-me-ver'], true)->label('My Custom Label (Kartik)') ?> |
You need to just set/modify the ActiveField::checkEnclosedTemplate = "{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}" You need to include your additional HTML/TEXT content within: $preLabel = "Signature<br>";
$fieldConfig = [
'checkEnclosedTemplate' => "{beginLabel}\n{$preLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}"
];
echo $form->field($model, 'rememberMe', $fieldConfig)
->checkbox(['id' => 'remember-me-ver'], true)
->label('My Custom Label (Kartik)'); |
I had shared just an example to customise the rendering that you need to adapt for your use case For example for an enclosed label I am not sure why you would want to nest another label tag inside... Just insert a markup e.g. a span or div with your own CSS class and style it based on the pre and post error state. Bootstrap should style all content inside a label with error states. You should also try with one of the enclosed OR non enclosed label to achieve this better (use |
As an example I achieved your use case for BS3.x with the following code: Set this in your view CSS file .kv-pre-label {
display: block;
margin: 0 0 5px -20px;
font-weight: bold;
}
.kv-pre-label .required {
color: red;
} Render the field $preText = $model->getAttributeLabel('signature') . ' <span class="required">*</span>';
$preLabel = Html::tag('span', $preText, ['class' => 'kv-pre-label']);
$template = "{beginLabel}\n{$preLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}";
$label = 'I declare that, to the best of my knowledge, I have no ' .
'direct or indirect conflict of interest in the valuation of this proposal.';
// $form is instance of kartik\form\ActiveForm
echo $form->field($model, 'signature', ['checkEnclosedTemplate' => $template])
->checkbox(['id' => 'kartik-custom'], true)
->label($label); Gives the following output: |
So you can wrap it in a separate function or widget to render it: class KvHelper {
public function renderCheckbox($model, $form, $attribute, $label, $options = []) {
$preText = $model->getAttributeLabel($attribute) . ' <span class="required">*</span>';
$preLabel = Html::tag('span', $preText, ['class' => 'kv-pre-label']);
$template = "{beginLabel}\n{$preLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}";
return $form->field($model, $attribute, ['checkEnclosedTemplate' => $template])
->checkbox($options, true)
->label($label);
}
} and use it in your form view: $label = 'I declare that, to the best of my knowledge, I have no ' .
'direct or indirect conflict of interest in the valuation of this proposal.';
echo KvHelper::renderCheckbox($model, $form, 'signature', $label); |
I really appreciate your help and effort 👍
As far as I understand setting the "label" option is the item-label of the checkbox. At least this is the functionality in the normal ActiveForm. As I have written above I have already found a workaround that I render a checkboxList with only one checkbox if I wanted to have a control-label for a single checkbox. I guess there must be a better difference between the item-label and the control-label for the single checkbox / radio. You can clearly see the difference for other fields e.g. textInput The item-label (set via the direct options) isn't used to render a label at all (it is only rendered as a tag attribute via HtmlOptions). The The same happens for This code would render differently for each class:
There are following kartik\widgets\ActiveForm:
with enclosedByLabel = true
with enclosedByLabel = false
Of course some of these |
@Bhoft the changes have been incorporated in this extension to equally provide support for both BS3 and BS4 and note that this change is only for checkboxes/radios. Bootstrap 4.x checkbox / radio markup for forms does not recommend enclosed labels for checkboxes and only uses non-enclosed labels. It is something which you need to consider when updating your Bootstrap related apps in future with 4.x. Though this widget will support an ability to include enclosed checkbox label in BS4 as well but it kind of deviates from the BS4 recommended style. Since in your use case you actually want 2 different labels to be displayed (one before checkbox and one after checkbox) - it is something which is not a default functionality but can be achieved with some additional config as mentioned. |
Im Ok with this, I guess i never stumbled above this issue when the singe checkbox code hasn't rendered my wished html code in some earlier versions of your class and wasn't also fixed by you to work :-) Still your checkbox function has then this issue that the LabelOptions aren't used if 'label' is set. But is guess this is a simple thing to fix.
From your documentation / Source:
|
BTW... a flipped alternative you were mentioning above. You can use the $postLabel = 'Long additional label text';
$template = "{beginLabel}\n{labelTitle}\n{input}\n{$postLabel}\n{endLabel}\n{error}\n{hint}";
return $form->field($model, $attribute, ['checkEnclosedTemplate' => $template])
->checkbox($options, true; Again use a bit of styling with CSS to change the paddings by enveloping the template in a custom container if desired. |
I haven't looked into BS4 yet (will when i have some time) so i couldn't say anything about this. Btw2 ;) |
I am not clear what is the issue you have with single checkbox. Check the active form demo pages for BS4 where for vertical, horizontal, and inline - the single checkboxes are rendered. The same demo for Bootstrap 3 is here. |
This will need to be corrected in docs |
In the bootstrap4 demo link https://getbootstrap.com/docs/4.1/components/forms/#horizontal-form there is a checkbox with a control "label". I haven't seen this in your demos. There is always a single checkbox with a single "item" label for the checkbox to click but no "control label" e.g. which could display the name of the field (which is shown on the error summary) and the required asterisk (via css see below).
Ok they haven't rendered a "label" tag for the control-label but only used a div without any classname but i guess this customizeable.
Hmm funny i couldn't find any bs4 demo where the control-labels have a required asterisk. There is only a :invalid or :valid for the form elements.. not the labels. But besides this the html code is "exactly" what i wanted to be rendered.
echo $form->field($model, 'required_field')->checkbox(['label' => $itemLabel, 'labelOptions'=> $labelOptions, 'id'=>uniqid()])->label($controlLabel); |
That is not a control label - it is just a text displayed via html markup via bootstrap grids (which is what I had mentioned earlier as additional markup If you cross check the bootstrap demo - it is a simple HTML markup: <div class="form-group row">
<div class="col-sm-2">Checkbox</div> <!-- this is what you are calling as control label but it does not control the input, it is just a text -->
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Example checkbox
</label>
</div>
</div>
</div> |
This is a good suggestion - will check that as a separate enhancement, .form-group.required .control-label::after {
content: " *";
color: red;
} |
yes i already mentioned it myself. But there isn't a huge difference between some text inside some "divs" and the same text in some "label" tag. I only called it control-label because the classes for these "attribute wise" labels have the classname "control-label" in BS3. The text is only the value of the model::getAttributeName() function. The problem with your class you couldn't render text outside (before) of the Btw. of course the "item label" is used in the single checkbox to have a clickable text for only this checkbox (and this is correct). For a checkbox-list there is a item label for each checkbox but also a control-label for the whole form-group / or field group (depends on how you name it) with e.g. the attributeName and if you use the css the required asterisk. And of course for a single checkbox it doesn't makes sense to also have the control-label pointing to the checkbox. But the Btw2 it is also allowed to have multiple labels pointing to one input, so also that shouldn't be an issue.
|
This is applicable only for BS3 - will check if I can provide an enhancement for this. |
This is resolved now via enhancement #117. Two new properties <?= $form->field($model, 'rememberMe', [
'contentBeforeField' => '<label for="signature" class="control-label">Signature</label>'
])->checkbox(['id' => 'signature']) ?> |
Also enhancement #118 addresses this comment earlier. New property |
I think this could been solved it a different / easier way. Wouldn't it be possible to have the single checkbox and radio rendered internally and only use the output of this to be added to the normal BS template for So the So depending on And then you have e.g. an
Then the label would be either showing the model attribue Name or if Maybe you also know a better solution as having an addtional showControlLabel option to control the Label output. |
Not sure that helps as it would be heavily customised markup. Note that the checkbox labels for Bootstrap styling are always by default rendered to the right of the input (this is true for both BS4 and BS3). Any content that you wish to render before the checkbox has to be a separate custom markup as per Bootstrap styling defaults. Hence in this extension, to make it simple for any input type (whether checkbox, radio, button group, normal text input, select, textarea, or any widget) a generic way to render custom markup before a specific section can be controlled via any of: and conversely markup after a section can be rendered via any of: |
Hi,
today i updated yii2-widget-activeform to version 1.5.7
and sadly the problem is now even more worse.
The attribute Label Text isn't used as the checkbox label and the custom label is not rendered any more (so the required label is missing).
Used the same php code as in the older issue:
Do you have an advice?
Also simple tests like below doesn't render correctly:
I have now troubles to downgrade because several other packages (kartik-v/yii2-detail-view) are depending ob kartik-v/yii2-builder which needs to be downgraded to get older versions of the kartik-v/yii2-widget-activeform.
I wasn't sure if a comment on a closed issue is to be seen therefore I have created a new one.
#111
The text was updated successfully, but these errors were encountered: