Laravel like Magento 1.x validator.
Magento validator is a validation service to validate inputs for requests. It depends on Zend validation classes, for more information about these validation rules visit Zend Documentation
Require the Magento validator via composer
composer require softxpert/magento1-validator
This isntallation will install modman package to map Magento files from the vendor directory.
Execute the following commands to map the module files into your Magento project
vendor/bin/modman init
# If this is the first installation
vendor/bin/modman link vendor/softxpert/magento1-validator
# If this is an update, then we need to re-link the files
vendor/bin/modman repair vendor/softxpert/magento1-validator
# Regenerate autoload files
composer dumpautoload
Finally we are done, and you'll find the module files under app/code/community/Softxpert/Validator
directory.
To validate a request you have 2 ways:
// Create an instance of the validator model
/** @var Softxpert_Validator_Model_Validator $validator */
$vaidator = Mage::getModel('softxpert_validator/validator');
// Preparing validator with data and rules arrays
$data = $this->getRequest->getParams();
$rules = [
'username' => 'required|alpha_num',
'email' => 'required|string|email',
];
$validator->validate($data, $rules);
// Check whether the data fails or passes
// returns true|false
if ( $validator->fails() ) {
// Do some stuff
}
It supports nested inputs too.
$data = [
'banner' => [
'url' => 'some value',
'link_type' => 'cutom',
'roles' => ['admin', 'user'],
'words' => [
[
'letters' => 789
],
[
'letters' => 'string'
]
]
]
];
$rules = [
'banner.url' => 'string',
'banner.url' => 'string|in:valid,custom',
'banner.roles.*' => 'string',
'banner.words.*.letters' => 'string',
];
/** @var Softxpert_Validator_Model_Validator $validator */
$vaidator = Mage::getModel('softxpert_validator/validator');
$validator->validate($data, $rules)->redirectOnFailure();
Create an observer for that particular route and handle all your request validations.
For more example usage, visit the ValidationsTest class.
String under alpha rule must consist of alphabetic characters.
String under alpha_num rule must consist of alphabetic and numeric characters.
Validates a given value to be between min
and max
values.
Break chain of rules on the first failure.
Validates the given value to equal to a given date format.
If format is not given, then the default format (yyyy-mm-dd)
will be used.
Validates a given value to be numeric.
Validates a given value to be an email.
Validate a given value to be an even number.
Allowed extensions for a particular file input field.
Validates a given value to be float.
Validates the given value to be greater than a min
value.
Validates a given value to be an integer.
The file under validation must be an image (jpeg, png, bmp, gif, ...)
Validates that a given value is one of the provided options.
Validates the given value to be an IP v4 address.
Validates the given value to be less than a max
value.
Field under validation is allowed to be null/empty.
Validates a given value to be odd number.
Validates the given string to match a given pattern.
Eg: regex:/^Test/
Field under validation is required.
Validates that the given value is a string.
Validates that the given file name is in the provided extension set (no MIME type checks).
String size validates a string size with min
and max
values.
If min
and max
are not provided, then the rule validates the value as a string.
If only min
is provided, then the rule validates the minimum string length with unlimited maximum length.
Validates the value to be a postal zip code. Locale must be provided.
Eg: zip_code:ar_EG
Prepare the validator with data and rules.
/**
* @param array $data
* @param array $rules
* @return Softxpert_Validator_Model_Validator
*/
public function validate($data, $rules)
Checks whether the data are valid or not.
If $fieldName
is provided, then the validation would be applied only on this field.
/**
* @param string|null $fieldName
* @return bool
*/
public function isValid($fieldName = null)
Checks whether the data fails the validation or not.
If $fieldName
is provided, then the validation would be applied only on this field.
/**
* @param string|null $fieldName
* @return bool
*/
public function fails($fieldName = null)
Return validation error messages array under errors
error bag.
/**
* @return array
*/
public function getMessages()
Return validation error messages in an HTML un-ordered list block.
/**
* @return string
*/
public function getMessagesTemplate()
End request and return with error messages on validation failure. This method supports both, redirecting to an HTML page with session error messages or JSON response.
If $redirectTo
is not specified, then the request will redirect to the previous page.
/**
* @param bool $wantsJson
* @param string|null $redirectTo
*/
public function redirectOnFailure($wantsJson = false, $redirectTo = null)
Determine whether the validation errors should break and return on the first in-valid rule, or return a complete set of error messages
/**
* @param bool $flag
* @return $this
*/
public function noChain($flag = true)
Please, read CONTRIBUTING.md for details on the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details
Please, read the CHANGELOG.md for more details about releases updates.