Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Override NumericField

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

Page: 1
Go to End
Author Topic: 864 Views
  • rob.s
    Avatar
    Community Member
    78 Posts

    Override NumericField Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Override NumericField Link to this post

    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...

    864 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.