yii2-ajaxform-plugin

0.0.4 • Public • Published

yii2-ajaxform-plugin

A simple plugin for ajax forms.
The plugin for use with the yii\widgets\ActiveForm

Installing using Composer

composer require rkit/yii2-ajaxform-plugin

Then, register asset

rkit\yii2\plugins\ajaxform\Asset::register($this);

Installing using NPM

npm install yii2-ajaxform-plugin --save

Then, add the module

require('yii2-ajaxform-plugin');

Usage

$('.ajax-form').yiiAjaxForm({
    beforeSend: function() {},
    error: function() {},
    complete: function() {},
    success: function(data) {},
  });

available $.ajax options

You can access to the form and button that caused the the form submit event

var $form = $(this);
var $button = $(this).data('yiiActiveForm').submitObject;

You can invoke validation method

$(this).yiiActiveForm('updateMessages', data);

Example

$('.ajax-form').yiiAjaxForm({
    beforeSend: function() {
      var $button = $(this).data('yiiActiveForm').submitObject;
      if ($button) {
        $button.prop('disabled', true);
      }
    },
    complete: function() {
      var $button = $(this).data('yiiActiveForm').submitObject;
      if ($button) {
        $button.prop('disabled', false);
      }
    },
    success: function(data) {
      if (data.redirect) {
        document.location.href = data.redirect;
      } else if (data.reload) {
        location.reload(true);
      }
      // show validation error messages
      $(this).yiiActiveForm('updateMessages', data);
    },
  });
 

Readme

Keywords

Package Sidebar

Install

npm i yii2-ajaxform-plugin

Weekly Downloads

3

Version

0.0.4

License

MIT

Last publish

Collaborators

  • rkit