Framework  3.9
string_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__))."/../field_renderers.inc";
39 
62 {
63  var $size = 50;
64  var $limit = 0;
65  var $autocomplete = true;
66  var $onChange = "";
67  var $disable = false;
68  var $password = false;
69  var $template = "";
70  var $confirm = false;
71  var $width = "";
72  var $CSSclass = "";
73  var $numeric = false;
74  var $onKeyPress = null;
75  var $placeholder = null;
76 
78  {
79  $this->FieldRenderer($parent);
80  }
81 
82  function renderScript($field)
83  {
84  if ($this->limit)
85  {
86 ?>
87 <script type='text/javascript'>
88 /* <![CDATA[ */
89  function <?echo $field ?>_keyup(event)
90  {
91  var form = document.id('<?echo $this->parent->id ?>');
92 
93  var ctrl = form['<?echo $field ?>'];
94  var len = ctrl.value.length;
95  if (len >= <?echo $this->limit?>)
96  {
97  ctrl.value = ctrl.value.substring(0, <?echo $this->limit ?>);
98  len = <?echo $this->limit ?>;
99  }
100 
101  var count = document.getElementById('<?echo $field ?>_length');
102 
103  // JDG 2/1/10 must check count to fix IE7 bug
104  if(count)
105  count.innerHTML = len;
106  }
107 </script>
108 <?
109  }
110  }
111 
112  function renderField($field, $readonly = false)
113  {
114  $this->_startField($field);
115 
116  if ($this->limit)
117  {
118  echo "<div>";
119  $onkeypress = " onkeyup='{$field}_keyup(event);'";
120  }
121 
122  if (!$onkeypress && $this->onKeyPress)
123  {
124  $onkeypress = " onkeyup='return {$this->onKeyPress}(this);'";
125  }
126 
127  $autocomplete = (!$this->autocomplete) ? "autocomplete='off' " : "";
128  $onchange = ($this->onChange) ? "onchange='$this->onChange(this)'" : "";
129  $disable = ($this->disable) ? "disabled='disabled'" : "";
130  $inputType = (!$this->password) ? "text" : "password";
131  $style = ($this->width) ? " style='width: {$this->width}'" : "";
132  $class = ($this->CSSclass) ? "class='{$this->CSSclass}'" : "";
133  $value = (!$this->password) ? htmlspecialchars($this->parent->data->get($field), ENT_QUOTES, 'UTF-8') : "";
134 
135  $keyPress = $this->numeric ? "onkeypress='return maskInput(event, false);'" : "";
136 
137  $placeholder = ($this->placeholder) ? " placeholder='".htmlSafe($this->placeholder)."'" : "";
138 
139  echo "<input id='{$this->parent->id}_{$field}' type='$inputType' name='$field' $placeholder $keyPress $onchange $autocomplete $class $style value='$value' size='{$this->size}' $disable $onkeypress/>";
140 
141  if ($this->limit)
142  {
143  echo "</div>";
144  }
145  $this->_endField($field);
146 
147  if ($this->confirm)
148  {
149  $confirm = "confirm_$field";
150  $this->_startField($confirm);
151 
152  $autocomplete = (!$this->autocomplete) ? "autocomplete='off' " : "";
153  $disable = ($this->disable) ? "disabled='disabled'" : "";
154  $inputType = (!$this->password) ? "text" : "password";
155  echo "<input id='{$this->parent->id}_{$confirm}' type='$inputType' name='$confirm' $autocomplete value='".htmlspecialchars($this->parent->data->get($field), ENT_QUOTES, 'UTF-8')."' size='{$this->size}' $disable/>";
156 
157  $this->_endField($field);
158  }
159  }
160 
161  function renderReadOnly($field)
162  {
163  $this->_startField($field);
164 
165  if (!$this->parent->readOnlyForm)
166  {
167  echo "<input id='{$this->parent->id}_{$field}' type='hidden' name='$field' value='".htmlspecialchars($this->parent->data->get($field), ENT_QUOTES, 'UTF-8')."'/>";
168  }
169 
170  echo $this->format($field);
171 
172  $this->_endField($field);
173  }
174 
175  function renderSearchField($field, $mode = "equal")
176  {
177  if ($mode == "range")
178  {
179  $from = htmlspecialchars($this->parent->params->get($field, "from"), ENT_QUOTES, 'UTF-8');
180  $to = htmlspecialchars($this->parent->params->get($field, "to"), ENT_QUOTES, 'UTF-8');
181 
182  echo "<tr>\n";
183  $this->_printLabel($field);
184  echo "<td>From <input type='text' id='$field' name='$field:from' value='{$from}' size='25'> to <input type='text' name='$field:to' value='{$to}' size='25'></td>\n";
185  echo "</tr>\n";
186  }
187  else
188  {
189  $value = htmlspecialchars($this->parent->params->get($field, $mode), ENT_QUOTES, 'UTF-8');
190  $this->_startField($field);
191  $placeholder = ($this->placeholder) ? " placeholder='".htmlSafe($this->placeholder)."'" : "";
192  echo "<input type='text' id='$field' name='$field:$mode' {$placeholder} value='{$value}' size='{$this->size}' >\n";
193  $this->_endField($field);
194  }
195  }
196 
197 
198  function addValidatorsToForm($field, $required = false)
199  {
200  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
201  $confirm = "confirm_$field";
202  $confirmLabel = "Confirm $label";
203 
204  if($required)
205  $this->parent->validator->add(new RequiredValidator($field, $label));
206 
207  if ($this->confirm)
208  {
209  $this->parent->validator->add(new MatchValidator($field, $label, $confirm, $confirmLabel));
210  }
211  }
212 
213 
214  function format($field)
215  {
216  return StringTypeRenderer::format($this->parent->data->get($field), $this->template);
217  }
218 
219 }
220 ?>
FieldRenderer is the abstract base class for all FieldRenderers.
_startField($field, $styles="")
Internal method to generate the starting HTML for the field (including the label)
_endField($field)
Internal method to generate the closing HTML for the field.
_printLabel($field, $colspan=1, $styles="", $annotation="")
Internal method to generate the HTML for the field label.
FieldRenderer($parent)
Constructor.
Tests whether two fields contain the same value.
Definition: validation.inc:814
RequiredField Validator.
Definition: validation.inc:76
Field renderer for string data fields.
$onKeyPress
Optional key press javascript callback. Note that if $limit is set, it will override this.
$disable
alternative to readonly; field can be reenabled thru javascript
$template
StringTypeRenderer template for readonly format (e.g., "prettify")
$confirm
Render a second field for the user to confirm their entry (for email addresses, etc)
renderField($field, $readonly=false)
$limit
optional - restricts users from entering character count over limit
$autocomplete
default is true. Set to false to disable autocompletion
$numeric
Restrict to numeric input.
$placeholder
Placeholder text to display when field is empty.
renderSearchField($field, $mode="equal")
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
$width
Optionally specify a CSS width for the field.
$password
Render the field as a password field (masked characters)
$CSSclass
Optionally specify a CSS class for the field.
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
$onChange
optional (javascript) callback on data input change
static format($value, $template="")