Jump to content

Validator


justb3a
 Share

Recommended Posts

TL;DR: I know, there is already an Validation Module. And it wasn't my intend to build another module. But I needed it in so many places which lead to duplicate code and suddenly there was a module  ^_^ Perhaps it's useful to someone. At the moment there are only validators I need in my current project, if you miss one, please tell me or (preferred) send a pull request!

 ------
 
This module provides a set of useful validation methods using ProcessWire sanitizers.

Example Usage:

$conf = array(
  'username' => array('isEmpty', 'isUnique' => array('ident' => 'name', 'sanitize' => 'username')),
  'pass' => array('range' => array('min' => 6, 'max' => 20)),
  'pass_confirm' => array('isEqual' => array('equal' => 'pass'))
);

$validator = new Validator;
$validator->setConfig($conf);
if (!$validator->isValid()) $errors = $validator->getErrors();

You can also use it to validate POST requests:

$validator = new Validator;
$validator->setConfig($validatorConf);

// validation failed
if (!$validator->isValid()) {
  $data = array(
    'success' => false,
    'errors' => $validator->getMessages() // $validator->getErrors()
  );
  $this->returnJson($data, 400);
}
❯ http -f POST http://pw.dev/v1/user/ email=info@com username=exampleUser pass=short
{
    "errors": {
        "email": [
            "Please enter a valid email address."
        ],
        "pass": [
            "This field must be at least '6' characters.",
            "'short' must contain at least one digit character"
        ]
    },
   "success": false
}

For a detailed documentation please have a look at the guides:

Edited by Nico Knoll
Added the "module" tag and removed it from title.
  • Like 10
Link to comment
Share on other sites

@rot: I expected such questions. Therefore I wrote a little introduction. You do not have to use this module. It's fine to use the other module. That's why I mentioned it. If you want to I can delete this thread/module and just use it for my own. I just started to validate some inputs and then it became more and more and I outsourced my code.

Link to comment
Share on other sites

@rot: I expected such questions. Therefore I wrote a little introduction. You do not have to use this module. It's fine to use the other module. That's why I mentioned it. If you want to I can delete this thread/module and just use it for my own. I just started to validate some inputs and then it became more and more and I outsourced my code.

I did not mean to criticise or offend you. Its great that you shared your work.

I just did not understand what made you write the module and not use the other one from the intro provided with this post and thought it might be of help if you tried to answer that question here. I asked for the USP if you want to call it that way.

  • Like 1
Link to comment
Share on other sites

It's a very valid question. It's where it can lead to a module mess much like in every module/plugin based system. The kind of: "I have 20 modules that do validation. Which is better, which really is tested and works? Which works for which PW version..." etc. 

  • Like 4
Link to comment
Share on other sites

I don't think so, its better to have multiple options yeah there's an existing Validator Module however they will always have different design paradigm I for once don't like GUMP, I like this API it's cleaner also it reminds me of Yii, imagine a platform where we only have one option. 

@justb3a

I actually love this Validator, will probarbly fork this and see ways to add custom validation and also a way to inject this in InputForm process input. Good work bro or sis 

  • Like 3
Link to comment
Share on other sites

It's a very valid question. It's where it can lead to a module mess much like in every module/plugin based system. The kind of: "I have 20 modules that do validation. Which is better, which really is tested and works? Which works for which PW version..." etc. 

first: "the better one - is the enemy of the good one" (german adage i think)

second: this is OT but this fact is one of the little deficits in the modules directory

->is it tested? hmm yes it is in the modules directory

->workd with PW version is not always uptodate....but if you reading some days the forums you learn that almost all older modules work with the actual PW version.

maybe there could be a rating like the "Like" function to hit a button for logged in users "Tested with version" and on the Compatibility line it could be a tag like this

Compatibility (Usertests)            |           2.5 (12)

would be very more informative for new PW users and the module autor (or someone else) would not have to set the modulenumber higher since there is an positive test for this version.....and this would give a good filter like "most tested" or modules "tested for 2.5"....just an idea

sorry for OT may this post fits in a other threat better.

@justb3a: thank you for this one, like Sephiroth wrote GUMP is may not the best option for everyone and your module looks very clean and easy to use i'll try it with the next project (there will be a lot to validate so i will report in a few weeks) :rolleyes: !

  • Like 3
Link to comment
Share on other sites

:undecided: I do not want to start a discussion and I can definitely understand both points of view. It's better to contribute to an existing module as reinventing the wheel again and again. But sometimes it's also good to have the possibility to choose between modules so you be able to pick the module which fits the best to your needs. 

Had I known what was awaiting me during this project, I maybe would have used the existing validation module. On the other site, processwire comes with some great sanitizers which are designed for the special processwire purposes (for example $sanitizer->username). An external validation library will never support this but sometimes you do want to test exactly such behavior and it's already there. Why not use this? 

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...