shimina
(Shimina P)
1
Hi,
How can i use ActiveForm with these requirements?
Submit form with ajax.
Before submitting with ajax: Check if error exits.
this is my view
<h1>Add New Job </h1>
<div class="work-jobs-form",id="workjob">
<?php $form = ActiveForm::begin(['id'=>'addjob','enableAjaxValidation'=>true]); ?>
<div class="col-md-12">
<?= $form->field($model, 'job_id')
->dropDownList(
ArrayHelper::map(Jobs::find()->all(), 'id','job_name'),["class"=>"form- control","prompt"=>"select"] ) ?>
<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>
Try this
$('form').on('beforeSubmit', function(e) {
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
success: function (data) {
...
},
error: function () {
alert("Something went wrong");
}
});
}).on('submit', function(e){
e.preventDefault();
});
LucasK
(Igor90riv)
3
Thx, work
<?php $form = ActiveForm::begin(['id' => 'form-order-article', 'enableClientValidation' => true, 'enableAjaxValidation' => false,
'action' => ['/personal/send-order-article'],
'options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true, 'class' => 'form-control']) ?>
<?= $form->field($model, 'url')->textInput(['autofocus' => true, 'class' => 'form-control']) ?>
<label for="usr"> <?= Yii::t('menu', 'form_create_article_category')?></label>
<div class="bs-docs-example">
<?= Html::activeDropDownList($model, 'id_category',
ArrayHelper::map(\common\models\Category::find()->all(), 'id', 'name'),
['class' => 'selectpicker dropdownlist form-control',
'data-width' => '100%',
'multiple' => 'true']) ?>
</div>
<?= $form->field($model, 'suggestions', ['inputOptions' => ['class' => 'summernote form-control']])->textarea(['rows' => 6]) ?>
<?= Html::input('submit', '0', Yii::t('menu', 'form_submit')); ?>
<?php ActiveForm::end(); ?>
$('#form-order-article').on('beforeSubmit', function(e) {
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
success: function (data) {
alert('Test');
},
error: function () {
alert("Something went wrong");
}
});
}).on('submit', function(e){
e.preventDefault();
});