cakephp form input autoFocus - cakephp-2.5

I am using cakephp form helper and I want to set the input attribute autoFocus. The best I can get is autoFocus="autoFocus" which is not valid. I need to set it to just autoFocus.
<input type="text" name="fname" autofocus>
My code is:
echo $this->Form->input('title', array('class'=>'form-control','autofocus'));

Try this
<?php
echo $this->Form->create('Staff', array('action' => 'login'));
echo $this->Form->inputs(array(
'legend' => __(''),
'username' => array('autofocus'=>'autofocus'),
'password'
));
echo $this->Form->end('Login');
?>

Related

Yii2 form checkbox template

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']
]
);

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]);
}

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);

CakePHP 2.0 Determine which submit button has been clicked

In CakePHP 1.3 you can create a form with multiple submit buttons:
echo $this->Form->submit('Submit 1', array('name'=>'submit');
echo $this->Form->submit('Submit 2', array('name'=>'submit');
and detect which submit button was pressed in the controller with:
if (isset($this->params['form']['submit']) && $this->params['form']['submit'] == "Submit 1") {
// first button clicked
}
In CakePHP, $this->params['form'] isn't set and the clicked button value doesn't appear anywhere in $this->request, $this->request->data, $this->params, $this->data or $_POST.
How do I determine which button has been clicked in CakePHP 2.0?
Thanks in advance.
Edit:
As requested, here's the code for the form:
<?php echo $this->Form->create('History', array('action'=>'add')); ?>
<div class='submit'>
<?php
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit'));
?>
</div>
<?php echo $this->Form->end()?>
And the output of the form:
<form action="/projects/kings_recruit/trunk/www/histories/add" id="HistoryAddForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input name="_method" value="POST" type="hidden">
</div>
<div class="submit">
<input name="submit" value="Yes" type="submit">
<input name="submit" value="No" type="submit">
</div>
</form>
Generally it is a bad practise to use the same name for both submit buttons.
There should be a "submit" key in the $_POST and $this->request->data
I tested this in CakePHP 2.1.1 as shown below:
The view code:
<?php echo $this->Form->create('Message', array('action'=>'test'));
// Extra test input field
echo $this->Form->input('test');
?>
<div class='submit'>
<?php
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit'));
?>
</div>
<?php echo $this->Form->end()?>
The in the controller in $this->request->data:
array(
'submit' => 'Yes',
'Message' => array(
'test' => 'TestFieldTest'
)
)
And in $_POST:
array(
'_method' => 'POST',
'data' => array(
'Message' => array(
'test' => 'TestFieldTest'
)
),
'submit' => 'Yes'
)
You can also give the two submits different names:
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submitY'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submitN'));
This way you can differ them in the $_POST or $this->request->data, because the keys will be the submits' names:
array(
'submitY' => 'Yes',
'Message' => array(
'test' => 'foo'
)
)
array(
'_method' => 'POST',
'data' => array(
'Message' => array(
'test' => 'Bar'
)
),
'submitY' => 'Yes'
)
Then to determine which button is pressed you can use a simple isset($_POST['']) or over $this->request->data ?
Don't use the same name for both submit buttons. Consider this example:
<?php echo $this->Form->create(false); ?>
<?php echo $this->Form->text('input'); ?>
<?php echo $this->Form->submit('Yes', array('name' => 'submit1')); ?>
<?php echo $this->Form->submit('No', array('name' => 'submit2')); ?>
<?php echo $this->Form->end(); ?>
debug($this->request->data) will produce the following when the "Yes" button is clicked:
array(
'submit1' => 'Yes',
'input' => 'test'
)
And here it is when the "No" button is clicked:
array(
'submit2' => 'No',
'input' => 'test'
)
To check which button was clicked:
if (isset($this->request->data['submit1'])) {
// yes button was clicked
} else if (isset($this->request->data['submit2'])) {
// no button was clicked
}
in 2.0 there is no $this->params['form'] anymore
all form helper posted fields end up in $this->data (which makes more sense anyway)
so
if (!empty($this->data['submit']) && $this->data['submit'] == "Submit 1") {}
note that !empty() is better here as well.
PS: you can use my enhanced upgrade shell to replace it in your code: https://github.com/dereuromark/upgrade
its the command
cake Upgrade.Upgrade request
(https://github.com/dereuromark/upgrade/blob/master/Console/Command/UpgradeShell.php#L833)
if (!empty($this->request->data['submit']) && $this->request->data['submit'] == "Yes") {
// do your stuff
}

How can I change the template of a form in sfDoctrineGuardPlugin?

How can I change the template of a form in sfDoctrineGuardPlugin?
That is, I need to change the HTML (class, id) of the input elements (username, password) of a login form provided by sfDoctrineGuardPlugin.
I've changed apps/app_name/modules/sfGuardAuth/templates/singinSuccess.php, but it then just echoes $form (I need to change contents of that part - $form):
<form action="<?php echo url_for('#sf_guard_signin') ?>" method="post">
<table>
<?php echo $form ?>
</table>
<input type="submit" class="go_button" value="ir" />
<?php echo __('Forgot your password?') ?>
</form>
(It really should be something like changing a _form.php => I can't find this, though :S)
Thank you all for any answers provided =)
In the sign in form class (forget it's name), there will be something like:
'username' => new sfWidgetFormInput(array(), array())
Modify that to:
'username' => new sfWidgetFormInput(array(), array('id' => 'jibbly', 'class' => 'wibbly'))