Swoole\Form::autoform PHP Method

autoform() static public method

根据数组,生成表单
static public autoform ( $form_array ) : unknown_type
$form_array
return unknown_type
    static function autoform($form_array)
    {
        $forms = array();
        foreach ($form_array as $k => $v) {
            //表单类型
            $func = $v['type'];
            //表单值
            $value = '';
            if (isset($v['value'])) {
                $value = $v['value'];
            }
            unset($v['type'], $v['value']);
            if ($func == 'input' or $func == 'password' or $func == 'text' or $func == 'htmltext') {
                $forms[$k] = self::$func($k, $value, $v);
            } else {
                $option = $v['option'];
                $self = $v['self'];
                $label_class = $v['label_class'];
                unset($v['option'], $v['self'], $v['label_class']);
                $forms[$k] = self::$func($k, $option, $value, $self, $v, $label_class);
                if ($func == 'radio' and isset($v['empty'])) {
                    $forms[$k] .= "\n<script language='javascript'>add_filter('{$k}','{$v['empty']}',function(){return getRadioValue('{$k}');});</script>";
                } elseif ($func == 'checkbox' and isset($v['empty'])) {
                    $forms[$k] .= "\n<script language='javascript'>add_filter('{$k}[]','{$v['empty']}',function(){return getCheckboxValue('{$k}[]');});</script>";
                }
            }
        }
        return $forms;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * 自动生成表单
  *
  * @param $set_id
  *
  * @return unknown_type
  */
 function getForm($set_id = 0)
 {
     $this->_form_();
     //传入ID,修改表单
     if ($set_id) {
         $data = $this->get((int) $set_id)->get();
         foreach ($this->_form as $k => &$f) {
             $f['value'] = $data[$k];
         }
         if (method_exists($this, "_set_")) {
             $this->_set_();
         }
         if ($this->_form_secret) {
             Form::secret(get_class($this) . '_set');
         }
     } elseif (method_exists($this, "_add_")) {
         $this->_add_();
         if ($this->_form_secret) {
             Form::secret(get_class($this) . '_add');
         }
     }
     return Form::autoform($this->_form);
 }