Yii2 form checkbox template - forms

I set the form parameters:
<?
$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
]);
?>
As a result, it still turns a standard form with standart classes:
<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>
Why is it adding the template as the input attribute?

You haven't provided the HTML template that you are trying to follow or integrate here in the checkbox, but regarding the template being added as the attribute of the input field is quite logical as you are passing it as the option of the checkbox rather than the field option.
You need to provide the template as an array to the third parameter option of the field see below
<?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']
]
);

Related

yii2 validate form element from model without using activeform

I want to use the simple form tag & element like
<form name="formname" action="" method="post">
<input type="text" name="title" value="" />
</form>
I want to validate all fields on client side & server side using yii model.
Model validations can easily apply with activeform but i don't want to use activeform.
Any easy way to validate the form fields on client & server both sides?
Use beginForm() method. And try something like below.
use yii\helpers\Html;
<?php $form = Html::beginForm()([
'method' => 'post',
'name' => 'formname',
]); ?>
<?= Html::textarea->textarea(['rows' => 6, 'name'=>'title'])->label(false) ?>
<div class="form-group">
<?= Html::submitButton('POST', ['class' => 'btn btn-primary']) ?>
</div>
<?php Html::endForm() ?>
and then in your model
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// $model->addRule(['fieldname'], 'string', ['max' => 50]);
}

Zend Form Rendering and Decorators (Use correctly with bootstrap)

The Bootstrap Example Code
http://getbootstrap.com/css/#forms
Copying a simple email input element from getbootstrap.com suggests we format the HTML in the following way:
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input id="exampleInputEmail1" class="form-control" type="email" placeholder="Email">
</div>
Above we have a <label> tag that closes straight after it's text content, "Email address".
I would now like to create the same form group using Zend Framework.
module/MyApp/src/MyModule/Form/MyForm.php
namespace MyModule\Form;
use Zend\Form\Form;
class MyModuleForm extends Form {
public function __construct($name = null)
{
$this->add(array(
'name' => 'email_address',
'type' => 'Email',
'options' => array(
'label' => 'Email address',
),
'attributes' => array(
'class' => 'form-control',
'placeholder' => 'Email'
)
));
The Zend Framework generated code
<div class="form-group">
<label>
<span>Email address</span>
<input class="form-control" type="email" placeholder="Email" id="exampleInputEmail1">
</label>
</div>
In the above HTML you can see that Zend has not closed the <label> tag. Instead the <label> ecapsulates its children.
How do I change the way the Zend rendering works?
I assume you are using ZF2 FormRow view helper to render your form element in your view script, e.g. $this->formRow($this->form->get('email_address'));
To render it differently you need to use the following view helpers
FormLabel
FormText
FormElementErrors
If for example you wanted to render as a definition list you would use something like
<dl class="zend_form">
<dt><?php echo $this->formLabel($this->form->get('email_address')); ?></dt>
<dd><?php echo $this->formText($this->form->get('email_address')); ?>
<?php echo $this->formElementErrors($this->form->get('email_address')); ?></dd>
</dl>
I hope this points you in the right direction

daterangepicker and codeigniter form not working

I'm using an example of bootstrap-datetimepicker in my codeigniter project.
I create a form with only the datetimepicker input.
view:
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open('/calendario/nadevent/', $attributes); ?>
<div class="form-group">
<label for="reservationtime">Fecha:</label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar"></i>
</span>
<input type="text" name="reservationtime" id="reservationtime" class="form-control" value="<?php echo set_value('reservationtime') ?>" />
</div>
</div>
<?php echo form_submit( 'submit', 'Submit','class="btn btn-default"'); ?>
<?php echo form_close(); ?>
JS:
$('#reservationtime').daterangepicker({timePicker: true, timePickerIncrement: 30, format: 'DD-MM-YYYY hh:mm:ss', timePicker12Hour: false, separator: '/'});
The datepicker works fine but when i submit the form, I want to format the data and split into an array:
Controller:
$this->form_validation->set_rules('reservationtime', 'reservationtime', '');
$array = explode('/',set_value('reservationtime'));
Then I try to insert the values:
form_data = array(
'start' => $array[0],
'end' => $array[1]
;
But if I print the array, its empty:
Array ( [start] => [end] => )
Wha'ts wrong with the code?
Thanks for the help :)
You really not getting data from your form, can you dump the $_POST?

Yii radio buttons issue

i generate radio buttons using Yii
the code looks like
<span class="gender">
<?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'male')); ?>
</span>
<span class="gender">
<?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'female')); ?>
</span>
It's generate next HTML code
<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">
<input value="male" class="male" name="web\models\register[gender]"id="web\models\register_gender" type="radio"></span>
<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">
<input value="female" class="female" name="web\models\register[gender]" id="web\models\register_gender" type="radio">
</span>
when i get POST from this form, if i checked female it returns female, but if i cheked male it returns 0. I think it is because of identical inputs ids. But how can i avoid it?
You can configure activeRadioButton to not output the "guard input" with value 0. Do this by passing the parameter 'uncheckValue' => null as part of your options array:
echo CHtml::activeRadioButton($model,'gender',
array('value' => 'male', 'uncheckValue' => null));
echo CHtml::activeRadioButton($model,'gender',
array('value' => 'female', 'uncheckValue' => null));
That said, using activeRadioButtonList is the best choice here. You can configure its template parameter to specify your custom HTML:
$genders = array('male' => 'male', 'female' => 'female');
$options = array(
'template' => '<span class="gender">{input</span>',
'uncheckValue' => null,
);
echo CHtml::activeRadioButtonList($model, 'genger', $genders, $options);

How to add name as show[xyz], show[abc], etc for multiple checkboxes in zend form

Hii... I want multiple checkboxes to be displayed as and so on...name="show[adgroups]", how can i do this using zend form code?? Please see below example how I wanted my output to be viewed:
Instructions
<div class="fieldgrp">
<label for="show_adgroups">Campaign/Ad-groups</label>
<div class="field"><input type="checkbox" name="show[adgroups]" id="show_adgroups" class="" value="adgroups" checked="checked" /></div>
</div>
<div class="fieldgrp">
<label for="show_keywords">Keywords</label>
<div class="field"><input type="checkbox" name="show[keywords]" id="show_keywords" class="" value="keywords" checked="checked" /></div>
</div>
Ignoring the "why", you can achieve what you want using a subform.
Simply name the subform "show" and add your "adgroups" and "keywords" checkboxes to it.
$form = new Zend_Form;
$show = new Zend_Form_SubForm();
$show->addElement('checkbox', 'adgroups', array(
'label' => 'Campaign/Ad-groups',
'checked' => true
));
$show->addElement('checkbox', 'keywords', array(
'label' => 'Keywords',
'checked' => true
));
$form->addSubForm($show, 'show');