Esempio n. 1
0
 /**
  * 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) === true) {
         $data = (array) $data;
     } elseif (null === $data) {
         if ('POST' === Koch_HttpRequest::getRequestMethod()) {
             $data = Koch_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) {
             $type = $formelement->getType();
             /**
              * Exclude some formelements from setValue(), e.g. buttons, etc.
              * Setting the value would just change the visible "name" of these elements.
              */
             if (true === in_array($type, array('submit', 'button', 'cancelbutton', 'resetbutton'))) {
                 continue;
             }
             // data[key] and formelement[name] have to match
             if ($formelement->getName() == ucfirst($key)) {
                 $formelement->setValue($value);
             }
         }
     }
 }