2017 © Pedro Peláez
 

library validation

SamsonPHP validation module

image

samsonphp/validation

SamsonPHP validation module

  • Friday, August 26, 2016
  • by samsonos
  • Repository
  • 2 Watchers
  • 0 Stars
  • 49 Installations
  • JavaScript
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

SamsonPHP validation module

SamsonPHP validation implementation, (*1)

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Total Downloads Stories in Ready, (*2)

Documentation

Follow official documentation., (*3)

Installation

You can install this package through Composer: composer require SamsonPHP/validation The packages adheres to the SemVer specification, and there will be full backward compatibility between minor versions., (*4)

Testing

$ vendor/bin/phpunit, (*5)

Usage example

var formValidation = new FormValidate([
    new FieldValidate({
        el: '#product_name',
        insertTo: '.error-wrapper',
        styleList: '*'
    }).required().length(),
    new FieldValidate({
        el: '#product_description',
        insertTo: '.error-wrapper',
        styleList: '*'
    }).required(),
    new FieldValidate({
        el: '#product_usp',
        insertTo: '.error-wrapper',
        styleList: '*'
    }).required()
]);

s('#send-form-button').click(function () {
    return formValidation.validate();
});

Using FieldValidator

Just create instance of FieldValidator class and pass options to it:, (*6)

var productFieldValidator = new FieldValidate({
  el: '#product_name',
  insertTo: '.error-wrapper',
  styleList: '*'
}).required().url()

And if you want get result of validation you have to call this method:, (*7)

productFieldValidator.validate(); //=> true|false

In this example:, (*8)

  • el - [required] Selector to field, can be string or s() element
  • insertTo - Selector to field which can dysplay error message, can be string or s() element
  • styleList - Prefix whcih will instet to message text(default: '')

But there can be a lot of options:, (*9)

  • trim - Need remove prefix and suffix spaces
  • fieldErrorClass - Class which will be added to field, can be string or array.(default: ['class-error-field-', 'class-error-field'])

If there is array then first element of it will be prefix to all false passed validators., (*10)

For example ['class-error-field-', 'class-error-field'], (*11)

Will add such classes to field: , (*12)

  • showErrorBlock - Show error block or not. Default if insetTo element passed then block will show. If you want hide error block then pass false to it
  • insertToType - Where insert error block(default:MODE_INSERT_TO_PARENT):, (*13)

    MODE_INSERT_TO_DEFAULT - There no changes with insertTo element, (*14)

    MODE_INSERT_TO_PARENT - Find elemnet insert to in parent element of this field, (*15)

  • showErrorBlockType - Where output classes(default:MODE_SHOW_ERROR_BLOCK_DEFAULT):, (*16)

    MODE_SHOW_ERROR_BLOCK_DEFAULT - Change classes in field, (*17)

    MODE_SHOW_ERROR_BLOCK_PARENT - Change classes in parent block, (*18)

Crate custom validator

There very easy to create new validator. It can be class validator or alias, (*19)

For create new validator just create class with export and validate methods export method will return object which will be added to main field object validate method which validate field with custom login, (*20)

Constructor of class can receive field object, (*21)

There is simple required validator:, (*22)

 /**
 * Required validator
 */
var RequiredValidator = (function () {

    /**
     * Init class
     * @param field
     */
    var constructor = function (field) {

        // Required type default values
        this.id = 'required';
        this.defaultRequiredMessage = 'Field have to be filled';
        this.field = field;
    };

    var self = constructor.prototype;

    /**
     * Export data into field
     */
    self.export = function () {
        var validatorInstance = this;

        return {

            /**
             * Required validation
             * @param errorMessage
             * @returns {FieldValidate}
             */
            required: function (errorMessage) {

                validatorInstance.defaultRequiredMessage = errorMessage || validatorInstance.defaultRequiredMessage;
                this.reservedValidators.push(validatorInstance.id);
                return this;
            }
        }
    };

    /**
     * Validate validator
     */
    self.validate = function () {

        if (this.field._getFieldValue() == '') {
            this.field.addError(this.id, this.defaultRequiredMessage);
            this.field.valid = false;
        } else {
            this.field.removeError(this.id);
            this.field.valid = true;
        }
    };

    return constructor;
})();

You need add your validator to register validator array, (*23)

// Register all used validators
this.registerValidator = [
    new RequiredValidator(this)
];

required method will be added to field instance and can be access as new FieldValidator(...).required() and such validator can be added, (*24)

And you can use alias validator, (*25)

There is when you need override some default values form another class validator and save it as new validator., (*26)

For example lets create url validator which use regExp validator, (*27)

/**
 * Url regexp validator
 */
var UrlValidator = {

    export: function () {
        return {

            /**
             * Url validation
             * @param errorMessage
             * @returns {FieldValidate}
             */
            url: function (errorMessage) {
                var msg = errorMessage || 'Field should be a url',
                    urlPattern = /^(https?:\/\/)?[\w]*\.[\w\-\&\#\=\/\.?\(\)]*$/;

                this.regExp(urlPattern, msg);
                return this;
            }
        }
    }
};

As you can see there is only one methond in object which return new object which will be used for extending, (*28)

And you have to add to register array this validator as object without creating, (*29)

// Register all used validators
this.registerValidator = [
    new RequiredValidator(this),
    new RegExpValidator(this),
    UrlValidator
];

And you can use it as:, (*30)

new FieldValidator(...).required().url(), (*31)

But You should not use this validator with regExp, (*32)

Contributing

Feel free to fork and create pull requests at any time., (*33)

Security

If you discover any security related issues, please use this repository issue tracker., (*34)

License

Open Software License ("OSL") v 3.0. Please see License File for more information., (*35)

SamsonOS, (*36)

The Versions

26/08 2016

dev-master

9999999-dev http://samsonos.com/

SamsonPHP validation module

  Sources   Download

Open Software License (OSL) v 3.0

The Requires

 

validation validate validate form validate field

26/08 2016

0.0.2

0.0.2.0 http://samsonos.com/

SamsonPHP validation module

  Sources   Download

Open Software License (OSL) v 3.0

The Requires

 

validation validate validate form validate field

18/03 2016

0.0.1

0.0.1.0 http://samsonos.com/

SamsonPHP validation module

  Sources   Download

Open Software License (OSL) v 3.0

The Requires

 

validation validate validate form validate field