Exemplo n.º 1
0
Arquivo: Form.php Projeto: ksst/kf
 /**
  * Set Values to Form.
  *
  * An associative array is used to pre-populate form elements.
  * The keys of this array correspond with the element names.
  *
  * There are two use cases for this method:
  * 1) pre-filled form
  *    Some default values are set to the form, which then get altered by the user.
  * b) incomming post data
  *    Set the incomming POST data values are set to the form for validation.
  *
  * @param object|array $data Object or Array. If null (default), POST parameters are used.
  */
 public function setValues($data = null)
 {
     // because $data might be an object, typecast $data object to array
     if (is_object($data)) {
         $data = (array) $data;
     }
     if (null === $data) {
         // fetch data from POST
         if ('POST' === \Koch\Http\HttpRequest::getRequestMethod()) {
             $data = \Koch\Http\HttpRequest::getPost();
         }
     }
     // now we got an $data array to populate all the formelements with (setValue)
     foreach ($data as $key => $value) {
         foreach ($this->formelements as $formelement) {
             /*
              * Exclude some formelements from setValue() by type, e.g. Buttons, etc.
              * Setting the value would just change the visible "name" of these elements.
              */
             $type = $formelement->getType();
             if (true === in_array($type, ['submit', 'button', 'cancelbutton', 'resetbutton'], true)) {
                 continue;
             }
             // data[key] and formelement[name] have to match
             //if ($formelement->getName() == ucfirst($key)) {
             $formelement->setValue($value);
             //}
         }
     }
 }
Exemplo n.º 2
0
 public static function getParameters()
 {
     // transfer parameters from HttpRequest Object to TargetRoute
     if (HttpRequest::getRequestMethod() === 'POST') {
         $request = new HttpRequest();
         $params = $request->getPost();
         self::setParameters($params);
     }
     return self::$parameters['params'];
 }