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.
Related
In controller file i print this,
in controller am getting NULL after printing.
how to the know if button is clicked ?
var_dump(\Yii::$app->request->post('submit'));
In view file
<?php $form = ActiveForm::begin([
'id' => 'button',
]); ?>
<?= Html::submitButton('submit', ['name' => 'submit', 'value' => 'submit_1', 'class' => 'btn']) ?>
<?php ActiveForm::end(); ?>
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,
]);
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,
]);
}
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
I want to create ActiveForm without model for just in case something. I did try with dynamicModel but i got some error :
use yii\base\DynamicModel;
$model = DynamicModel::validateData(compact('KOMENTAR'), [
[['KOMENTAR'], 'string', 'max' => 128],
]);
This is the form i want to create
<br>
<?php $form = ActiveForm::begin([
'method' => 'post',
]); ?>
<?= $form->field($model, 'KOMENTAR')->textarea(['rows' => 6])->label(false) ?>
<div class="form-group">
<?= Html::submitButton('POST', ['class' => 'btn btn-primary']) ?>
</div>
This is the error
Getting unknown property: yii\base\DynamicModel::KOMENTAR
Normally ActiveItems are used to work with a model, but Yii2 have a helper class called Html to use the same items like classic HTML.
Use beginForm() method from Html. And try something like that:
use yii\helpers\Html;
<?= Html::beginForm(['/controller/view', 'id' => $model->id], 'POST'); ?>
<?= Html::textarea('KOMENTAR', '', ['rows' => 6])->label(false); ?>
<div class="form-group">
<?= Html::submitButton('POST', ['class' => 'btn btn-primary']); ?>
</div>
<?= Html::endForm(); ?>
You can read more about this helper in the documentation.
Since you are using compact('KOMENTAR'), you should have a $KOMENTAR variable.
Read more about compact : http://php.net/manual/fr/function.compact.php
Or you should simply create your model like this :
$model = new \yii\base\DynamicModel(['KOMENTAR']);
$model->addRule(['KOMENTAR'], 'string', ['max' => 128]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// do what you want
}