I currently use cakePHP 2.4.5 on PHP 5.3.27, both may change if necessary (but it would be better if not).
My custom validation rules in a cakephp model require external data to work, such as:
public $nameSpecialChars = '\'- ';
public $dotWhitespace = '. ';
public $timeLabels = array('Jahr', 'month', 'jour', 'minuta');
public $tlSeparator = ', ';
These would ideally be defined elsewhere, but at the top of the model file is good enough for now.
The idea is that they may change later. >nameSpecialChars< is for example used to define special characters which may appear in person names; synonyms of >'< may be included, >ยด<, >`<, etc. .
However, defining rules which use them, does not seem to work:
public $validate = array(
'street' => array(
'required' =>true ,
'allowEmpty'=>false,
'rule' =>array('isName', $nameSpecialChars)
)
);
Generates a Fatal Error on the 'rule' line: >Error: syntax error, unexpected T_VARIABLE, expecting ')'<.
I have found that passing another field as parameter may be done by including a string with the field name, and the documentation demonstrates passing a constant by writing it out. - The latter would imply that passing a string to a string parameter will interpret it as constant, not field name... or would it?
How am I supposed to pass these variables?
Alternatively: Is there a better way to achieve externalisation of these elements?
As a reference, here is the function to the cited custom rule (note that it is of placeholder character):
// Names must contain only letters and a couple of special characters.
// note on UTF8 handling pre-php6: https://stackoverflow.com/a/16862181/3434465
// whole thing should be reworked once proper UTF8 support is available
public function isName($check, string $allowedSpecialChars)
{
setlocale(LC_CTYPE, 'utf8'); // to ensure correct ctype_alpha evaluation;
// may change arbitrarily during server operation, thence always ensure it be set
if(mb_substr($allowedSpecialChars, -1) != 'u')
{
$allowedSpecialChars = $allowedSpecialChars . 'u'; // ensure that UTF8-flag is set
}
$name = array_values($check)[0]; // reduce to input string; bit of a hack, better replace once better way found
$charArray = preg_split("//u", $name, -1, PREG_SPLIT_NO_EMPTY); // theoretically UTF8-aware split
bool validCharacters = true;
foreach($charArray as $char) // not quite UTF8 compatible, I fear
{
validCharacters = validCharacters && (
ctype_alpha($char) // UTF8-aware due to locale
||
preg_match($allowedSpecialChars, $char, null) // UTF8-aware due to terminating 'u'-flag
);
}
return validCharacters;
}