Jump to:

5516 Posts in 1734 Topics by 1221 members

Customising the CMS

SilverStripe Forums » Customising the CMS » NumericField with specific input range validate

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1027 Views
  • IngmarHendriks
    Avatar
    Community Member
    1 Post

    NumericField with specific input range validate Link to this 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!

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: NumericField with specific input range validate Link to this post

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

    1027 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.