I'm using some plugins (yii\widgets\MaskedInput)in dynamic-form, I notice that plugin work only at first element, when adding an option but this normal load but the entry "score" does not validate the entry of only numbers but allows you to enter text as a normal entry. Please advice. Thanks before.
My code like this.
_Form
<?php
$form = ActiveForm::begin([
'id' => 'dynamic-form',
"enableAjaxValidation" => true,
]);
?>
<div class="row">
<div class="col-sm-6">
<?=$form->field($model, 'title')->textInput(['maxlength' => true])->input('title', ['placeholder' => "Title Evaluation"]) ?>
<div class="row">
<div class="col-sm-6">
<?=
$form->field($model, 'date_start')->widget(DateTimePicker::className(), [
'options' => ['placeholder' => '-- Select Date --'],
'pluginOptions' => [
'language' => 'fr',
'autoclose' => true,
'calendarWeeks' => true,
'daysOfWeekDisabled' => [0, 5],
'todayHighlight' => true,
'todayBtn' => true,
]
]);
?>
</div>
<div class="col-sm-6">
<?=
$form->field($model, 'date_end')->widget(DateTimePicker::className(), [
'options' => ['placeholder' => '-- Select Date --'],
'pluginOptions' => [
'language' => 'fr',
'autoclose' => true,
]
]);
?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?=
$form->field($model, "type_evaluation_id")->dropDownList(
ArrayHelper::map(EvalTipo::find()->all(), 'id', 'description'), [
'prompt' => '-- Select Type --'
]);
?>
</div>
<div class="col-sm-3">
<?=
$form->field($model, 'duration')->widget(TimePicker::className(), [
'pluginOptions' => [
'showSeconds' => true,
'showMeridian' => false,
'minuteStep' => 1,
'secondStep' => 5,
]
]);
?>
</div>
<div class="col-sm-3">
<?=$form->field($model, 'qualification')->textInput(['placeholder' => 'Qualification']) ?>
</div>
</div>
</div>
<div class="col-sm-6">
<?=$form->field($model, 'description')->textarea(['rows' => 9, 'placeholder' => 'Enter evaluation description']) ?>
</div>
</div>
<div class="content">
<div class="box box-success box-solid">
<div class="box-header with-border">
<h3 class="box-title">Questions Evaluation</h3>
</div>
<div class="panel-body">
<?php
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 30, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelQuestion[0],
'formId' => 'dynamic-form',
'formFields' => [
'item',
'puntuacion',
],
]);
?>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Question</th>
<th style="width: 500px;">Options</th>
<th class="text-center" style="width: 90px;">
<button type="button" class="add-item btn btn-success btn-xs"><span class="glyphicon glyphicon-plus"></span></button>
</th>
</tr>
</thead>
<tbody class="container-items">
<?php foreach( $modelQuestion as $indexQuestion => $modelQuestions ): ?>
<tr class="item">
<td class="vcenter">
<table class="table table-bordered table-striped">
<td class="vcenter">1.</td>
<td class="vcenter">
<?php
// necessary for update action.
if( !$modelQuestions->isNewRecord ){
echo Html::activeHiddenInput($modelQuestions, "[{$indexQuestion}]id");
}
?>
<?=
$form->field($modelQuestion, "[{$indexQuestion}]question")->dropDownList(
ArrayHelper::map(QuestType::find()->all(), 'id', 'description'), [
'prompt' => '-- Select Question --'
]);
?>
<?=$form->field($modelQuestions, "[{$indexQuestion}]question")->label(false)->textInput(['maxlength' => true, 'placeholder' => 'Title question']) ?> <?=
$form->field($modelQuestions, "[{$indexQuestion}]score")->label(false)->widget(MaskedInput::className(), [
'clientOptions' => [
'alias' => 'decimal',
'groupSeparator' => '.',
'digits' => 0,
'autoGroup' => true,
'removeMaskOnSubmit' => true,
'rightAlign' => false,
],
'options' => [
'class' => 'form-control',
'maxlength' => true,
]
])
?><?=$form->field($modelQuestions, "[{$indexQuestion}]justified ")->checkbox(array('label' => '', 'labelOptions' => array('style' => 'padding:5px;'),))->label('You want a response justified ?'); ?>
</td>
</table>
</td>
<td>
<?=
$this->render('_form-opc', [
'form' => $form,
'modelQuestion' => $indexQuestion,
'modelsOpc' => $modelsOpc[$indexQuestion],
])
?>
</td>
<td class="text-center vcenter" style="width: 90px; verti">
<button type="button" class="remove-item btn btn-danger btn-xs"><span class="glyphicon glyphicon-minus"></span></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="form-group">
<?=Html::submitButton($model->isNewRecord ? '<span class="fa fa-plus"></span> Crear' : '<span class="fa fa-edit"></span> Modificar', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Here you can see the problem.
problem Maskinput validation
You need to bind the MaskedInput plugin to the newly created input using javascript. You can use the afterInsert event of the wbraganca/yii2-dynamicform wrapping the call to the .inputmask() jquery plugin that the extension is based on.
on the top of your view add the following and it will start working for every newly added row
$js =<<< JS
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
$(item).find("input[name*='[price]']").inputmask({
"alias":"decimal",
"groupSeparator":".",
"digits":0,
"autoGroup":true,
"removeMaskOnSubmit":true,
"rightAlign":false
});
});
JS;
$this->registerJs($js, \yii\web\View::POS_LOAD);
Related
Is it possible to convert GridView from table format to div in the index so I can customize the look of my index; my View is
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'Useravatar',
'format' => 'html',
'label' => 'Avatar',
'value' => function ($data) {
return Html::img('http://localhost:8585/yii45/wfp/web/' . $data['Useravatar'],
['width' => '80px', 'height' => '80px']);
},
],
'User_id',
'Usermode',
'Username',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
if It's possible to make it something like, not exactly but somthing that use div and column
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
]); ?>
<div>
<div class="row">
<div class="col-md-3 text-center"> Username </div>
<div class="col-md-6"> Usermode </div>
<div class="col-md-3 text-right"> Useravatar </div>
</div>
<div class="row">
<div class="col-md-12"> Info </div>
</div>
<div class="row">
<div class="col-md-6"> edit </div>
<div class="col-md-6"> delete </div>
</div>
</div>
<?php Pjax::end(); ?>
I found this code about ListView :
https://www.yiiframework.com/doc/guide/2.0/en/output-data-widgets#list-view
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;
$dataProvider = new ActiveDataProvider([
'query' => Post::find(),
'pagination' => [
'pageSize' => 20,
],
]);
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_post',
]);
But I don't know where I have to add this code
For someone who Have the same problem like me the solution is :
in Controller Create a function Example : actionUserlist
public function actionUserlist()
{
$Model_User = new User;
return $this->render('widgetUserList', [
'Model_User' => $Model_User,
]);
}
2 - Create a php file in view " widgetUserList.php "
<?php
use yii\widgets\ListView;
use yii\data\ActiveDataProvider;
$dataProvider = new ActiveDataProvider([
'query' => $Model_User::find(),
'pagination' => [
'pageSize' => 15, //the number of items in a page : 15
],
]);
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_userList',
],
]);
?>
3- Create a view file " _userList.php "
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="userlist">
<h2><?= Html::encode($model->Username) ?></h2>
<?= $model->Useremail ?>
</div>
What I want to do is to change from bootstrap vertical to horizontal form and this is what I've tried:
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'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, 'firstname') ?>
<?= $form->field($model, 'lastname') ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'bla') ?>
</div>
</div>
<?php ActiveForm::end() ?>
but it gave me an error Setting unknown property: yii\widgets\ActiveForm::layout
Please help!!!
For layout option to work ActiveForm must be instance of yii\bootstrap\ActiveForm instead of yii\widgets\ActiveForm.
Change your use statement.
In boostrap you can use
<form class="form-inline"> // for inline fields form
or
<form class="form-horizontal"> // for horizonatal fields form
eg:
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'action' => ['index'],
'method' => 'get',
'class' => 'form-horizontal',
]); ?>
I have a form field "dob[]" with array input like
<?php $form = ActiveForm::begin();
for($i=0;$i<= 3;$i++):
echo $form->field($model, 'dob[]')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Date Of Birth'],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
]
]);
endfor; ?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Datepicker working only on first "dob" field but rest of the field having only button format of datepicker but calendar not working.
This is because the javascript cannot determine the correct input fields, after the first one. Take a look in your source code. All widgets have properly the same id and/or name. You have to setup a unique ID for each of the generated widgets.
By the way, it is always a good approach to name form data accordingly.
That is documented at the demo page.
The following should work:
<?php
$form = ActiveForm::begin();
for ($i=0; $i < 3; $i++) {
echo $form->field($model, 'date_end')->widget(DatePicker::classname(), [
'options' => [
'placeholder' => 'Date Of Birth',
'name' => 'DOB' .$i,
'id' => 'DOB-ID' . $i,
],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
],
]);
}
?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Compare validation is action strange - no matter what, emails are never the same, and error is shown all the time.
This is the code for validation:
public function rules()
{
return [
[['first_name', 'last_name', 'email', 'conf_email'], 'required'],
['title', 'match', 'pattern' => '/^[a-zA-Z]{0,100}$/',
'message' => 'Title must contain only letters.'],
[['first_name', 'last_name'], 'match', 'pattern' => '/^[a-zA-Z]{0,45}$/',
'message' => 'The {attribute} must contain only letters.'],
[['email', 'first_name', 'last_name'], 'trim'],
[['email', 'conf_email'], 'email'],
['email', 'string', 'max' => 255],
['email', 'UniqueValidator'],
['conf_email', 'compare', 'compareAttribute'=>'email', 'skipOnEmpty' => false,
'message' => 'Emails do not match.'],
];
}
And code for the form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
$form = ActiveForm::begin([
'action' => Url::toRoute([
'team/ajax-add-team-member',
'id' => $team->idteam,
'portalID' => $portal->idportal
]),
'enableAjaxValidation' => true,
'validationUrl' => '/team/team-validation',
'id' => 'team-invite-form',
'options' => [
'class' => 'clearfix'
]
]) ?>
<div class="col-md-12">
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-12">
<?php echo $form->field($model, 'title')->textInput() ?>
</div>
<div class="col-md-5 col-sm-5 col-xs-12">
<?php echo $form->field($model, 'first_name')->textInput() ?>
</div>
<div class="col-md-5 col-sm-5 col-xs-12">
<?php echo $form->field($model, 'last_name')->textInput() ?>
</div>
</div>
</div>
<div class="col-md-12">
<?php
echo $form->field($model, 'email')->textInput();
echo $form->field($model, 'conf_email')->textInput();
echo $form->field($model, 'is_medical_professional')->checkbox();
?>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<?php
echo Html::submitButton('Send', ['id' => 'add-team-member-form-btn', 'class' => 'btn btn-default']);
ActiveForm::end();
?>
What is going on here o.O?
Thiis line looks strange:
['email', 'UniqueValidator'],
Try using ['email', 'unique'], instead
If this will not help - provide more details. What exact error do you faced?
getting an error cant upload the extension kartik select2....
need some ideaaa
using yii want to install the select 2 extension in the branch table...
<div class="branches-form">
<?php $form = ActiveForm::begin(); ?>
<?=$form->field($model,'companies_company_id')->widget(Select2::classname(), [
'data' =>ArrayHelper::map(Companies::find()->all(),'company_id','company_name'),['prompt'=>'Select Company'] ,
'language' => 'en',
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions'=> [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'branch_name')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'branch_address')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'branch_status')->dropDownList([ 'active' => 'Active', 'inactive' => 'Inactive', '' => '', ], ['prompt' => 'Status']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Properly read this documentation http://demos.krajee.com/widget-details/select2
Try this solution:
Also include use kartik\select2\Select2; at the top of your file.
<?= Select2::widget([
'attribute' => 'companies_company_id',
'model' => $model,
'data' =>ArrayHelper::map(Companies::find()->all(),'company_id','company_name'),['prompt'=>'Select Company'] ,
'language' => 'en',
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions'=> [
'allowClear' => true
],
]) ?>