Framework  3.9
zipcode_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 
58 {
59  var $autocomplete = true; // default
60  var $onChange = ""; // optional callback on data input change
61  var $disable = false; // alternative to readonly; field can be reenabled thru javascript
62  var $includePlus4 = true;
63  var $international = false;
64  var $internationalLabel = "Postal Code";
65 
66  function ZipCodeFieldRenderer(&$form)
67  {
68  $this->FieldRenderer($form);
69  }
70 
71  function setInternational($label = "Postal Code")
72  {
73  $this->international = true;
74  }
75 
76  function addValidatorsToForm($field, $required = false)
77  {
78  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
79 
80  if($required)
81  {
82  if (!$this->international)
83  {
84  $expr = $this->includePlus4 ? "^\\d{5}(?:-\\d{4})?$" : "^\\d{5}$";
85 
86  $this->parent->regexp($field, $expr, "Please supply a valid zip code.");
87  }
88 
89  $this->parent->validator->add(new RequiredValidator($field, $label));
90  }
91  }
92 
93  function renderScript($field)
94  {
95  $size = $this->includePlus4 ? 10 : 5;
96 ?>
97 <script type='text/javascript'>
98 /* <![CDATA[ */
99  function <?echo $field ?>_keyup(event)
100  {
101  var ctrl = document.id('<? echo "{$this->parent->id}_$field" ?>');
102  var len = ctrl.value.length;
103  if (len >= <?echo $size?>)
104  {
105  ctrl.value = ctrl.value.substring(0, <?echo $size?>);
106  len = <?echo $size?>;
107  }
108 
109  var count = ctrl.length;
110 
111  // JDG 2/1/10 must check count to fix IE7 bug
112  if(count)
113  count.innerHTML = len;
114  }
115 </script>
116 <?
117  }
118 
119  function renderField($field)
120  {
121  $this->_startField($field);
122 
123 
124  if (!$this->international)
125  {
126  $size = $this->includePlus4 ? 10 : 5;
127  $onkeyup = " onkeyup='{$field}_keyup(event);'";
128  $onkeypress=" onkeypress='return maskInput(event)'";
129  }
130  else
131  {
132  $size = 12;
133  }
134 
135  $autocomplete = (!$this->autocomplete) ? "autocomplete='off' " : "";
136  $onchange = ($this->onChange) ? "onchange='$this->onChange(this)'" : "";
137  $disable = ($this->disable) ? "disabled='disabled'" : "";
138 
139  echo "<input id='{$this->parent->id}_{$field}'
140  type='text' style='width: auto' name='$field' $onchange $autocomplete
141  value='".htmlspecialchars($this->parent->data->get($field), ENT_QUOTES, 'UTF-8')."'
142  size='{$size}' $disable $onkeypress $onkeyup/>";
143  $this->_endField($field);
144  }
145 
146 
147  function renderReadOnly($field)
148  {
149  $this->_startField($field);
150 
151  echo $this->parent->data->get($field);
152 
153  $this->_endField($field);
154  }
155 
156  function renderSearchField($field, $mode = "equal")
157  {
158  $this->_startField($field);
159 
160  $onkeyup = " onkeyup='{$field}_keyup(event);'";
161  $onkeypress=" onkeypress='return maskInput(event)'";
162 
163  if ($mode == "range")
164  {
165  $from = htmlspecialchars($this->parent->params->get($field, "from"), ENT_QUOTES, 'UTF-8');
166  $to = htmlspecialchars($this->parent->params->get($field, "to"), ENT_QUOTES, 'UTF-8');
167 
168  echo "From <input type='text' id='$field' name='$field:from' value='{$from}' size='10'$onkeypress$onkeyup> to <input type='text' name='$field:to' value='{$to}' size='10'$onkeypress$onkeyup>";
169  }
170  else
171  {
172  $value = htmlspecialchars($this->parent->params->get($field, $mode), ENT_QUOTES, 'UTF-8');
173  echo "<input type='text' id='$field' name='$field:$mode' value='{$value}' size='10'$onkeypress$onkeyup>";
174  }
175 
176  $this->_endField($field);
177 
178  }
179 
180 }
181 ?>
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.
FieldRenderer($parent)
Constructor.
RequiredField Validator.
Definition: validation.inc:76
Field renderer for zipcode data fields.
setInternational($label="Postal Code")
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 ...
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...