Beispiel #1
0
 /**
  * 从配置载入表单设置和元素
  *
  * @code php
  * $form = QForm();
  * $config = Helper_YAML::loadCached('form_config.yaml');
  * $form->loadFromConfig($config);
  * @endcode
  *
  * @param array $config 包含表单配置的数组
  *
  * @return QForm 返回表单对象本身,实现连贯接口
  */
 function loadFromConfig(array $config)
 {
     if (isset($config['~form'])) {
         foreach ((array) $config['~form'] as $attr => $value) {
             $this->_attrs[$attr] = $value;
         }
     }
     unset($config['~form']);
     parent::loadFromConfig($config);
     return $this;
 }
Beispiel #2
0
 /**
  * 从配置批量添加元素
  *
  * 具体用法参考开发者手册关于表单的章节。
  *
  * @param array $config
  *
  * @return QForm_Group
  */
 function loadFromConfig(array $config)
 {
     foreach ($config as $id => $attrs) {
         if (!is_array($attrs)) {
             $attrs = array();
         }
         if (!isset($this->_attrs['qform_group_id'])) {
             $this->_attrs['qform_group_id'] = "";
         }
         if (isset($attrs['_elements'])) {
             if (!isset($attrs['qform_group_id'])) {
                 $attrs['qform_group_id'] = $id;
             }
             $elements = (array) $attrs['_elements'];
             unset($attrs['_elements']);
             $group = new QForm_Group($id, $attrs, $this);
             if (!empty($elements)) {
                 $group->loadFromConfig($elements);
             }
             $this->_elements[$id] = $group;
         } else {
             if (isset($attrs['_filters'])) {
                 $filters = $attrs['_filters'];
                 unset($attrs['_filters']);
             } else {
                 $filters = null;
             }
             if (isset($attrs['_validations'])) {
                 $validations = $attrs['_validations'];
                 unset($attrs['_validations']);
             } else {
                 $validations = null;
             }
             $attrs['qform_group_id'] = $this->_attrs['qform_group_id'];
             $element = new QForm_Element($id, $attrs, $this);
             if (!empty($filters)) {
                 $element->addFilters($filters);
             }
             if (!empty($validations)) {
                 $element->addValidations($validations);
             }
             if (isset($attrs['value'])) {
                 $element->_unfiltered_value = $attrs['value'];
             }
             $this->_elements[$id] = $element;
         }
     }
     return $this;
 }