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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

NumericField with specific input range validate


Go to End


2 Posts   1687 Views

Avatar
IngmarHendriks

Community Member, 1 Post

10 April 2011 at 10:22am

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

Forum Moderator, 1899 Posts

11 April 2011 at 11:03pm

Edited: 11/04/2011 11:03pm

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