phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
HtmlForm.php
Go to the documentation of this file.
1 <?php
2 
4 
11 use Ajax\JsUtils;
17 
24 class HtmlForm extends HtmlSemCollection {
25 
30  protected $_fields;
31 
35  protected $_validationParams;
36 
37  protected $_extraFieldRules;
38 
39  public function __construct($identifier, $elements=array()) {
40  parent::__construct($identifier, "form", "ui form");
42  $this->setProperty("name", $this->identifier);
43  $this->_fields=array ();
44  $this->addItems($elements);
45  $this->_validationParams=[];
46  $this->_extraFieldRules=[];
47  }
48 
49  protected function getForm(){
50  return $this;
51  }
52 
59  public function addHeader($title, $niveau=1, $dividing=true) {
60  $header=new HtmlHeader("", $niveau, $title);
61  if ($dividing)
62  $header->setDividing();
63  return $this->addItem($header);
64  }
65 
71  public function addDivider($caption=NULL){
72  return $this->addContent(new HtmlDivider("",$caption));
73  }
74 
81  public function addFields($fields=NULL, $label=NULL) {
82  if (isset($fields)) {
83  if (!$fields instanceof HtmlFormFields) {
84  if (!\is_array($fields)) {
85  $fields=\func_get_args();
86  $end=\end($fields);
87  if (\is_string($end)) {
88  $label=$end;
89  \array_pop($fields);
90  } else
91  $label=NULL;
92  }
93  $this->_fields=\array_merge($this->_fields, $fields);
94  $fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields);
95  }
96  if (isset($label)){
97  $fields->wrap("<div class='field'><label>{$label}</label>","</div>");
98  }
99  } else {
100  $fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count());
101  }
102  $this->addItem($fields);
103  return $fields;
104  }
105 
106  public function addItem($item) {
107  $item=parent::addItem($item);
108  if (\is_subclass_of($item, HtmlFormField::class) === true) {
109  $this->_fields[]=$item;
110  }
111  return $item;
112  }
113 
118  public function getField($index) {
119  if (\is_string($index)) {
120  $field=$this->getElementById($index, $this->_fields);
121  } else {
122  $field=$this->_fields[$index];
123  }
124  return $field;
125  }
126 
131  public function setEqualWidth() {
132  return $this->addToProperty("class", "equal width");
133  }
134 
140  public function addField($field) {
141  return $this->addItem($field);
142  }
143 
153  public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) {
154  $message=new HtmlMessage($identifier, $content);
155  if (isset($header))
156  $message->addHeader($header);
157  if (isset($icon))
158  $message->setIcon($icon);
159  if (isset($type))
160  $message->setStyle($type);
161  return $this->addItem($message);
162  }
163 
164 
165 
166  public function compile(JsUtils $js=NULL,&$view=NULL){
167  if(\sizeof($this->_validationParams)>0)
168  $this->setProperty("novalidate", "");
169  return parent::compile($js,$view);
170  }
171 
172  public function run(JsUtils $js) {
173  if(isset($js)){
174  $compo=$js->semantic()->form("#".$this->identifier);
175  }else{
176  $compo=new Form();
177  $compo->attach("#".$this->identifier);
178  }
179  foreach ($this->_fields as $field){
180  if($field instanceof HtmlFormField){
181  $this->addCompoValidation($compo, $field);
182  }
183  }
184  foreach ($this->content as $field){
185  if($field instanceof HtmlFormFields){
186  $items=$field->getItems();
187  foreach ($items as $_field){
188  if($_field instanceof HtmlFormField)
189  $this->addCompoValidation($compo, $_field);
190  }
191  }
192  }
193  foreach ($this->_extraFieldRules as $field=>$fieldValidation){
194  $this->addExtraCompoValidation($compo, $fieldValidation);
195  }
196  $this->_runValidationParams($compo,$js);
197  return $this->_bsComponent;
198  }
199 
200  public function getExtraFieldValidation($fieldname){
201  if(!isset($this->_extraFieldRules[$fieldname])){
202  $this->_extraFieldRules[$fieldname]=new FieldValidation($fieldname);
203  }
204  return $this->_extraFieldRules[$fieldname];
205  }
206 
207  public function addValidationParam($paramName,$paramValue,$before="",$after=""){
208  $this->addBehavior($this->_validationParams, $paramName, $paramValue,$before,$after);
209  return $this;
210  }
211 
212  public function setValidationParams(array $_validationParams) {
213  $this->_validationParams=$_validationParams;
214  return $this;
215  }
216 
217  public function hasValidationParams(){
218  return sizeof($this->_validationParams)>0;
219  }
220 
221  public function getValidationParams() {
223  }
224 
225  public function removeValidationParam($param){
226  unset($this->_validationParams[$param]);
227  return $this;
228  }
229 
230 }
Semantic Message component.
Definition: HtmlMessage.php:18
trait FormTrait
trait used in Widget and HtmlForm
Definition: FormTrait.php:18
addField($field)
Adds a field (alias for addItem)
Definition: HtmlForm.php:140
addToProperty($name, $value, $separator=" ")
addExtraCompoValidation(Form $compo, FieldValidation $validation)
Definition: FormTrait.php:34
addCompoValidation(Form $compo, HtmlFormField $field)
Definition: FormTrait.php:26
addContent($content, $before=false)
__construct($identifier, $elements=array())
Definition: HtmlForm.php:39
_runValidationParams(Form &$compo, JsUtils $js=NULL)
Definition: FormTrait.php:38
compile(JsUtils $js=NULL, &$view=NULL)
Definition: HtmlForm.php:166
setValidationParams(array $_validationParams)
Definition: HtmlForm.php:212
addHeader($title, $niveau=1, $dividing=true)
Definition: HtmlForm.php:59
semantic(Semantic $semantic=NULL)
getter or setter of the Semantic-UI variable
Definition: JsUtils.php:159
Semantic UI divider component.
Definition: HtmlDivider.php:14
addBehavior(&$array, $key, $value, $before="", $after="")
Definition: BaseTrait.php:202
Base class for Semantic Html collections.
addDivider($caption=NULL)
Adds a divider.
Definition: HtmlForm.php:71
JQuery PHP library.
Definition: JsUtils.php:23
Semantic Header component.
Definition: HtmlHeader.php:19
addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL)
Definition: HtmlForm.php:153
addValidationParam($paramName, $paramValue, $before="", $after="")
Definition: HtmlForm.php:207
getElementById($identifier, $elements)
Definition: BaseHtml.php:179
setEqualWidth()
automatically divide fields to be equal width
Definition: HtmlForm.php:131
addFields($fields=NULL, $label=NULL)
Adds a group of fields.
Definition: HtmlForm.php:81