samsonphp / validation
SamsonPHP validation module
Installs: 57
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 2
Open Issues: 1
Language:JavaScript
Requires
This package is not auto-updated.
Last update: 2024-11-07 01:03:36 UTC
README
SamsonPHP validation implementation
##Documentation Follow official documentation.
##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.
##Testing
$ vendor/bin/phpunit
##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:
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:
productFieldValidator.validate(); //=> true|false
In this example:
- 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:
- 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.
For example ['class-error-field-', 'class-error-field']
Will add such classes to field: <input ... class="class-error-field-required class-error-field-regexp class-error-field">
- 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):
MODE_INSERT_TO_DEFAULT - There no changes with insertTo element
MODE_INSERT_TO_PARENT - Find elemnet insert to in parent element of this field
- showErrorBlockType - Where output classes(default:MODE_SHOW_ERROR_BLOCK_DEFAULT):
MODE_SHOW_ERROR_BLOCK_DEFAULT - Change classes in field
MODE_SHOW_ERROR_BLOCK_PARENT - Change classes in parent block
##Crate custom validator There very easy to create new validator. It can be class validator or alias
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
Constructor of class can receive field object
There is simple required validator:
/**
* 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
// 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
And you can use alias validator
There is when you need override some default values form another class validator and save it as new validator.
For example lets create url validator which use regExp validator
/**
* 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
And you have to add to register array this validator as object without creating
// Register all used validators
this.registerValidator = [
new RequiredValidator(this),
new RegExpValidator(this),
UrlValidator
];
And you can use it as:
new FieldValidator(...).required().url()
But You should not use this validator with regExp
##Contributing Feel free to fork and create pull requests at any time.
##Security If you discover any security related issues, please use this repository issue tracker.
##License Open Software License ("OSL") v 3.0. Please see License File for more information.