Last active
November 1, 2016 00:57
-
-
Save fitzagard/f184ffb9300e2c9ead1f855e7e4acc7f to your computer and use it in GitHub Desktop.
li3 Validation Example Using Multiple Form Inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example: Sometimes you need a bit more validation power on form inputs. For instance, a form validation | |
* may be dependpent on multiple inputs and advanced testing. | |
* | |
* The example below shows the power of $options array. That's where all your form inputs along with some other custom | |
* parts will be stored. The key to getting access to your form POST data is through `values` which lives in $options. | |
* The extract method pulls out the 'values' array providing access to the POST data. | |
* | |
* In this particular example were trying to check if the field input `acronym` is unique among a list of cities in MongoDB. | |
* | |
*/ | |
Validator::add('isUniqueAcronym', function($value, $format = null, array $options = array()) { | |
$options += array( | |
'values' => array() | |
); | |
extract($options); | |
$conditions['acronym'] = $value; | |
$conditions['cities'] = ['$in' => $values['cities']]; | |
$stuff = Stuff::count(compact('conditions')); | |
return ($stuff == 0) ? true : false; | |
}); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A side note: If your input has nested data (i.e. a multi-select dropdown) then your array keys will be in dot notation. Make use of the expand method in
\lithium\util\Set
to turn your dot-notation into a multi-dimensional array.