FormValidation Helper code including jquery validator remote checks, dependencies
Permalink
Dear All --
For a project I'm working on I've had to significantly extend the FormValidationHelper. It could use some cleaning up, additional features, etc, but I figure I'll post it here if anybody wants to use bits and pieces.
A few key features.
1. Unlike the core, this keeps required and other checks separate. So if you use the helper's email validation, it'll only return an error if there's SOMETHING and that SOMETHING isn't an email address. I think this is better, but something to be aware of. That's why I recreated some of the validation constants.
2. The addRequiredDependency is does a required check based on the value of another field (e.g. "shipping address is required if distributionMethod != 'download'"). So far, I've only needed it for radio buttons, so other functionality doesn't exist.
3. The remote (or unique) check calls another class and passes it the value and allows the class to check that the value is unique. You call it something like this
Where code is the field name, ID is the existing ID (you have to exclude the current record in unique checks), 'elements' is the filename, AmpElementList is the class, and amplify_lmt is the package (the last three are used to find, load, and instantiate the class). Within that class, I have a function like
which returns true if there are no other records with the same value.
This is called during the normal $vf->test(), but there's also have a tool file that calls this on command (see next bullet point):
4. I've added jquery validator support. There's a method that outputs the relevant validation rules (not all are supported) in a array structure that can be passed to jquery validate. This includes the unique check (jquery calls it remote), which is what the tool is for (note that there's some hardcoding of the URL). It's run something like this:
Note that getModelValidation() returns my validation helper. I've included that method in all of my models which sets the validations and returns a validation helper object; it works out pretty well.
Have fun, and if you're having any difficulties, just ask.
(You'll have to remove the jpg extension.)
James
For a project I'm working on I've had to significantly extend the FormValidationHelper. It could use some cleaning up, additional features, etc, but I figure I'll post it here if anybody wants to use bits and pieces.
A few key features.
1. Unlike the core, this keeps required and other checks separate. So if you use the helper's email validation, it'll only return an error if there's SOMETHING and that SOMETHING isn't an email address. I think this is better, but something to be aware of. That's why I recreated some of the validation constants.
2. The addRequiredDependency is does a required check based on the value of another field (e.g. "shipping address is required if distributionMethod != 'download'"). So far, I've only needed it for radio buttons, so other functionality doesn't exist.
3. The remote (or unique) check calls another class and passes it the value and allows the class to check that the value is unique. You call it something like this
$vf->addUnique('code', $this->ID, 'elements', 'AmpElementList', 'amplify_lmt', 'SKU must be unique');
Where code is the field name, ID is the existing ID (you have to exclude the current record in unique checks), 'elements' is the filename, AmpElementList is the class, and amplify_lmt is the package (the last three are used to find, load, and instantiate the class). Within that class, I have a function like
public static function validateUnique($fieldName, $value, $exceptPK) { $list = new AmpElementList(); if ($fieldName == 'code') { $list->filter($fieldName, $value); } else { throw new Exception("Invalid field"); } if ($exceptPK != null) { $list->filter('ID', $exceptPK, '<>'); } return ($list->getTotal() == 0); }
which returns true if there are no other records with the same value.
This is called during the normal $vf->test(), but there's also have a tool file that calls this on command (see next bullet point):
<?php defined('C5_EXECUTE') or die("Access Denied."); header('Content-type: application/json'); list($pkg, $file, $model, $exceptPK) = explode(':', $_REQUEST['remKey']); Loader::model($file, $pkg); if (call_user_func("$model::validateUnique", $_REQUEST['fieldName'], $_REQUEST[$_REQUEST['fieldName']], $exceptPK)) { echo "true"; } else { echo "false"; } ?>
4. I've added jquery validator support. There's a method that outputs the relevant validation rules (not all are supported) in a array structure that can be passed to jquery validate. This includes the unique check (jquery calls it remote), which is what the tool is for (note that there's some hardcoding of the URL). It's run something like this:
Note that getModelValidation() returns my validation helper. I've included that method in all of my models which sets the validations and returns a validation helper object; it works out pretty well.
Have fun, and if you're having any difficulties, just ask.
(You'll have to remove the jpg extension.)
James