Welcome to the Cookbook

loading...

4.1.5 Benutzer Validierung Regeln

If you haven’t found what you need thus far, you can always create your own validation rules. There are two ways you can do this: by defining custom regular expressions, or by creating custom validation methods.

4.1.5.1 Benutzerdefinierte Validierung mit Regulären Ausdrücken

Der Originaltext dieses Abschnitts hat sich seit der letzen Übersetzung geändert. Bitte hilf dabei die Unterschiede zu lösen. Du kannst:

Mehr Informationen zu Übersetzungen

Falls Sie eine Validierungsmethode benötigen, die Sie mit Regulären Ausdrücken erreichen können/wollen, können Sie auch eigene benutzerdefinierte Reguläre Ausdrücke als Validierungsregel für die Daten des zu validierenden Feldes definieren.

var $validate = array(
    'login' => array(
        'rule' => array('custom', '/^[a-z0-9]{3,}$/i'),  
        'message' => 'Nur Buchstaben und Zahlen, mindestens 3 Zeichen'
    )
);
  1. var $validate = array(
  2. 'login' => array(
  3. 'rule' => array('custom', '/^[a-z0-9]{3,}$/i'),
  4. 'message' => 'Nur Buchstaben und Zahlen, mindestens 3 Zeichen'
  5. )
  6. );

Das Beispiel hier überprüft, ob die Daten des login Feldes nur Buchstaben und Zahlen mit einer Mindestlänge von 3 Zeichen enthalten.

4.1.5.2 Adding your own Validation Methods

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

Sometimes checking data with regular expression patterns is not enough. For example, if you want to ensure that a promotional code can only be used 25 times, you need to add your own validation function, as shown below:

<?php
class User extends AppModel {
    var $name = 'User';
  
    var $validate = array(
        'promotion_code' => array(
            'rule' => array('limitDuplicates', 25),
            'message' => 'This code has been used too many times.'
        )
    );
 
    function limitDuplicates($check, $limit){
        //$check will have value: array('promomotion_code' => 'some-value')
        //$limit will have value: 25
        $existing_promo_count = $this->find( 'count', array('conditions' => $check, 'recursive' => -1) );
        return $existing_promo_count < $limit;
    }
}
?>
  1. <?php
  2. class User extends AppModel {
  3. var $name = 'User';
  4. var $validate = array(
  5. 'promotion_code' => array(
  6. 'rule' => array('limitDuplicates', 25),
  7. 'message' => 'This code has been used too many times.'
  8. )
  9. );
  10. function limitDuplicates($check, $limit){
  11. //$check will have value: array('promomotion_code' => 'some-value')
  12. //$limit will have value: 25
  13. $existing_promo_count = $this->find( 'count', array('conditions' => $check, 'recursive' => -1) );
  14. return $existing_promo_count < $limit;
  15. }
  16. }
  17. ?>

The current field to be validated is passed into the function as first parameter as an associated array with field name as key and posted data as value.

If you want to pass extra parameters to your validation function, add elements onto the ‘rule’ array, and handle them as extra params (after the main $check param) in your function.

Your validation function can be in the model (as in the example above), or in a behavior that the model implements. This includes mapped methods.

Model/behavior methods are checked first, before looking for a method on the Validation class. This means that you can override existing validation methods (such as alphaNumeric()) at an application level (by adding the method to AppModel), or at model level.

When writing a validation rule which can be used by multiple fields, take care to extract the field value from the $check array. The $check array is passed with the form field name as its key and the field value as its value. The full record being validated is stored in $this->data member variable.

<?php
class Post extends AppModel {
  var $name = 'Post';
  
  var $validate = array(
    'slug' => array(
      'rule' => 'alphaNumericDashUnderscore',
      'message' => 'Slug can only be letters, numbers, dash and underscore'
      )
    );
    
    function alphaNumericDashUnderscore($check) {
      // $data array is passed using the form field name as the key
      // have to extract the value to make the function generic
      $value = array_values($check);
      $value = $value[0];
      
      return preg_match('|^[0-9a-zA-Z_-]*$|', $value);
    }
}
?>
  1. <?php
  2. class Post extends AppModel {
  3. var $name = 'Post';
  4. var $validate = array(
  5. 'slug' => array(
  6. 'rule' => 'alphaNumericDashUnderscore',
  7. 'message' => 'Slug can only be letters, numbers, dash and underscore'
  8. )
  9. );
  10. function alphaNumericDashUnderscore($check) {
  11. // $data array is passed using the form field name as the key
  12. // have to extract the value to make the function generic
  13. $value = array_values($check);
  14. $value = $value[0];
  15. return preg_match('|^[0-9a-zA-Z_-]*$|', $value);
  16. }
  17. }
  18. ?>