Skip to content

Instantly share code, notes, and snippets.

@am-doherty
Last active December 11, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am-doherty/4673875 to your computer and use it in GitHub Desktop.
Save am-doherty/4673875 to your computer and use it in GitHub Desktop.
flightCheckFail - a pre-validation example for Propel (v 1.6.4) fields. Overcomes a limitation with (some) validators that require correct data in the object before validation can take place
/* flightCheckFail - a pre-validation example for Propel (v 1.6.4) fields
*
* Overcomes a limitation with (some) validators that require correct data in the object before validation can take place.
* Example below uses date field
*
* A.M. Doherty 22nd March 2012
* read more here: http://stackoverflow.com/questions/9570570/propel-orm-version-1-6-4-understanding-validators
* and here: http://codeandeffect.co.uk/blog/2012/programming/php/flight-checking-values-for-propel/
*/
/* Sample usage:
if ($_GET['starting_date'] ):
$event = new Event();
$sanitisedFormVal = sanitise($_GET['starting_date']);
if ($check = $event->flightCheckFail('StartingDate','DateValidator',$sanitisedFormVal)) {echo $check;}
else { $event->setStartingDate($sanitisedFormVal);}
*/
/*Drop into Event class*/
class Event extends BaseEvent {
public function flightCheckFail($name,$validatorClass,$value){
$colname = $this->getPeer()->getTableMap()->getColumnByPhpName($name)->getName();
$validators = $this->getPeer()->getTableMap()->getColumn($colname)>getValidators();
foreach($validators as $validatorMap) {
if ($validatorMap->getClass()==$validatorClass) {
$validator = BasePeer::getValidator($validatorMap>getClass());
if ( $validator->isValid($validatorMap, $value) === false) {
$failureMessage = $validatorMap->getMessage();
}
}
}
if($failureMessage) {return $failureMessage;}
else {return false;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment