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

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

Related

Yii2 - checkbox change commit ActiveForm

in the index.php there is a checkbox within a active form
<div class="form-group pull-right" style="margin-right: 10px">
<?php Pjax::begin(['id' => 'options']); ?>
<?php $form = ActiveForm::begin(['method' => 'get', 'action' => ['ensemble/index'], 'options' => ['data-pjax' => true]]); ?>
<?= $form->field($searchModel, 'completed')->checkbox(); ?>
<?= Html::submitButton('Apply', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
also there is kartik gridview in the index.php
<?= GridView::widget($gridConfig); ?>
with the following configuration
$gridConfig['pjax'] = true;
$gridConfig['pjaxSettings'] = ['options' => ['id' => 'pjaxGrid']];
the ensemble controller with the index action looks like this
public function actionIndex()
{
$searchModel = new EnsembleSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
This does work every thing just fine. I can check / uncheck the checkbox field and hit the submit button; which then reloads the gridview. Just cool.
Now i was wondering if it is possible, to just make this with only by clicking the checkbox? So if i check / uncheck the checkbox the gridview will be reloaded (pjax'd).
cheers,
Luc
Assign id to your form as well as class to your checkbox.
<div class="form-group pull-right" style="margin-right: 10px">
<?php Pjax::begin(['id' => 'options']); ?>
<?php $form = ActiveForm::begin([
'id' => 'filter-form',
'method' => 'get',
'action' => ['ensemble/index'],
'options' => ['data-pjax' => true]
]); ?>
<?= $form->field($searchModel, 'completed',['class'=>'status_chk'])->checkbox(); ?>
<?= Html::submitButton('Apply', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
Add this script to your index.php file.
<?php
$this->registerJs(
'$("document").ready(function(){
$(document).on("change",".status_chk", function() {
$("#filter-form").submit();
});
});'
);
?>
I would say that you could do that with JavaScript. You could do something like this:
You must know how to identify the Form and the Checkbox (you could assign them so ids..)
In a JavaScript you could do something like this:
var form = document.getElementById("form-id");
document.getElementById("your-checkbox-id").addEventListener("click", function () {
form.submit();
});
As explain in this answer:
submit form with javaScript

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.

How can I implement modal in Yii2

I have two forms named schedule_route,bus_stop in my project.I want to display bus_stop form as a popup window when clicking in a button placed in the schedule_route form.The bus_stop form is rendering using the action actionCreate on the Stops controller.How can I implement this using the Modal in yii2?
schedule_route form is:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\depdrop\DepDrop;
use yii\helpers\ArrayHelper;
use common\models\Stops;
use kartik\time\TimePicker;
use common\models\Schedules;
use yii\bootstrap\Modal;
use yii\helpers\Url;
/* #var $this yii\web\View */
/* #var $model app\models\ScheduleRoute */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="schedule-route-form">
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'schedule_id')->dropDownList(ArrayHelper::map(Schedules::find()->all(), 'id', 'route_name'), ['prompt' => 'Choose...']); ?>
<?php echo $form->field($model, 'stop_id')->dropDownList(ArrayHelper::map(Stops::find()->all(), 'id', 'stop'), ['prompt' => 'Choose...']); ?>
<?php
Modal::begin([
'header' => '<h2>Schedule Route</h2>',
'id'=>'modal',
'toggleButton' => ['label' => 'Add '],
]);
Modal::end();?>
<div class="modal remote fade" id="modalvote">
<div class="modal-dialog">
<div class="modal-content loader-lg"></div>
</div>
</div>
<?php echo $form->field($model, 'depart')->widget(TimePicker::classname(), []);?>
<?= $form->field($model, 'route_order')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
You can implement yii2 built in bootstrap Modal
use yii\bootstrap\Modal;
Modal::begin([
'header' => 'Header title',
'id' => 'mymodal',
//'options' => ['class' => 'modal'] //in case if you dont want animation, by default class is 'modal fade'
]);
// your form here
Modal::end();
Then you add data-toggle='modal' and data-target='#modalId' attributes to an element on clicking which you want the modal to open, for example:
<button data-toggle='modal' data-target='#mymodal'>Click me</button>
Or you can trigger with jQuery:
$('button').click(function(){$('#mymodal').modal('show');});
In the controller you have to load the view with a renderAjax method.
This method load the view without the layout.
I learned how to do it following this video wich is hihgly recommended
How to load a view within a modal

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