npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autoajax

v2.1.4

Published

AutoAjax form library for sending forms and automaticall laravel validation.

Downloads

29

Readme

AutoAjax.js

This package builds and sends ajax requests from <form> inputs. Then automatically receives upcoming validated request and provide automatical error messages management with all events required for Ajax management. Suitable for Laravel or plain PHP.

Example

This is part of package for implementation in JavaScript / VueJs application side. More for PHP / Laravel side of this package on autoAjax

Features

  • Automatically builds request data from <form> input elements.
  • Handles Laravel validation and automatically binds error messages to each input in form.
  • VueJs integration
  • PlainJs integration

Installation via NPM

npm i autoajax --save

Or basic installation

<!-- jQuery and jQuery form -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.50/jquery.form.min.js"></script>

<!-- autoAjax.min.js -->
<script src="dist/autoAjax.min.js"></script>

VueJs Integration

var autoAjax = require('autoajax');

Vue.use(autoAjax);

Initializing autoAjax form in VueJS

If you want initialize autoAjax form in VueJS component, you need use v-autoAjax directive in form element.

<template>
    <form method="post" action="/contact" v-autoAjax="formOptions" v-autoAjaxRow="rowData" @onSubmit="onSubmit" @onSuccess="successEvent" @onValidation="validationEvent" @onError="errorEvent" @onComplete="onComplete">
        <div class="form-group">
            <input type="text" name="email">
        </div>
        <div class="form-group">
            <textarea name="content"></textarea>
        </div>
        <div class="form-group">
            <button type="submit">submit form</button>
        </div>
    </form>
</template>

<script>
export default {
    data: {
        rowData : {
            email : '[email protected]',
            content : 'content',
        },
        formOptions : {
            //...
        }
    },
  
    methods: {
        onSubmit(form) {
            console.log(form);
        },
        successEvent(data, response) {
            console.log(data, response);
        },
        errorEvent(data, response) {
            console.log(data, response);
        },
        validationEvent(data, response) {
            console.log(data, response);
        },
        onComplete(data, response) {
            console.log(data, response);
        }
    }
}
</script>

VueJs directives

Form initialization

Basic autoAjax form initialization you can bind with:

<form v-autoAjax></form>

For custom form options you can use directive with options parameter like:

<form v-autoAjax="myFormOptions"></form>

Automatically reset input values on success response

<form v-autoAjax v-autoReset></form>

Automatically bind row data into form fields

<form v-autoAjax v-autoAjaxRow="myRowData"></form>

VueJs Events

This is list of available form events. Need to be placed in form element.

On every form submit

@submit="mySubmitEvent" or @onSubmit="mySubmitEvent"

Receiving success response data on HTTP 200

@success="mySuccessEvent" or @onSuccess="mySuccessEvent"

Receiving error response data on HTTP 500 and other error codes.

@error="myErrorEvent" or @onError="myErrorEvent"

On (laravel) validation error HTTP 422 or HTTP 430 error code

@validation="myValidationErrorEvent" or @onValidation="myValidationErrorEvent"

On all completed HTTP responses with any code

@complete="myCompleteEvent" or @onComplete="myCompleteEvent"

Plain JavaScript Integration

Initializing autoAjax form instance in plain JS

If you want initialize autoAjax form, you initialize autoAjax method on your form element.

<form method="post" action="/contact" data-row="{ email : '[email protected]' }" class="myAutoAjaxform">
    <div class="form-group">
        <input type="text" name="email">
    </div>
    <div class="form-group">
        <textarea name="content"></textarea>
    </div>
    <div class="form-group">
        <button type="submit">submit form</button>
    </div>
</form>

<script>
$(function(){
    var options = {
        //Resets for on success response
        autoReset : true,
    
        onSubmit : function(data, response){},
        onSuccess : function(data, response){},
        onError : function(data, response){},
        onValidation : function(data, response){},
        onComplete : function(data, response){},
    
        //Can be used also form without "on" at the beggining
        // submit : function(data, response){...
        // success : function(data, response){...
    
        //and any other settings from autoAjax options...
    };
    
    $('form.myAutoAjaxform').autoAjax(options)
});
</script>

Available classes for HTML forms

autoReset - resets form values after success message

Available attributes for HTML forms

data-row - JSON Value of data which will be binded into form fields.

AutoAjax Options

AutoAjax options can be applied in VueJs directive v-autoAjax="myOptions" or in jQuery initialization autoAjax on form element $('#myForm').autoAjax({ ... }). This options will be applied only on selected form instance.

var options = {
    //Automatically resets all form inputs to default values after success response
    autoReset : false,
    
    //Automaticaly add validation error messages after each bad filled input
    showInputErrors : true,
    
    //General success/error/validation form message
    showMessage : true,
    
    //Automaticaly add global validation error message into form message element
    //showMessage needs to be true
    showValidationMessage : true,
    
    //Available selectors and classes
    selectors : {
        messageSelector: '.alert',
        messageSuccessClass : '.alert-success',
        messageErrorClass : '.alert-danger',
        inputWrapperErrorClass : '.has-error',
    },
    
    //All available messages
    messages : {
        error : 'Something went wrong, please try again later.',
        validation: 'Please fill all required fields.',
    },
       
    //Event on every form submit
    onSubmit() => {},
    
    //Event on success response (HTTP 200)
    onSuccess() => {},
    
    //Event on error response
    onError() => {},
    
    //Event on validation response
    onValidation() => {},
    
    //On all completed responses codes
    onComplete() => {},
    
    //Generate error message for input element
    getErrorMessageElement(message, key, form) {
        return '<span class="error-message error">'+message+'</span>';
    },
    
    //Add validation error message after this element
    addErrorMessageAfterElement(input){
        //You can modify, where should be placed validation error message for each input
        //If you want place validation after input parent, you can do something like:
        //return input.parent();
        
        return input;
    },
}

AutoAjax global options

You can mutate global AutoAjax options for all form instances.

autoAjax.setOptions({
    //Other settings from autoAjax options
    //..
    
    //Custom message
    messages: {
        error : 'My custom global error message',
    },
    
    //Custom error message element
    getErrorMessageElement(message){
        return '<span class="my-custom-error-message error">'+message+'</span>';
    }
});