[TYPO3-ect] validator heresy
Steve Ryan
stever at syntithenai.com
Wed Aug 15 16:00:01 CEST 2007
I'm having a go at generation of validator rules from TCA.
I'm looking at using something like
10 {
field = title
pattern = /.+/
message = %%%emptyTitleError%%%
}
20 {
field = text
php = if (substr($value)) $return=true;
message = %%%incorrectTextError%%%
}
to achieve some of the rules that can't be accomplished by preg like unique.
Doesn't smell good but it's convenient ????? Don't imagine that hacking
php snippets into otherwise such well structured codes is not going to
be popular. The value is being able to structure a much more flexible
rule set into an object.
Extension class to validator below
Use it as follows
$rules=$this->getValidationRulesFromTCA('fe_users','name,email,address');
$rules['50']['field']='name';
$rules['50']['php']='if (strlen($value)<2) $return=true; ';
$rules['50']['message']='too few characters in your name';
$validator->setRules($rules);
$validator->validate();
---------------------------
class tx_markablequiz_validator extends tx_lib_validator {
private $rules;
/**
* Set the namespace of rules to be used for validation.
*
* @param string validation rules namespace
* @return void
*/
function useRules($path = 'validationRules.') {
$this->pathToRules = $path;
$this->rules=$this->controller->configurations->get($this->pathToRules);
}
function setRules($rules) {
$this->rules=$rules;
}
function getRules() {
return $this->rules;
}
function validate($object = null) {
if(is_object($object)) {
$this->setArray($object);
}
if(is_array($this->rules)) {
$this->_validateByPREGAndPHPRules();
}
$this->set('_errorList', $this->errors);
$this->set('_errorCount', count($this->errors));
}
function _validateByPREGAndPHPRules() {
//debug(array('rules',$this->rules));
foreach($this->rules as $rule) {
// assume variables $value and $return
if ($rule['php']) {
$value=$this->get($rule['field']);
$return=false;
eval($rule['php']);
//echo $rule['php'];
if ($return) {
echo 'fail php test '.$rule['message'];
$this->errors[] = array_merge(array('.type' => 'rule'), $rule);
}
} else if($rule['pattern']&&!preg_match($rule['pattern'],
$this->get($rule['field']))) {
}
}
}
}
More information about the TYPO3-team-extension-coordination
mailing list