Advertisement
heuert

Custom KeywordValidator - SilverStripe

Jan 29th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. <?php
  2. class KeywordValidator extends Validator {
  3.     public function php($data) {
  4.         $keywords = preg_replace('/^[^_A-z- ]+$/', '', $data['Keywords']);
  5.         $tags = explode(' ', $keywords);
  6.         $fail = false;
  7.         if (count($tags) < 3) {
  8.             $fail = true;
  9.         }
  10.         if (!$fail) {
  11.             foreach ($tags as $tag) {
  12.                 if (strlen($tag) === 0) {
  13.                     $fail = true;
  14.                 }
  15.             }
  16.         }
  17.         if ($fail) {
  18.             $this->validationError('Keywords', 'There must be at least 3 valid keywords');
  19.             return false;
  20.         }
  21.         return true;
  22.     }
  23.  
  24.     public function javascript() {
  25.         //echo "alert('somethingspecial');";
  26.         return false;
  27.     }
  28. }
  29.  
  30. class MyPage extends Page {
  31.     public function getCMSValidator() {
  32.         $validator = new KeywordValidator();
  33.         return $validator;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement