setClose(false); /** 设置表单属性 */ $this->setAction($action); $this->setMethod($method); $this->setEncodeType($enctype); } /** * 设置表单编码方案 * * @access public * @param string $enctype 编码方法 * @return Typecho_Widget_Helper_Form */ public function setEncodeType($enctype) { $this->setAttribute('enctype', $enctype); return $this; } /** * 增加输入元素 * * @access public * @param Typecho_Widget_Helper_Form_Element $input 输入元素 * @return Typecho_Widget_Helper_Form */ public function addInput(Typecho_Widget_Helper_Form_Element $input) { $this->_inputs[$input->name] = $input; $this->addItem($input); return $this; } /** * 获取输入项 * * @access public * @param string $name 输入项名称 * @return mixed */ public function getInput($name) { return $this->_inputs[$name]; } /** * 获取所有输入项的提交值 * * @access public * @return array */ public function getAllRequest() { $result = array(); $source = (self::POST_METHOD == $this->getAttribute('method')) ? $_POST : $_GET; foreach ($this->_inputs as $name => $input) { $result[$name] = isset($source[$name]) ? $source[$name] : NULL; } return $result; } /** * 设置表单提交方法 * * @access public * @param string $method 表单提交方法 * @return Typecho_Widget_Helper_Form */ public function setMethod($method) { $this->setAttribute('method', $method); return $this; } /** * 设置表单提交目的 * * @access public * @param string $action 表单提交目的 * @return Typecho_Widget_Helper_Form */ public function setAction($action) { $this->setAttribute('action', $action); return $this; } /** * 获取此表单的所有输入项固有值 * * @access public * @return array */ public function getValues() { $values = array(); foreach ($this->_inputs as $name => $input) { $values[$name] = $input->value; } return $values; } /** * 获取此表单的所有输入项 * * @access public * @return array */ public function getInputs() { return $this->_inputs; } /** * 获取提交数据源 * * @access public * @param array $params 数据参数集 * @return array */ public function getParams(array $params) { $result = array(); $source = (self::POST_METHOD == $this->getAttribute('method')) ? $_POST : $_GET; foreach ($params as $param) { $result[$param] = isset($source[$param]) ? $source[$param] : NULL; } return $result; } /** * 验证表单 * * @access public * @return mixed */ public function validate() { $validator = new Typecho_Validate(); $rules = array(); foreach ($this->_inputs as $name => $input) { $rules[$name] = $input->rules; } $id = md5(implode('"', array_keys($this->_inputs))); /** 表单值 */ $formData = $this->getParams(array_keys($rules)); $error = $validator->run($formData, $rules); if ($error) { /** 利用session记录错误 */ Typecho_Cookie::set('__typecho_form_message_' . $id, Json::encode($error)); /** 利用session记录表单值 */ Typecho_Cookie::set('__typecho_form_record_' . $id, Json::encode($formData)); } return $error; } /** * 显示表单 * * @access public * @return void */ public function render() { $id = md5(implode('"', array_keys($this->_inputs))); $record = Typecho_Cookie::get('__typecho_form_record_' . $id); $message = Typecho_Cookie::get('__typecho_form_message_' . $id); /** 恢复表单值 */ if (!empty($record)) { $record = Json::decode($record, true); $message = Json::decode($message, true); foreach ($this->_inputs as $name => $input) { $input->value(isset($record[$name]) ? $record[$name] : $input->value); /** 显示错误消息 */ if (isset($message[$name])) { $input->message($message[$name]); } } Typecho_Cookie::delete('__typecho_form_record_' . $id); } parent::render(); Typecho_Cookie::delete('__typecho_form_message_' . $id); } }