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

Override NumericField


Go to End


2 Posts   1654 Views

Avatar
rob.s

Community Member, 78 Posts

25 November 2011 at 11:12pm

Edited: 25/11/2011 11:39pm

Hi,

in germany it is usual to enter the comma as decimal separator.
When using db fields as type 'Float', the "NumericField" is used by scaffolded forms.
Here you get a validation error when typing "9,99" because "9.99" is expected.

I built a custom numeric field as 'AWNumericField':

class AWNumericField extends TextField{
	
	function Field() {
		$html = parent::Field();
		//Requirements::javascript(SAPPHIRE_DIR . 'javascript/NumericField.js');		
		return $html;
	}

	
	/** PHP Validation **/
	function validate($validator){
                $this->value = str_replace(',', '.', $this->value);
		if($this->value && !is_numeric(trim($this->value))){
 			$validator->validationError(
 				$this->name,
				sprintf(
					_t('AWNumericField.VALIDATION', "'%s' is not a number, only numbers can be accepted for this field"),
					$this->value
				),
				"validation"
			);
			return false;
		} else{
			return true;
		}
	}
	
       
        function Value() {
            return number_format($this->value, 2, ',', '.');
        }
        
}

And it works when i build forms using this field.
But i want sapphire to take this field by default (by scaffolding)

I thougt that

Object::useCustomClass('NumericField', 'AWNumericField');
//or
Object::useCustomClass('NumericField', 'AWNumericField', true);

would do the job.
It does not.

Now i tried to build an extension an override some methods:

class NumericFieldExtension extends Extension {
	function Field() {
		$html = parent::Field();
		//Requirements::javascript(SAPPHIRE_DIR . 'javascript/NumericField.js');		
		return $html;
	}
        
        function jsValidation() {
            return;
        }

	
	/** PHP Validation **/
	function validate($validator){
                $this->value = str_replace(',', '.', $this->value);
		if($this->value && !is_numeric(trim($this->value))){
 			$validator->validationError(
 				$this->name,
				sprintf(
					_t('AWNumericField.VALIDATION', "'%s' is not a number, only numbers can be accepted for this field"),
					$this->value
				),
				"validation"
			);
			return false;
		} else{
			return true;
		}
	}
	
       
        function Value() {
            return number_format($this->value, 2, ',', '.');
        }    
}

and in _config.php

Object::add_extension('NumericField', 'NumericFieldExtension');

Still doesn't work .....

Any ideas or suggestions ?
Rob

Avatar
martimiz

Forum Moderator, 1391 Posts

27 November 2011 at 5:38am

I don't think in an extension or decorator overriding existing owner functions is possible, you can extend owner functions that have an extension hook defined, or create new funtionality...