Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

NumericField with specific input range validate


Reply

2 Posts   1047 Views

Avatar
IngmarHendriks

10 April 2011 at 10:22am Community Member, 1 Post

I have a NumericField that represents the amount of items in a video page.
In the CMS i have a NumericField that may only contain a number between 1 and infinity. How can i make a validator on the NumericField so that is doesn't allow negative numbers or a zero?

Any help will be welcome!

Avatar
swaiba

11 April 2011 at 11:03pm (Last edited: 11 April 2011 11:03pm), Forum Moderator, 1796 Posts

note this is code I use in ModelAdmin, so it may require changing to work for a Page...

in the MyPage.php

...
   function getCMSValidator() {
      return new MyValidator();
   }
...
}

...

class MyValidator extends RequiredFields {
   function php($data) {
      $bRet = parent::php($data);

      $i = (int)$data['ItemsInAVideoPage'];
      if (!ctype_digit($strValue) || $i < 1) {
         $this->validationError('ItemsInAVideoPage','Items In A Video Page must be a non-zero integer',"required");
         $bRet = false;
      }

      return $bRet;
   }
}