yii2 Ajax Submit

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">

&lt;?php &#036;form = ActiveForm::begin(['id'=&gt;'addjob','enableAjaxValidation'=&gt;true]); ?&gt;





&lt;div class=&quot;col-md-12&quot;&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'job_id')


            -&gt;dropDownList(


            ArrayHelper::map(Jobs::find()-&gt;all(), 'id','job_name'),[&quot;class&quot;=&gt;&quot;form-    control&quot;,&quot;prompt&quot;=&gt;&quot;select&quot;] ) ?&gt;


&lt;div class=&quot;form-group&quot;&gt;


    &lt;?= Html::submitButton(&#036;model-&gt;isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' =&gt; &#036;model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?&gt;


&lt;/div&gt;





&lt;?php ActiveForm::end(); ?&gt;

</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();

});

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();

});

Hey use https://github.com/ronysilvati/yii2-ajax-request