Exemplo n.º 1
0
 /**
  * Form element validation handler for #type 'number'.
  *
  * Note that #required is validated by _form_validate() already.
  */
 public static function validateNumber(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $value = $element['#value'];
     if ($value === '') {
         return;
     }
     $name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
     // Ensure the input is numeric.
     if (!is_numeric($value)) {
         $form_state->setError($element, t('%name must be a number.', array('%name' => $name)));
         return;
     }
     // Ensure that the input is greater than the #min property, if set.
     if (isset($element['#min']) && $value < $element['#min']) {
         $form_state->setError($element, t('%name must be higher than or equal to %min.', array('%name' => $name, '%min' => $element['#min'])));
     }
     // Ensure that the input is less than the #max property, if set.
     if (isset($element['#max']) && $value > $element['#max']) {
         $form_state->setError($element, t('%name must be lower than or equal to %max.', array('%name' => $name, '%max' => $element['#max'])));
     }
     if (isset($element['#step']) && strtolower($element['#step']) != 'any') {
         // Check that the input is an allowed multiple of #step (offset by #min if
         // #min is set).
         $offset = isset($element['#min']) ? $element['#min'] : 0.0;
         if (!NumberUtility::validStep($value, $element['#step'], $offset)) {
             $form_state->setError($element, t('%name is not a valid number.', array('%name' => $name)));
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Tests Number::validStep() with offset.
  *
  * @dataProvider providerTestValidStepOffset
  * @covers ::validStep
  *
  * @param numeric $value
  *   The value argument for Number::validStep().
  * @param numeric $step
  *   The step argument for Number::validStep().
  * @param numeric $offset
  *   The offset argument for Number::validStep().
  * @param bool $expected
  *   Expected return value from Number::validStep().
  */
 public function testValidStepOffset($value, $step, $offset, $expected)
 {
     $return = Number::validStep($value, $step, $offset);
     $this->assertEquals($expected, $return);
 }