I am opening a ActiveForm in modal window in yii2.
This error comes when I am opening a ActiveForm in Modal window . my index.php is
<?php
use yii\helpers\Html;
use yii\bootstrap\Modal;
$this->title = 'Roles';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="role-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Role', '#', [
'class' => 'btn btn-success',
'id' => 'create-role-model',
'data-toggle' => 'modal',
'data-target' => '#activity-create-modal',
]) ?>
</p>
</div>
<?php
Modal::begin([
'id' => 'activity-create-modal',
'header' => '<h2>Hello world</h2>',
'footer' => Html::button('Close', ['class' => 'btn btn-default', 'data-dismiss' => 'modal'])
. PHP_EOL . Html::button('Add', ['class' => 'btn btn-primary btn-modal-save']),
]);
$model= new \frontend\models\Role();
echo $this->renderAjax('_form',['model' => $model]);
Modal::end();
?>
and my _form.php is
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* #var $this yii\web\View */
/* #var $model frontend\models\Role */
/* #var $form yii\widgets\ActiveForm */
?>
<?php $form = ActiveForm::begin([
'id' => 'create-ro',
'enableClientValidation' => true,
]); ?>
<?= $form->field($model, 'id')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'created_on')->textInput() ?>
<?= $form->field($model, 'updated_on')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
However when I place ActiveForm without render in index.php Yii Validation is working.Am I doing it an wrong manner . What have to be done so that modal validation start
It's echo $this->render('_form',['model' => $model]); you need, not $this->renderAjax().
The latter actually terminates the entire page and returns it without a layout around it (so including any javascript includes and so on).
Since you are still in another view that is definitely not what you need. So just use render(). It's a bit confusing because Controller::render() actually does close the page (and also adds javascripts), but in case of the View (Which $this is in that context) it does what renderPartial does for the controller.
Related
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
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$form = ActiveForm::begin(['id' => 'login-form']);
ActiveForm::end();
?>
If i run this code the following error will occur.
Fatal error: Class 'yii\bootstrap\ActiveForm' not found in
E:\wamp\www\yii2-paypal-master\backend\views\site\login.php on line 6
Kindly help me to fix this.
Thanks
Replace
use yii\bootstrap\ActiveForm;
with
use yii\widgets\ActiveForm;
Form:
<?php $form = ActiveForm::begin([
'options'=>['enctype'=>'multipart/form-data'],
]);
?>
<?= $form->errorSummary($model) ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<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(); ?>
First, check the file assets/AppAsset.php if your Class App Asset is like mine, using Boostrap 3 or 4 or o:
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap4\BootstrapAsset',
];
}
You need to change it to use yii\bootstrap\ActiveForm; change this to instead use yii\bootstrap4\ActiveForm; it worked for me.
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
}
My application have to have multiple language, so I decided to separate each language by using tab (Yii2 gui), but how can I render the form in side the 'content' key?
<?php
$language_tab=[];
$increment=0;
$content="I love you";
foreach($language as $obj){
$language_tab[$increment] = array('label' => $obj->name ,'content' => $content);
$increment++;
}
echo Tabs::widget([
'items' => $language_tab,
'options' => ['tag' => 'div'],
'itemOptions' => ['tag' => 'div'],
'headerOptions' => ['class' => 'my-class'],
'clientOptions' => ['collapsible' => false],
]);
?>
<div class="status-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'date_created')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I just wanna change from $content to the form below.
Please help!!!
You may create separate view for the form and render it:
...
'content' => $this->render('_language_form', ['language' => $obj, 'model' => $model]),
...
I'm creating a form view and I want to organize the form fields with tabs structure, using the official Tabs widget.
Is it possible init the Tabs widget with the id (or class) of the div elements that contains the active form fields?
One example of how you can manage it is doing like this:
First, divide your contact-form into one view-file for each tab.
Place the ActiveForm::begin() and ActiveForm::end() around the Tabs::widget()
Render the contact-form pages into content, with parameters $model and $form
Example code:
views/site/contact.php
<?php
/* #var $this yii\web\View */
$this->title = 'Contact';
use yii\bootstrap\Tabs;
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= Tabs::widget([
'items' => [
[
'label' => 'One',
'content' => $this->render('contact_form1', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'Two',
'content' => $this->render('contact_form2', ['model' => $model, 'form' => $form]),
],
]]);
?>
<?php ActiveForm::end(); ?>
views/site/contact_form1.php
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
views/site/contact_form2.php
<?php
use yii\helpers\Html;
use yii\captcha\Captcha;
?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
Hope this helps!
Just add at the top of your contact.php global $form; and all works fine.
I have another solution:
When we call $form->field($model, 'name')->textInput(), it will return the model of class yii\widgets\ActiveField, so just continue calling a method of this class as $form->field($model, 'name')->textInput()->render(). It will return a string then you can use it for the tab's content.
I have an example code in my application for translating multi languages as the following code:
<?php
$items = [];
foreach ($translateModels as $translateModel) {
$tabContent = $form->field($translateModel, "[{$translateModel->code}]name")->textInput()->render();
$items[] = [
'label' => $translateModel->language->name,
'content' => $tabContent,
];
}
?>
<?= Tabs::widget([
'options' => [
'class' => 'nav-tabs',
'style' => 'margin-bottom: 15px',
],
'items' => $items,
]) ?>
Maybe it's help.