I can't create a Pjax form inside an active form. What I've found is that when I put the Pjax inside an active form it doesn't work (it submits the entire page) but when I put it outside the active form its works fine:
<?php
$form = ActiveForm::begin([
'id' => 'CouplePartnerForm',
'action' => ['couple-partner/savecouplepartner'],
'enableAjaxValidation' => true,
'enableClientValidation' => true,
'validateOnSubmit' => true,
]);
?>
<div class="row">
<?php Pjax::begin([]); ?>
<?= Html::beginForm(['couple-partner/form-submission'], 'post', ['data-pjax' => '0', '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><?= isset($stringHash) ?$stringHash :"" ?></h3>
<?php Pjax::end(); ?>
</div>
<?php ActiveForm::end(); ?>
So what is the problem here?
Related
I am currently in the process of updating a (rather big) application from CakePHP 3 to 4.
I have this template:
<?= $this->Form->create(
$dmpLayer,
[
'url' => [
'controller' => 'DmpLayers',
'action' => 'edit',
],
]
); ?>
<div class="row">
<div class="col-4">
<?= $this->element('DataLayers/layer-table'); ?>
</div>
<div id="form-div" class="col-6">
<div class="layer-form">
<?= $this->element('DataLayers/form') ?>
</div>
</div>
<div class="col-2">
<div class="layer-form">
<h2>Form Actions</h2>
<?= $this->Form->submit('Create/Update Layer', ['class' => 'btn btn-success']); ?>
</div>
</div>
</div>
<?= $this->Form->end(); ?>
<?= $this->Html->script('data-layers'); ?>
which includes the DataLayers/form element:
<div class="row">
<div class="col-12">
<h4>Artist Layer</h4>
<?php
echo $this->Html->tag('fieldset', $this->element(
'actions/add',
[
'url' => [
'prefix' => 'Admin',
'plugin' => false,
'controller' => 'SegmentCores',
'action' => 'add',
],
]
)
. $this->Form->control('artist_layer.segment_cores[]', [
'multiple',
'options' => $segmentCores,
'label' => 'Segment Core',
'value' => $selectedValues['segment_cores'],
])
. $this->Form->control('artist_layer.segment_potentials[]', [
'multiple',
'options' => $segmentPotentials,
'label' => 'Segment Potential',
'value' => $selectedValues['segment_potentials'],
])
. $this->Form->control('artist_layer.layer_tags[]', [
'multiple',
'options' => $layerTags,
'label' => 'Artist Tag',
'value' => $selectedValues['artist_tags'],
])
. $this->Form->control('artist_layer.genres[]', [
'empty' => 'No genre set',
'options' => $genres,
'label' => 'Genre',
'value' => $selectedValues['genres'],
]);
?>
</div>
</div>
<?php
$this->Form->unlockField('artist_layer.genres');
$this->Form->unlockField('artist_layer.segment_cores');
$this->Form->unlockField('artist_layer.segment_potentials');
$this->Form->unlockField('artist_layer.layer_tags');
?>
In the initialize function of the AppController I have this:
$this->loadComponent('Security');
When I visit the page, it doesn't render and I immediately get this error:
FormProtector instance has not been created. Ensure you have loaded the FormProtectionComponent in your controller and called FormHelper::create() before calling FormHelper::unlockField()
This is the only form in my application for which this error happens. Every other form is working fine, and I am calling the Form->unlockField() function in many of them.
I am obviously calling Form->create() in my code, so is this because I am including an element to add fields to the form that is defined in the "main" template? Or is there some other explanation?
I have already attempted adding
$this->loadComponent('FormProtection');
to my AppController, but this causes a whole lot more problems in many other places in the app, and it doesn't solve the problem anyway (the page renders, but I get an error when submitting the form to save the data).
There are some questions here, but the problem is in the filters that are in the gridview.
My problem is that I can not integrate an external form with the gridview itself because I do not want to use the search form that is part of the gridview.
Controller
public function actionIndex()
{
$searchModel = new BlogSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Views
index.php
<?= Html::button('Filter', ['data-toggle' => 'modal', 'data-target' => '#filter-modal', 'class' => 'btn btn-primary']) ?>
<?php
Modal::begin([
'header' => '<h3>Search Blog</h3>',
'id' => 'filter-modal'
]);
echo $this->render('_search', ['model' => $searchModel]);
Modal::end();
?>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'title',
'content'
]
]); ?>
<?php Pjax::end(); ?>
_search.php
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'content') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
In the "index.php" the gridview is inside the pjax, but the search form is outside, but even if the form was within "Pjax :: begin" it would not work, either.
That is, when I do the search the page is reloaded. I want only gridview updatated.
As far as I understood from the discussion you don't want to use the filter fields inside the GridView and want to use the search form instead to filter the GridView. if that is correct you need to do 2 things for that
1. Move your form inside the pjax block
index.php
<?= Html::button('Filter', ['data-toggle' => 'modal', 'data-target' => '#filter-modal', 'class' => 'btn btn-primary']) ?>
<?php Pjax::begin(['enablePushState'=>false]); ?>
<?php
Modal::begin([
'header' => '<h3>Search Blog</h3>',
'id' => 'filter-modal'
]);
echo $this->render('_search', ['model' => $searchModel]);
Modal::end();
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'title',
'content'
]
]); ?>
<?php Pjax::end(); ?>
2. And the second an most important thing is to include the option of data-pjax inside the form options.
_search.php
<?php $form = ActiveForm::begin([
'action' => ['index'],
'id'=>'my-form',
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'content') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
EDIT
You might find problem with the modal overlay staying there and the modal window itself hides, provide your form with an id like id=>"my-form" and add the following inside your _search.php file to bind the beforeSubmit event for ActiveFormJS
$this->registerJs('$("#my-form").on("beforeSubmit", function (e) {
$("#filter-modal").modal("hide");
});', \yii\web\View::POS_READY);
First i want to say that i know there is many post about jQuery and Yii2 around the network but still can't make it clear for me! My question is pretty simple for you i think. When a user is with User role permission he shouldn't be able to update or delete others posts. And so if the user is this kind of guy i want to make both anchors ('Edit' and 'Delete') with opacity 0.5.
This is my view part:
<?php $form = \yii\bootstrap\ActiveForm::begin([
'method' => 'post'
]);
?>
<div class="col-md-8 text-left">
<?= Html::a('Edit', ['update', 'id' => $model->post_id], ['class' => 'btn btn-primary']); ?>
<?=
Html::a('Delete', ['delete', 'id' => $model->post_id],
['data-method' => 'POST', 'class' => 'btn btn-danger']);
?>
</div>
<div class="col-md-4 text-right">
<?= Html::a('Back', 'index', ['class' => 'btn btn-warning']); ?>
</div>
<?php \yii\bootstrap\ActiveForm::end(); ?>
I have added my file to AppAsset.php(test.js):
public $js = [
'js/slide-show.js',
'js/test.js'
];
I know how to make my fucntion but do not know how to implement it. Think i should do it with if statement (if(user->isGuest){ execute function and make anchros with opacity 0.5 }). Can you guys teach me the right way? Will be thankful for every advice! Thank you in advance!
You could assign a disable option so the button is displayed but disabled
if (Yii::$app->user->isGuest) {
echo Html::a('Edit', ['update', 'id' => $model->post_id], ['class' => 'btn btn-primary',
'disabled' => 'disabled']);
echo Html::a('Delete', ['delete', 'id' => $model->post_id],
['data-method' => 'POST', 'class' => 'btn btn-danger',
'disabled' => 'disabled']);
}
I am using yii/bootrap/active form and I set the layout to Horizontal
How can I reduce a gap between two columns ? I mean, where is the CSS file in yii/bootrap/activeform ?
My view code is like this
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Modal;
use yii\helpers\BaseHtml;
use app\assets\myassets\InputAsset;
?>
<?php
$listData = ['1' => 'Ya', '0' => 'Tidak'];
$button = Html::button('Cancel', ['class' => 'btn btn-default btn-md', 'data' => ['dismiss' => "modal"]]);
$button .= Html::button('Submit', ['id' => 'submit', 'class' => 'btn btn-success success btn-md']);
Modal::begin([
'header' => '<h2>Konfirmasi</h2>',
'toggleButton' => false,
'id' => 'confirm-submit',
'footer' => $button
]);
echo 'Data ini akan disimpan ?';
Modal::end();
?>
<div class="jenis-form">
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'id' => 'marketing',
'action' => ['index'],
'method' => 'get',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-2',
'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-4',
],
],
]);
?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'jn_byr')->textInput(['readonly' => !$model->isNewRecord,'autofocus'=>$model->isNewRecord,]) ?>
<?= $form->field($model, 'nama')->textInput(['autofocus'=>!$model->isNewRecord,]) ?>
<?= $form->field($model, 'ket_dnd')->textInput() ?>
<?= $form->field($model, 'ket_bat')->textInput() ?>
<?= $form->field($model, 'ket_tts')->textInput() ?>
<?= $form->field($model, 'k_inv')->textInput() ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'ket_battg')->textInput() ?>
<?= $form->field($model, 'ketkw')->textInput() ?>
<?= $form->field($model, 'ketrk')->textInput() ?>
<?= $form->field($model, 'tagih[]')->dropDownList($listData,['prompt'=>'<<< Pilih >>>']); ?>
<?= $form->field($model, 'deposit')->dropDownList($listData,['prompt'=>'<<< Pilih >>>']); ?>
</div>
</div>
<div>
<?= 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>
Im using bootstrap and Im trying to set a background image for the login screen.I try with div with id that catch all the things and set the background but didnt work .
Here my Login.php
<div id="bimg">
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to login:</p>
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n <div class=\"center\"><div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div></div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]);
?>
<?= $form->field($model, 'email')->textInput(['autofocus' => true, 'placeholder' => 'Write your email address']) ?>
<?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Write your password']) ?>
<?=
$form->field($model, 'rememberMe')->checkbox([
'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
])
?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<div>
<?= Html::a(Yii::t("app", "Forgotten password") . "?", ["/site/forgot"]) ?>
<br>
</div>
<br>
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
and the class with path in bootstrap.css
#bimg{
width:100%;
background-image: url(../web/assets/css/images/5.jpg) !important;
}
In layouts-main.php
controller->action->id == 'login') ? 'background-wrapper' : NULL ?>">
Check on which page u are , and if u are the page u want set the "class" u want