コード例 #1
0
ファイル: Form.php プロジェクト: loopsframework/base
 /**
  * Checks passed input for its validity
  *
  * The passed input array may be a flat array. This is an array of a single dimension that
  * can be expanded into an array of multiple dimension. See the documentation of Loops\Misc::unflattenArray
  * for details on how this is done. The used delimiter for $value is "-" (dash character);
  *
  * If the passed input array does not specify all keys of the form elements, the missing values will be substituted
  * by the NULL variable.
  * The value of the input will then be assigned to the form elements. No hard filtering is used, the value will stay
  * as close as possible to its input value even if it failes validation.
  *
  * Each form element will validate its input and only if all values could be validated successfully, the "Form\onConfirm"
  * event will be triggered with the form value as the first argument and the form as the second.
  * All triggered must return a positive value or the input will not be considered as successfully validated.
  *
  * If the input is valid, the hard filter will be applied on the value.
  * (See documentation of the form element class for details)
  * The form value will now be in an optimal state and its confirmed property set to true.
  *
  * If the input could not be validated, its content will still be reflected into the form value.
  * However submission of the form is not possible until confirmation succeeds.
  *
  * @param array $values
  * @return bool TRUE if the input could be validated and the form is in confirmed state.
  */
 public function confirm($values)
 {
     $this->confirmed = FALSE;
     $values = Misc::unflattenArray($values, "-");
     foreach ($this->getFormElements() as $name => $child) {
         $child->setValue(array_key_exists($name, $values) ? $values[$name] : NULL);
         $this->value->offsetSet($name, $child->getValue(FALSE));
     }
     if (!$this->validate()) {
         return FALSE;
     }
     if (!$this->fireEvent("Form\\onConfirm", [$this->value, $this], TRUE, FALSE)) {
         return FALSE;
     }
     $this->applyFilter();
     return $this->confirmed = TRUE;
 }
コード例 #2
0
ファイル: LoopsMiscTest.php プロジェクト: loopsframework/base
 public function testUnflattenArray()
 {
     $test['a'] = 1;
     $test['b']['c'] = 2;
     $test['b']['d'] = 3;
     $test['e']['f']['g'] = 4;
     $flat['a'] = 1;
     $flat['b.c'] = 2;
     $flat['b.d'] = 3;
     $flat['e.f.g'] = 4;
     $this->assertEquals($test, Misc::unflattenArray($flat));
     $flat2['a'] = 1;
     $flat2['b-c'] = 2;
     $flat2['b-d'] = 3;
     $flat2['e-f-g'] = 4;
     $this->assertEquals($test, Misc::unflattenArray($flat2, "-"));
 }