that ensures that every field listed in the 'fieldList' during save
(whitelist) has all of its rules set to 'required'... and it will also
complain if the 'fieldList' isn't provided to the save() method.
<?php
class AppModel extends Model {
function beforeValidate() {
//--------------------------------------------------
// Set all fields in the whitelist to be required
foreach ($this->whitelist as $cField) {
if (!isset($this->validate[$cField])) {
//--------------------------------------------------
// There is no validation for this field
continue;
} else if (is_string($this->validate[$cField])) {
//--------------------------------------------------
// Simple string validation:
//
// var $validate = array(
// 'field' => 'notempty'
// );
//
//--------------------------------------------------
$rule = $this->validate[$cField];
$this->validate[$cField] = array(
$rule => array(
'rule' => $rule,
'required' => true,
),
);
} else if (isset($this->validate[$cField]['rule'])) {
//--------------------------------------------------
// Array for a single rule validation:
//
// var $validate = array(
// 'notEmpty' => array(
// 'rule' => 'notEmpty',
// 'message' => 'Your email address is required.',
// ),
// );
//
//--------------------------------------------------
$this->validate[$cField]['required'] = true;
} else if (is_array($this->validate[$cField])) {
foreach ($this->validate[$cField] as $cRuleName => $cRuleValue) {
if (is_array($cRuleValue) && isset($cRuleValue['rule'])) {
//--------------------------------------------------
// Array of rules validation:
//
// var $validate = array(
// 'email' => array(
// 'notEmpty' => array(
// 'rule' => 'notEmpty',
// 'message' => 'Your email address is
required.',
// ),
// ),
// );
//
//--------------------------------------------------
$this->validate[$cField][$cRuleName]['required'] = true;
} else if (is_string($cRuleValue)) {
//--------------------------------------------------
// Simple array validation:
//
// var $validate = array(
// 'field' => array('notempty')
// );
//
//--------------------------------------------------
$this->validate[$cField][$cRuleName] = array(
'rule' => $cRuleValue,
'required' => true,
);
}
}
}
}
//--------------------------------------------------
// Valid
return true;
}
function beforeSave() {
//--------------------------------------------------
// Ensure the whitelist exists on save
if (!isset($this->whitelist) || !is_array($this->whitelist) || count
($this->whitelist) == 0) {
exit('Cannot submit this form without a whiteList/fieldList');
}
//--------------------------------------------------
// Valid
return true;
}
}
?>
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment