Giter Site home page Giter Site logo

silverstripe-ajaxforms's Introduction

SilverStripe Ajax Forms

Introduction

Ajax forms for silverstripe, using jquery and jquery validate

Requirements

  • SilverStripe CMS ^4.0
  • jQuery
  • jQuery validate

Installation

composer require "thewebmen/silverstripe-ajaxforms"

How to use

Make sure you have jquery and jquery validate loaded on your pages with ajax forms. Also include the following js file:

Requirements::javascript('thewebmen/silverstripe-ajaxforms:resources/js/ajaxforms.js');

Then create a form by extending the class TheWebmen\Ajaxforms\AjaxForm that returns an instance of the TheWebmen\Ajaxforms\AjaxFormResponse class on success.

Form example

use TheWebmen\Ajaxforms\AjaxForm;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\RequiredFields;

class ExampleForm extends AjaxForm {

    public function __construct(RequestHandler $controller = null)
    {
        $fields = new FieldList(array(
            TextField::create('MyField', 'This is a field')
        ));

        $actions = new FieldList(array(
           FormAction::create('handle', 'Send')
        ));

        $validator = new ExampleFormValidator(array(
            'MyField'
        ));

        parent::__construct($controller, 'MyField', $fields, $actions, $validator);
    }

    public function handle($data){
        $response = new \TheWebmen\Ajaxforms\AjaxFormResponse();
        return $response->redirect('http://www.google.nl');
    }

}

class ExampleFormValidator extends RequiredFields {

    public function php($data)
    {
        $valid = parent::php($data);
        if($data['MyField'] != 'Test'){
            $this->validationError(
                'MyField',
                'This field is only valid if the value is: Test',
                'required'
            );
            $valid = false;
        }
        return $valid;
    }

}

Form errors are displayed using jQuery validate, you can initiate your own jQuery validate to customize the settings or you can let the ajaxforms.js script handle this to use jQuery validate's default settings.

The class ".is-posting" is added to the form while it is positing, you should use this class to add some progress styling.

Todo

  • Improve documentation
  • Add check if page exist for the redirectToPage response

silverstripe-ajaxforms's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.