Click a button twice to POST - yii2

this my view file for a signin page
<?php $form = ActiveForm::begin(); ?>
<?= $form->field( $model, 'email' )->textInput( [ 'maxlength' => 200 ], [ 'id' => 'email' ] ) ?>
<?= $form->field( $model, 'password' )->passwordInput( [ 'maxlength' => 200 ] ) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-14">
<?= Html::submitButton( 'Login',
[ 'class' => 'btn btn-danger btn-block btn-lg', 'name' => 'login-button' ] ) ?>
</div>
</div>
<input type='sub' name='submit' value='Reset Password' class="btn btn-link btn-default" '>
<?php ActiveForm::end(); ?>
it works perfectly but you have to click on the login button twice. there is no other problem in signing in. i don't have a clue why i have to click the login button twice.

Firstly You've got syntax in your input type='sub' at the end You've got ' instead of /. You should render it by Html::input, then you'll avoid syntax.
Secondly change this input name. name='submit' brookes ActiveForm. Not sure why...

name='submit' is your problem here. Change it to name='not_submit' and all will be well.
The post here explains why name='submit' causes problems for ActiveForm.
Yii2 form submit button must be clicked two times for action. How to prevent this?

Related

Disable ActiveForm Clientside Validation on button other than submit Yii2

I have an upload excel to DB form.
It has 1 File input and 2 buttons 'Upload' and 'Delete All'.
Problem Scenario:
I don't want client-side activeform validation on 'Delete All' as File is not required here.
Code:
<?php $form = ActiveForm::begin([ 'enableClientValidation' => false,
'options' =>['action' => Url::toRoute('/site/halltickets')
,'method' => 'POST'
, 'enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'file')
->fileInput()->label("Upload Only Excel File [Allowed Formats: xlsx, xls]") ?>
<center>
<?= Html::submitButton('Upload »', ['class' => 'btn btn-warning']) ?>
<?= Html::a('Delete All »',
Url::toRoute(['site/delete-halltickets']),
['class' => 'btn btn-danger',
'data-confirm' => Yii::t('yii', 'Delete all Hall Ticket Allocations?'),
]);?>
</center>
<?php ActiveForm::end(); ?>
to understand better, below is the image.
It is bad idea to place "Delete All" button on the upload form. Insert this button outside the form. It will more user-friendly.
Additionally you can remove "required" validator from model. Or define it only for the specific scenarios.

Yii2 form submit button must be clicked two times for action. How to prevent this?

yii2 submit button needs to be clicked two times in form
I have a problem where I need to check more than one submit buttons in the controller. It works but I need to click submit buttons two times.
In controller :
switch(\Yii::$app->request->post('submit')) {
case 'submit_1' :
//my code
break;
case 'submit_2' :
//my code
In view
<?= Html::submitButton('NEXT', ['name' => 'submit', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
There is an issue with using jquery reserved words as your attribute id or attribute names.
Search for "Be careful when naming form elements such as submit buttons" at
https://github.com/yiisoft/yii2/blob/master/docs/guide/input-forms.md
Search "Additional Notes" at https://api.jquery.com/submit/
Changing your submit names will fix your click twice problem:
<?= Html::submitButton('NEXT', ['name' => 'submit_next', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit_prev', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
try to change name of button as array
<?= Html::submitButton('NEXT', ['name' => 'submit[]', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit[]', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
and in your controller :
$submittedType = \Yii::$app->request->post('submit');
switch($submittedType[0]) {
//your code
}
To avoid double click disable validate on submit:
$form = ActiveForm::begin([
'validateOnSubmit' => false,
]);

YII2 how to use pjax to submit a form without refresh the page

I use a yii2 to create a website, trying to use a form to submit some data.
Can some body give me a example. Tell me how to use pjax to submit a form without reload the page.
There's an example of Yii2 Pjax submit form in this link
views\site\form-submission.php:
<?php Pjax::begin(); ?>
<?= Html::beginForm(['site/form-submission'], 'post', ['data-pjax' => '', 'class' => 'form-inline']); ?>
<?= Html::input('text', 'string', Yii::$app->request->post('string'), ['class' => 'form-control']) ?>
<?= Html::submitButton('Hash String', ['class' => 'btn btn-lg btn-primary', 'name' => 'hash-button']) ?>
<?= Html::endForm() ?>
<h3><?= $stringHash ?></h3>
<?php Pjax::end(); ?>
controllers\SiteController.php:
public function actionFormSubmission()
{
$security = new Security();
$string = Yii::$app->request->post('string');
$stringHash = '';
if (!is_null($string)) {
$stringHash = $security->generatePasswordHash($string);
}
return $this->render('form-submission', [
'stringHash' => $stringHash,
]);
}

Setting autocomplete input with yii2?

How can I change the input width when I use an autocomplete widget in an active form. This is what I have:
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<div class="col-md-6">
<?php echo $form->field($model, 'name')
->label('Imię i nazwisko')
->widget(AutoComplete::classname(), [
'clientOptions' => [
'source' => [$data],
]
])
?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'email')->label('Adres email') ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'phone')->label('Telefon') ?>
</div>
The autocompleter widget works but I have a problem with the input box. As you can see from the screenshot the "Imię i nazwisko" autocomplete box is much smaller than the others. How can I select my prefer width of this input?
Thanks to #aslawin for helping with the answer!
<?= $form->field($model, 'name')
->label('Imię i nazwisko')
->widget(AutoComplete::classname(), [
'clientOptions' => [
'source' => [$data],
],
'options' => ['style'=>'width: 400px']
])
?>

How to open in a new window for a submit button?

Is it possible to open in a new window when i click a submit button ? I have TWO SUBMIT buttons in my active form.
I know if i give 'target'=>'_blank' i will get the desired result in normal cases but it didn't work in active form.
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<div class="col-md-6">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Submit') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<div class="col-md-6">
<?= Html::submitButton('Pdf', ['class' => 'btn btn-primary','name'=>'Pdf'],['target'=>'_blank']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
I tried as above. then i decided to give target => _blank to active form as shown below
<?php $form = ActiveForm::begin(
['options'=>['target'=>'_blank']]); ?>
It works but not in the desired way. Now if i click on any one of the buttons i will be redirected to new page. I only want to get redirected to new page when i click on button 'Pdf'.
Any Ideas?
I don't want any javascript to be used.
You can use anchor for new window.
<?= Html::a('Pdf', 'url', ['class' => 'btn btn-primary','name'=>'Pdf', 'target'=>'_blank']) ?>
form = ActiveForm::begin(
['options'=>['target'=>'_blank']]);
it needs to work