How to Modify Active Form Field in yii2 - yii2-advanced-app

Normally, this code <?= $form->field($model, 'q_36a')->checkbox() ?> will produce the following...
<div class="form-group field-tblquestion-q_36a required">
<input type="hidden" name="TblQuestion[q_36a]" value="0"><label><input type="checkbox" id="tblquestion-q_36a" name="TblQuestion[q_36a]" value="1"> Q 36a</label>
<div class="help-block"></div>
</div>
how do i modify it to make it like this?
<label>
<input name="switch-field-1" class="ace ace-switch ace-switch-5" type="checkbox">
<span class="lbl"></span>
</label>
I want to make it like this because I integrated ace admin template in my app.

Got it working.
<?= $form->field($model, 'q_36a', ['template' => "{input}<span class='lbl'></span>",])->checkbox(['class' => 'ace ace-switch ace-switch-5'], false) ?>

Related

How to make textfield automatic field when dropdown is selected in codeigniter

Hallo i'm new in codeigniter, i want to make textinput filled after selected value of dropdown, can anyone give an example for me (for models, conntroller and view) ? thx
here is my dropdown form
<div class="form-group">
<?php $id = 'id="nama" class="form-control input-md" required';
echo form_dropdown('nama',$nama_cabang,'',$id)?>
<div class="invalid-feedback">
<?php echo form_error('nama_cabang') ?>
</div>
</div>
and its my textinput
<div class="form-group">
<input class="form-control <?php echo form_error('kode_cabang') ? 'is-invalid':'' ?>"
type="text" name="kode" placeholder="Kode Cabang" />
<div class="invalid-feedback">
<?php echo form_error('kode_cabang') ?>
</div>
</div>
how can the value of textinput changed when dropdown is selected.
You could use javascript onchange listener to set the trigger, and assign the text input with the current selected value using this.value :
<div class="form-group">
<?php $id = 'id="nama" class="form-control input-md" required onchange="document.getElementById(\'kodeCabang\').value = this.value"';
echo form_dropdown('nama',$nama_cabang,'',$id)?>
<div class="invalid-feedback">
<?php echo form_error('nama_cabang') ?>
</div>
</div>
And add an id attribute (id="kodeCabang") to set the target element :
<div class="form-group">
<input class="form-control <?php echo form_error('kode_cabang') ? 'is-invalid':'' ?>" id="kodeCabang"
type="text" name="kode" placeholder="Kode Cabang" />
<div class="invalid-feedback">
<?php echo form_error('kode_cabang') ?>
</div>
</div>

Multiple form in the same ctp file with same controller in cakephp 3

I have an issue with a 2 different forms in one ctp file.
explanation : I want to use two forms related to different actions in same controller. I use the first form to add 2 text field a table and I use the second form just to search and retrieve the data.
MY ctp:
Form 1 adding message and Email
<?= $this->Form->create($message) ?>
<div class="form-group">
<label for="name" class="col-form-label">Name</label>
<input name="name" class="form-control" id="name" placeholder="Your Name" type="text">
</div>
<div class="form-group">
<label for="email" class="col-form-label">Email</label>
<input name="email" class="form-control" id="email" placeholder="Your Email" type="email">
</div>
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
$this->Form->end() ?>
Form 2 Searching field :
<?= $this->Form->create(null, ['url' => ['action' => 'search']]) ?>
<div class="form-group">
<label for="what" class="col-form-label">What?</label>
<input name="what" class="form-control" id="what" placeholder="What are you looking for?" type="text">
</div>
<div class="form-group">
<?php echo $this->Form->input('country_id', [
'options' => $countries,
'id' => 'country_id',
'label' => ['text' => __('Where?')]
]); ?>
</div>
<button type="submit" class="btn btn-primary width-100">Search</button>
<?= $this->Form->end() ?>
So i clicked on submit it works fine but when I clicked on search it doesn't go to the desired action it is still in the same action . Thank you!
This code is not doing what you think it's doing:
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
$this->Form->end() ?>
It will echo the submit button but NOT the form end tag. You then open another form, but the browser may interpret this as a bad tag and ignore it. (Technically, I think the browser behaviour towards this malformed HTML is undefined, so you might get different behaviour from different browsers.)
Try this instead:
<?php
echo $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
echo $this->Form->end();
?>
or
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
echo $this->Form->end() ?>
or
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]) .
$this->Form->end() ?>
I'd recommend the first option as being much clearer code and less prone to accidental breakage with future edits; I would never allow either of the latter two on a project I was managing.
The problem was solved by replacing this code:
<?= $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
$this->Form->end() ?>
By this :
<?php
echo $this->Form->button('Submit', ['class'=> "btn btn-primary large icon float-right"]);
echo $this->Form->end();
?>

cakephp2 override Form input action

default cakephp echo $this->Form->input('name');
returns something like this
<div>
<label for="mdl.name">name</label>
<input type="text" id="mdl.name" name="mdl.name" />
</div>
but I want different let's say I want the the following
<div class="form-group more classes">
<h3>name</h3>
<div>
<span class="extra span"></span>
<input type="text" class="form-control" id="mdl.name">
<span class="extra span"></span>
</div>
<label for="mdl.name">name</label>
<span class="another span"></span>
</div>
I saw /lib/cake/view/Helper/FormHelper.php copied to app/view/Helper
but have not seen any div, label or input tags
What you might be looking for is:
echo $this->Form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---'
));
Which will output:
<div class="input">
--before--
<label for="UserField">Field</label>
--between---
<input name="data[User][field]" type="text" value="" id="UserField" />
--after--
</div>
Reference: CakePHP 2.x FormHelper

cakephp login form not working

I've made a login form and now I need to have the same code in one of my Cakephp project but it doesn't work..
<!-- LOGIN SECTION START -->
<section id="login">
<div class="container">
<div class="row">
<div class="Absolute-Center is-Responsive">
<h1 class="text-center form-login-title">Log In</h1>
<div class="col-sm-12 col-md-12 col-md-offset-0">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
<div class="form-group inner-icon right-icon"> <!--USER NAME-->
<i class="glyphicon glyphicon-user"></i>
<input class="form-control" type="text" name='username' placeholder="username"/>
</div>
<div class="form-group inner-icon right-icon"> <!--PASSWORD-->
<i class="glyphicon glyphicon-lock"></i>
<input class="form-control" type="password" name='password' placeholder="password"/>
</div>
<?= $this->Form->end(__('Sign In')); ?> <!-- SUBMIT BUTTON -->
<div class="form-group text-center">
Forgot Password
</div>
</div><!-- /.col-sm-12 -->
</div>
</div><!-- /.row -->
</div><!-- /.container -->
</section>
<!-- LOGIN SECTION END -->
So with that snippet does not let me log in (the back end is not the problem)
Oh, and how can I change the submit button style ? I've tried <?= $this->Form->end(__(Sign In), array('class'=>'btn-primary')); ?>
but it doesn't help much :(
Thanks
You should use the CakePHP helpers, so instead plain HTML:
Change this:
<input class="form-control" type="text" name='username' placeholder="username"/>
Into this:
<?php
echo $this->Form->input('username', array(
'class' => 'form-control',
'placeholder' => 'username',
'label' => false
));
?>
And this:
<input class="form-control" type="password" name='password' placeholder="password"/>
Into this:
<?php
echo $this->Form->input('password', array(
'type' => 'password',
'class' => 'form-control',
'placeholder' => 'password',
'label' => false
)); ?>
CakePHP Helper will print the input fields with the correct name values for each one, so in your controller you receive the correct data through the array $this->request->data.
For the button style try:
<?=$this->Form->end('Sign In', array('class'=>'btn btn-danger'))?>
Like Ricardo mentioned,use CakePHP syntax and conventions.
Also it will be more helpful if you provide your controller actions. Probably your action is wrong.
For more things about forms and auth here:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html

How change standard decoration in zend form

in my Directory Form i have FormArena
$this->addElement('radio', 'warrior',array(
'decorators' => array(
'ViewHelper',
),
'disableLoadDefaultDecorators' => true,
'separator' => '',
));
On my view page i have
<?php echo $this->forms['formarena']->getItem('warrior'); ?>
And in controller i have
$getWarriorsList = $this->objArena->getActiveUsers($this->varUserData['id']);
$objFormArena = new FormArena();
if (!empty($getWarriorsList)){
foreach($getWarriorsList AS $varWarriors)
$varOptions[$varWarriors['id_users']] = $varWarriors['nick'];
$objFormArena->warrior->addMultiOptions($varOptions);
unset($varOptions);
}
$this->view->forms = array(
'formarena' => $objFormArena,
);
Ok so it is easy. I take data from base and add option to view by controller. But when i see source code on page i have:
<div class="radio">
<label for="warrior-27">
<input type="radio" name="warrior" id="warrior-27" value="27">makapaka</label>
<label for="warrior-29">
<input type="radio" name="warrior" id="warrior-29" value="29">Kasia</label>
</div>
but i need do
<div class="radio">
<input id="warrior-27" type="radio" name="warrior" value="27">
<label for="warrior-27">makapaka</label>
<input id="warrior-29" type="radio" name="warrior" value="29">
<label for="warrior-29">Kasia</label>
</div>
What should i need to do. I tried search from web but i have still nothing from 2 days :(
The current option does not allow rendering in the exact format you wished.
It can only be either
<div class="radio">
<label for="warrior-27">
<input type="radio" name="warrior" id="warrior-27" value="27">makapaka</label>
<label for="warrior-29">
<input type="radio" name="warrior" id="warrior-29" value="29">Kasia</label>
</div>
or
<div class="radio">
<label for="warrior-27">
makapaka
<input type="radio" name="warrior" id="warrior-27" value="27"></label>
<label for="warrior-29">
Kasia
<input type="radio" name="warrior" id="warrior-29" value="29"></label>
</div>
To render your form element as you mentioned, you need to create either a custom view helper and pass it as an option to the ViewHelper decorator or use the ViewScript decorator to generate a custom output.
Personally, I would prefer the ViewScript decorator for its easy of use.
Example of use :
$this->addElement('radio', 'warrior',array(
'decorators' => array(
array('ViewScript', array('viewScript' => 'decorator.phtml'))
)
));
and in your decorator.phtml
<div class="radio">
<?php foreach($this->element->getMultiOptions() as $value => $label): ?>
<input id="<?php echo $this->element->getName() ?>-<?php echo $value ?>" type="radio" name="<?php echo $this->element->getName() ?>" value="<?php echo $value ?>">
<label for="<?php echo $this->element->getName() ?>-<?php echo $value ?>"><?php echo $label ?></label>
<?php endforeach ?>
</div>
NB : This script file can be used for all your form radio elements
Hope it helps