Having run into this issue myself, I created a Latin-9 compatible
validation function for alphanumeric data. Our system is not using
Unicode, so you need to be using Latin-9 (ISO-8859-15) for your
CakePHP installation and database. I assume the approach could be
adapted to UTF-8 quite easily.

I chose to use a function to generate the characters I need for the
regexp, as keeping the actual code ASCII makes it immune to encoding
issues. I decided to keep my "magic string" as a global constant, but
encapsulating it into the validation function would work as well.

=== Put in bootstrap.php -- STARTS ===

// Special non-ascii characters for encoding-proof usage.
global $LATIN_9_EXTCHARS;
$LATIN_9_EXTCHARS = array(
  166, // S with caron (hachek)
  168, // s with caron (hachek)
  180, // Z with caron (hachek)
  184, // z with caron (hachek)
  188, // Capital ligature OE
  189, // Small ligature oe
  190, // Y with diaeresis
  192, // A Grave
  193, // A Acute
  194, // A Circumflex
  195, // A Tilde
  196, // A Diaeresis
  197, // A Ring
  198, // AE Digraph
  199, // C Cedilla
  200, // E Grave
  201, // E Acute
  202, // E Circumflex
  203, // E Diaeresis
  204, // I Grave
  205, // I Acute
  206, // I Circumflex
  207, // I Diaeresis
  208, // Uppercase Eth
  209, // N Tilde
  210, // O Grave
  211, // O Acute
  212, // O Circumflex
  213, // O Tilde
  214, // O Diaeresis
  216, // O Slash
  217, // U Grave
  218, // U Acute
  219, // U Circumflex
  220, // U Diaeresis
  221, // Y Acute
  222, // Uppercase Thorn
  223, // German Double s
  224, // a Grave
  225, // a Acute
  226, // a Circumflex
  227, // a Tilde
  228, // a Diaeresis
  229, // a Ring
  230, // ae Digraph
  231, // c Cedilla
  232, // e Grave
  233, // e Acute
  234, // e Circumflex
  235, // e Diaeresis
  236, // i Grave
  237, // i Acute
  238, // i Circumflex
  239, // i Diaeresis
  240, // Lowercase Eth
  241, // n Tilde
  242, // o Grave
  243, // o Acute
  244, // o Circumflex
  245, // o Tilde
  246, // o Diaeresis
  248, // o Slash
  249, // u Grave
  250, // u Acute
  251, // u Circumflex
  252, // u Diaeresis
  253, // y Acute
  254, // Lowercase Thorn
  255, // y Diaeresis
);

// Function to turn array above into a string.
function _charcodes_to_string($codes) {
  $output = '';
  foreach($codes as $code) {
    $output .= chr($code);
  }
  return $output;
}

// Special chars character string for validation regexps.
global $LATIN_9_EXTCHARS_STRING;
$LATIN_9_EXTCHARS_STRING = _charcodes_to_string($LATIN_9_EXTCHARS);

=== Put in bootstrap.php -- ENDS ===


Then the actual validation function:

=== Put in app_model.php -- STARTS ===

  /**
   * Validator function for alphanumeric values with chars
   * from the Latin-9 charset.
   *
   * @param array $data The data containing the field to be validated
as passed by the framework.
   * @param boolean $digits Allow digits, true or false.
   * @param boolean $spaces Allow spaces, true or false.
   * @return boolean true if field validates, false if not.
   */
  function latin9AlphaNumeric($data, $digits, $spaces) {

    // $data should contain a single element, the field being
validated.
    $target = array_pop($data);

    // Compile the regexp.
    $regex = '/[^A-Z' . $GLOBALS['LATIN_9_EXTCHARS_STRING'];
    if ($digits === true) $regex .= '\\d';
    if ($spaces === true) $regex .= ' ';
    $regex .= ']/i';

    // If preg_match returns 0 no offending chars were found, return
true.
    // In any other case return false.
    return preg_match($regex, $target) == 0 ? true : false;

  }

=== Put in app_model.php -- ENDS ===


The validation function has parameters to allow/disallow digits and/or
spaces. I created wrappers (like latin9Alpha(), latin9AlphaNumeric()
etc.) for it to use in my validation definitions without extra
arguments, but it could be called straight as-is as well. Then you
just need to call it with both flags (digits, spaces) defined.
Otherwise CakePHP will pass some extra data as the second argument,
messing up the true/false evaluation for the flags. Refer to custom
validation functions with arguments in the 1.2 docs for details.

Hopefully this is useful to someone else out there! :)

  Regards,
Max Romantschuk
http://max.romantschuk.fi/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to