0

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;
}

2 Answers 2

1

Generates a Fatal Error on the 'rule' line: >Error: syntax error, unexpected T_VARIABLE, expecting ')'<.

php basics: you can't use a variable in a property declaration.

Add your rule to the validate property in the beforeValidate() callback.

Sign up to request clarification or add additional context in comments.

Comments

0

Use: define(, ); php-based page generation is generating hypertext on page request - no reason not to employ recursively (define template spaces in generating php-file, insert run-time constant before page request into template space).

Write standard value into generating php-file, so that there is a valid entry at all times: define("nameSpecialChars", '|'.'[\'-\s]'); Use regex or similar to replace value when needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.