put submit button in Tabs::widget in yii2 - forms

I have a Tabs::widget that all settings are located in different tabs in a ActiveForm and admin can set config in each tab and once submit.(multiple forms in one widget )
in setting view :
<?php $form = ActiveForm::begin(); ?>
<?php
echo \yii\jui\Tabs::widget([
'headerOptions' => ['class' => 'tabs'],
'itemOptions' => ['tag' => 'div'],
'items' => [
[
'label' => 'serverSetting',
'content' => $this->render('serverSetting', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'emailSetting',
'content' => $this->render('emailSetting', ['model' => $model, 'form' => $form]),
],
[
'label' => 'smsSetting',
'content' => $this->render('smsSetting', ['model' => $model, 'form' => $form]),
],
],
]);
?>
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary', 'name' => ''])) ?>
</div>
<?php ActiveForm::end(); ?>
in view of one of the tabs (smsServer view):
<?php
use yii\helpers\Html;
use app\components\ActiveForm;
?>
<div class="user-form">
<?= $form->field($model, 'login')->textInput(['placeholder' => 'host']) ?>
<?= $form->field($model, 'password1')->textInput(['placeholder' => 'username']) ?>
<?= $form->field($model, 'wsdl')->textInput(['placeholder' => 'password']) ?>
<?= $form->field($model, 'from1')->textInput(['placeholder' => 'port']) ?>
</div>
in controller :
public function actionSetting()
{
$model = new Setting();
$model->setAttributes(Yii::$app->params, false);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
file_put_contents(Yii::getAlias('#app/config') . DIRECTORY_SEPARATOR . 'extra_params.php', base64_encode(serialize($model->attributes)));
}
return $this->render('setting', [
'model' => $model,
]);
}
My form and submit button do not work. where is my problem?

Seems you don't have an ActiveForm::end()
.......
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary'])) ?>
</div>
<?php ActiveForm::end(); ?>
and for debugging don't suppress the name of button

So I found my answer. My validation was false.
in rule():
[['login', 'wsdl', 'password','from1'], 'required',],
But I post form without filled all fields.
I changed to
[['login',], 'required',],
[['login', 'wsdl', 'password',from1'], 'safe',],

Related

In Cakephp 3.6 How do I get the size and type of an image to send it by form?

How do I get the size and type of an image to send it by form?
Well that, I want to save in the table of images the size and type of an image that is uploaded through a form. With Ajax, I can recover those data, but to pass them to PHP I can only do when sending the form, also directly with PHP:
if ($this->request->is('post')) {
$isData = $this->request->getdata();
$imagene->imagen = $isData['image_path'];
$imagene->tipo = $isData['type']
$imagene->tamano = $isData['size'];
...
But I want to do it before sending the form, which is when the insertion is done in the database.
Form:
<?= $this->Form->create($imagene, ['novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<?= $this->Form->hidden('tipo', ['value' => $tipo, 'class' => 'tipo-addimage']); ?>
<?= $this->Form->hidden('$tipo', ['value' => $size, 'class' => 'tamano-addimage']); ?>
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
<?= $this->Form->end(); ?>
Now I see that if I do a debug of $isData, the field: "imagen" does not appear:
'tabla' => 'users',
'id_tabla' => '22',
'tipo' => '',
'tamano' => ''
UPDATING
I've changed things in the form and the controller:
form:
<?= $this->Form->create($imagene, ['enctype' => 'multipart/form-data', 'novalidate', 'id' => 'addimageform', 'class' => 'form addimageform']); ?>
<?= $this->Form->control('imagen', ['type' => 'file', 'class' => 'imagen-addimage']); ?>
<div class="centrar-submit">
<?= $this->Form->button('Subir imagen', ['id' => 'submit', 'class' => 'submit-addimage']); ?>
<?= $this->Form->button('Omitir', ['id' => 'omitir', 'class' => 'omitir-addimage', 'redirect' => ['controller' => 'administracion', 'action' => 'index']]); ?>
</div>
<?= $this->Form->end(); ?>
Controller:
public function add($table, $idTable) {
$imagene = $this->Imagenes->newEntity();
if ($this->request->is('post')) {
$isData = $this->request->getdata();
debug($this->request->getData('imagen')); // <---- Is null
debug($isData); // <---- Is empty
...
Why? I don't know.
You should mentioned what type of form you used. That is file type you should use
echo $this->Form->create($article, ['type' => 'file']);
Hope this help you.

Yii2 DatePicker within Modal

when I use the kartik\date\DatePicker within Modal,but it's report error that like https://******.com/assets/d9620747/css/bootstrap-datepicker3.css.map Failed to load resource: the server responded with a status of 404 (Not Found).plz help me;
My Controller's code:
public function actionBook($id = null)
{
$ticket = $this->findModel($id);
$model = new TicketOrder();
if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
// userCreate scenario
$model->scenario = 'create';
} else {
return $this->renderAjax('book', [
'ticket' => $ticket,
'model' => $model,
]);
}
}
My View's code:
<?php
Modal::begin([
'id' => 'create-modal',
'header' => '<h4 class="modal-title">订票</h4>',
'size' => 'modal-lg',
// 'footer' => 'Close',
]);
Modal::end();
?>
<?php
$js = <<<JS
$(function(){
$(".modal-wraper").click(function(){
var id = $(this).attr('data-id');
var url = "/sights/product-ticket/book?id=" + id;
$.get(url,{},function(data){
$(".modal-body").html(data);
});
$('#create-modal').on('shown.bs.modal', function(){
$('[data-toggle="popover"]').popover();
});
$("#create-modal").modal('show');
});
});
JS;
$this->registerJs($js);
My ajaxRender view's code:
<?php
use kartik\form\ActiveForm;
use kartik\date\DatePicker;
?>
<h3>由于资源方过多,建议一个手机号只购买一张票。</h3>
<?php
$form = ActiveForm::begin([
'type' => ActiveForm::TYPE_HORIZONTAL,
]);
?>
<?= $form->field($model, 'fullname')->textInput() ?>
<?= $form->field($model, 'mobile')->textInput() ?>
<?= $form->field($model, 'plantime')->widget(DatePicker::className(), [
'type' => DatePicker::TYPE_COMPONENT_APPEND,
]) ?>
<?= $form->field($model, 'comment')->textarea() ?>
<div class="button-group">
<?= \yii\helpers\Html::a('关闭', ['#'], ['class' => "btn btn-primary", 'data-dismiss' => "modal"]) ?>
<?= \yii\helpers\Html::button('购买', ['class' => 'btn btn-success pull-right', 'type' => 'submit']) ?>
</div>
<?php
$form->end();
?>
Like this:
image
I got error when I click the DatePicker Button.
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (bootstrap-datepicker3.css.map, line 0)
This issue has been already resolved. See it on GitHub.
Upgrade datepicker to version 1.4.2.

yii2 loading pjax form dynamically

Problem: I want to update GridView in pjax style but it redirect to the form creation page.
What the code below does:
Having an index page with GridView to display data list and open a form in a modal window to create new record. The form code is added into the modal dynamically.
When click on "Create Country" button on index page, it calls country/create to get the HTML code of the form and insert it into the modal, then it shows the modal window.
When click on the "Create" button on the form, it submit the form to country/create. This will return the HTML code of the index page, and I want it to update the GridView part, but it does not.
The code:
Controller CountryController.php
class CountryController extends Controller
{
public function actionIndex()
{
return $this->renderIndex();
}
public function actionCreate()
{
$model = new Country();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->renderIndex();
} else {
return $this->renderAjax('_form', [
'model' => $model,
]);
}
}
private function renderIndex()
{
$searchModel = new CountrySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
View index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\web\View;
$this->title = Yii::t('app', 'Countries');
$this->params['breadcrumbs'][] = $this->title;
yii\bootstrap\Modal::begin(['id' => 'modal']);
yii\bootstrap\Modal::end();
?>
<div class="country-index">
<h1><?= Html::encode($this->title) ?></h1>
<div>Current Time: <?= date('Y/m/d H:i:s') ?></div>
<p><?= Html::a(Yii::t('app', 'Create Country'),
['create'],
['class' => 'btn btn-success show-modal']) ?>
</p>
<?php Pjax::begin(['id' => 'pjax-grid']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>
<?php
$this->registerJs("$(function() {
$('.show-modal').click(function(e) {
e.preventDefault();
$('#modal').modal('show').find('.modal-body')
.load($(this).attr('href'));
});
});", View::POS_READY, '.show-modal');
?>
View _form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\View;
?>
<?php
$this->registerJs(
'$("document").ready(function(){
$("#pjax-create").on("pjax:end", function() {
$.pjax.reload({container:"#pjax-grid"}); //Reload GridView
});
});'
, View::POS_READY, 'pjax-create-end');
?>
<div class="country-form">
<?php yii\widgets\Pjax::begin(['id' => 'pjax-create']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => TRUE]]); ?>
<?= $form->field($model, 'name')->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(); ?>
<?php yii\widgets\Pjax::end() ?>
</div>
Use Html::button instead Html::a and simple overwrite the content of the modal:
yii\bootstrap\Modal::begin(['id' => 'modal']);
echo '<div id="modal-content"></div>';
yii\bootstrap\Modal::end();
echo Html::button('Create Country', [
'onClick' => 'createCountry("' . Url::to([create]) . '")',
'class' => 'btn btn-primary'
]);
$script = <<< JS
function createCountry(url) {
$('#modal').modal('show').find('#modal-content').load(url);
}
JS
$this->registerJs($script, View::POS_END);

Yii2 Color Picker in Form

Is there any built in class or an extinction that makes the user pick color from something like this?
You can simply render a color input like this with out using any plugins.
<?= $form->field($model, 'color', [
'template' => "{input}"
])->input('color',['class'=>"input_class"]) ?>
You can use kartik\widgets\ColorInput widget. first install kartik\widgets\ColorInput from this link
e.g. (Updated)
use kartik\widgets\ColorInput; or kartik\color\ColorInput;(for previous ver.)
<?php $form = ActiveForm::begin([
'id' => 'form',
]); ?>
<?= $form->field($model, 'color_code')->widget(ColorInput::classname(), ['options' => ['placeholder' => 'Select Color...'],]); ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-primary btn-create' : 'btn btn-info btn-create']) ?>
</div>
<?php ActiveForm::end(); ?>

How to handle multiple forms in the same view?

I have a default form for my specific view.
Through an element I (dinamically) include another view (using view extension) in order to provide an upload form.
My problem is that the second form seems to submit the first one.
Default form
<div class="content-box-content">
<?php
echo $this->Form->create("WebSubject", array(
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'input-notification error png_bg'
)
)
)
));
?>
<?=$this->Form->input('id', array('type' => 'hidden'))?>
<?=$this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire'))?>
<?=$this->Form->input('description', array('type' => 'textarea', 'label' => 'Descriere', 'id' => 'description'))?>
<?=$this->Form->input('description_long', array('type' => 'textarea', 'label' => 'Continut', 'id' => 'description_long'))?>
<?=$this->Form->submit('Salveaza', array('class' => "button"))?>
</div>
This way I include the element
<div class="tab-content default-tab" id="fotoUploadTab">
<?php
echo $this->element('file_upload_form', array(
'view' => 'upload_admin',
'webFileType' => 'image',
'redirect' => $SHT['here']
)
);
?>
<div class="tab-content default-tab">
Lista imagini
</div>
</div>
Element code
<?php
$view = (isset($view)) ? $view : "upload_admin";
$webFileType = (isset($webFileType)) ? $webFileType : "image";
$redirect = (isset($redirect)) ? $redirect : "/";
?>
<?php
$this->extend("/WebFiles/".$view);
?>
Extended View code
<div class="tab-content default-tab">
<?php echo $this->Form->create("WebFile", array('action' => '/', 'type' => 'file')); ?>
<input type="hidden" name="redirect" value="" />
<?php echo $this->Form->input('entity_id', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('entity_table_name', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('type', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire')); ?>
<?php echo $this->Form->input('description', array('class' => "text-input small-input", 'label' => 'Descriere')); ?>
<?php echo $this->Form->submit('Upload', array('class' => 'button')); ?>
</div>
As seen in the last snippet, I tried to force the last form by providing an action url, but on submiting it, it sends data as the first one does.
How should I handle this ?
Thank you!
If you just want to have both forms, the one from parent and the other from the child view/element make sure you call $this->Form->end() in both templates and that you are not nesting a form inside the other. Probably, in your case, just by adding end() to both forms will solve your issue.
As a side note, you cannot have a parent view opening a Form with $this->Form->create() and inject fields into it using the child view, basically because you need create() to be called before any input is rendered and parent views are rendered after the child is executed.