Version: current

     1 <?php
     2 /**
     3  * EditableNumericField
     4  *
     5  * Allow users to define a validating editable numeric field for a UserDefinedForm
     6  *
     7  * @package userforms
     8  */
     9
    10 class EditableNumericField extends EditableFormField {
    11
    12     static $singular_name = 'Numeric Field';
    13
    14     static $plural_name = 'Numeric Fields';
    15
    16     function getFieldConfiguration() {
    17         $fields = parent::getFieldConfiguration();
    18
    19         $min = ($this->getSetting('MinLength')) ? $this->getSetting('MinLength') : '';
    20         $max = ($this->getSetting('MaxLength')) ? $this->getSetting('MaxLength') : '';
    21         $step = ($this->getSetting('Step')) ? $this->getSetting('Step') : '';
    22         //$pattern = ($this->getSetting('Pattern')) ? $this->getSetting('Pattern') : '';
    23
    24         $extraFields = new FieldSet(
    25             new FieldGroup(_t('EditableNumericField.MINMAXVALUES', 'Min and Max values'),
    26                 new TextField($this->getSettingName('MinLength'), "", $min),
    27                 new TextField($this->getSettingName('MaxLength'), " - ", $max)
    28             ),
    29             new TextField($this->getSettingName('Step'), _t('EditableNumericField.STEP', 'Step'), $step)
    30             //new TextField($this->getSettingName('Pattern'), _t('EditableNumericField.PATTERN', 'Pattern'), $pattern)
    31         );
    32
    33         $fields->merge($extraFields);
    34
    35         return $fields;
    36     }
    37
    38     function getFormField() {
    39         $fields = new NumericField($this->Name, $this->Title);
    40         if($this->getSetting('MinLength')) {
    41             $fields->setHTML5Attribute('min', $this->getSetting('MinLength'));
    42         }
    43         if($this->getSetting('MaxLength')) {
    44             $fields->setHTML5Attribute('max', $this->getSetting('MaxLength'));
    45         }
    46         $step = 'any';
    47         if($this->getSetting('Step')) {
    48             $step = $this->getSetting('Step');
    49         }
    50         $fields->setHTML5Attribute('step', $step);
    51         if($this->getSetting('Pattern')) {
    52             $fields->setHTML5Attribute('pattern', $this->getSetting('Pattern'));
    53         }
    54         return $fields;
    55     }
    56
    57     /**
    58      * Return the validation information related to this field. This is
    59      * interrupted as a JSON object for validate plugin and used in the
    60      * PHP.
    61      *
    62      * @see http://docs.jquery.com/Plugins/Validation/Methods
    63      * @return Array
    64      */
    65     public function getValidation() {
    66
    67         return array_merge(
    68             parent::getValidation(),
    69             array(
    70                 'number' => true
    71             )
    72         );
    73     }
    74 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -