Magento 1.x Validator

Magento 1.

x ValidatorMohamed Abdul-FattahBlockedUnblockFollowFollowingMar 22Magento is one of the most flexible and powerful e-commerce platforms in the market; what you might not know about Magento is that it is also an object-oriented (OO) PHP framework developed on top of Zend Framework.

Unfortunately, this flexibility adds some level of complexity too.

Currently, a clean installation of Magento has around 30,000 files and over 1.

2 million lines of code.

One of the most complex parts of Magento 1.

x is to validate inputs on the server side with Zend validations.

 Validations with Zend can go into three different ways:1.

Instantiating a new validation object for every input with the corresponding validation rule/class.

Zend Validation Classes2.

Using Zend static validator instead of instantiating new objects for every validation rule.

Zend Static Validator3.

Using Zend Filter Input to chain validation rules into one validator object to use at once.

Zend Filter Input ValidatorAs you see, the 3rd way is the best way to validate inputs for a controller action with a single method.

But still creating the rules array is a complex task especially for complex rules where the input should be validated with more than one rule and meta-commands.

Meanwhile, the PHP community has the Laravel framework which implements a fabulous validator that easies the developers' lives with its extensive rules, flexibility, and simplicity.

And fortunately, we had (at Softxpert) implemented a Laravel like validator for Magento 1.

x platforms that eases and encourages implementing server-side validations for better security and stability.

Here I’ll walk you through the installation process and some example usages for the module.

Requiring the module via the composer was a problem because Magento 1.

x doesn’t support the composer and cannot read modules from the vendor directory.

But fortunately, modman helps to link the module files to the Magento file structure properly.

# Import the modulecomposer require softxpert/magento1-validatorThis installation will install the modman package to map Magento files from the vendor directory.

Execute the following commands to map the module files into your Magento projectvendor/bin/modman init# If this is the first installationvendor/bin/modman link vendor/softxpert/magento1-validator# If this is an update, then we need to re-link the filesvendor/bin/modman repair vendor/softxpert/magento1-validator# Regenerate autoload filescomposer dumpautoloadFinally, we are done with the installation, and you’ll find the module files under app/code/community/Softxpert/Validator directory.

Using the Softxpert validator is as simple as writing down the rules separated by a single pipeline.

Validating inputs with Softxpert ValidatorWhile complex rules are now simplified by typing down all the needed rules into a single line, nested arrays could add complexity to the validation logic by breaking the data array into flat variables to be validated per input.

Fortunately, we don’t have to break the data array into smaller inputs because this could be done with the wild card * character as follows.

Validating nested arraysNotice that we can chain the validator with the redirectOnFailure method which automatically redirects to the previous page with error messages in the session error variable, or returns JSON error messages if a true flag was passed as a 1st parameter redirectOnFailure(true) .

For further information about the available rules and methods, please visit the Github repo.

.

. More details

Leave a Reply