5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI
15 * TForm displays an HTML form. Besides regular body content,
16 * it displays hidden fields, javascript blocks and files that are registered
17 * through {@link TClientScriptManager}.
19 * A TForm is required for a page that needs postback.
20 * Each page can contain at most one TForm. If multiple HTML forms are needed,
21 * please use regular HTML form tags for those forms that post to different
24 * @author Qiang Xue <qiang.xue@gmail.com>
25 * @package System.Web.UI
28 class TForm extends TControl
31 * Registers the form with the page.
32 * @param mixed event parameter
34 public function onInit($param)
36 parent::onInit($param);
37 $this->getPage()->setForm($this);
41 * Adds form specific attributes to renderer.
42 * @param THtmlWriter writer
44 protected function addAttributesToRender($writer)
46 $writer->addAttribute('id',$this->getClientID());
47 $writer->addAttribute('method',$this->getMethod());
48 $uri=$this->getRequest()->getRequestURI();
49 $writer->addAttribute('action',str_replace('&','&',str_replace('&','&',$uri)));
50 if(($enctype=$this->getEnctype())!=='')
51 $writer->addAttribute('enctype',$enctype);
53 $attributes=$this->getAttributes();
54 $attributes->remove('action');
55 $writer->addAttributes($attributes);
57 if(($butt=$this->getDefaultButton())!=='')
59 if(($button=$this->findControl($butt))!==null)
60 $this->getPage()->getClientScript()->registerDefaultButton($this, $button);
62 throw new TInvalidDataValueException('form_defaultbutton_invalid',$butt);
68 * @param THtmlWriter writer
70 public function render($writer)
72 $page=$this->getPage();
74 $this->addAttributesToRender($writer);
75 $writer->renderBeginTag('form');
77 $cs=$page->getClientScript();
78 if($page->getClientSupportsJavaScript())
80 $cs->renderHiddenFieldsBegin($writer);
81 $cs->renderScriptFilesBegin($writer);
82 $cs->renderBeginScripts($writer);
84 $page->beginFormRender($writer);
85 $this->renderChildren($writer);
86 $cs->renderHiddenFieldsEnd($writer);
87 $page->endFormRender($writer);
89 $cs->renderScriptFilesEnd($writer);
90 $cs->renderEndScripts($writer);
94 $cs->renderHiddenFieldsBegin($writer);
96 $page->beginFormRender($writer);
97 $this->renderChildren($writer);
98 $page->endFormRender($writer);
100 $cs->renderHiddenFieldsEnd($writer);
103 $writer->renderEndTag();
107 * @return string id path to the default button control.
109 public function getDefaultButton()
111 return $this->getViewState('DefaultButton','');
115 * Sets a button to be default one in a form.
116 * A default button will be clicked if a user presses 'Enter' key within
118 * @param string id path to the default button control.
120 public function setDefaultButton($value)
122 $this->setViewState('DefaultButton',$value,'');
126 * @return string form submission method. Defaults to 'post'.
128 public function getMethod()
130 return $this->getViewState('Method','post');
134 * @param string form submission method. Valid values include 'post' and 'get'.
136 public function setMethod($value)
138 $this->setViewState('Method',TPropertyValue::ensureEnum($value,'post','get'),'post');
142 * @return string the encoding type a browser uses to post data back to the server
144 public function getEnctype()
146 return $this->getViewState('Enctype','');
150 * @param string the encoding type a browser uses to post data back to the server.
151 * Commonly used types include
152 * - application/x-www-form-urlencoded : Form data is encoded as name/value pairs. This is the standard encoding format.
153 * - multipart/form-data : Form data is encoded as a message with a separate part for each control on the page.
154 * - text/plain : Form data is encoded in plain text, without any control or formatting characters.
156 public function setEnctype($value)
158 $this->setViewState('Enctype',$value,'');
162 * @return string form name, which is equal to {@link getUniqueID UniqueID}.
164 public function getName()
166 return $this->getUniqueID();