Framework  3.9
credit_card_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2014 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 
41 {
42  var $size = 19;
43  var $onKeyUp; // javascript callback function
44  var $disable = false; // alternative to readonly; field can be reenabled or set thru javascript
45  var $onChange = ""; // optional callback on data input change
46 
47  function CreditCardNumberFieldRenderer(&$parent, $field = null)
48  {
49  $this->FieldRenderer($parent);
50  if ($field && !$parent->data->hasField($field))
51  {
52  $parent->add($this, $field);
53  }
54  }
55 
56  function addValidatorsToForm($field, $required = false)
57  {
58  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
59 
60  $this->parent->validator->add(new CreditCardNumberValidator($field, $label));
61 
62  if($required)
63  {
64  $this->parent->validator->add(new RequiredValidator($field, $label));
65  }
66 
67  }
68 
69  function renderScript($field)
70  {
71  $id = "{$this->parent->id}_{$field}";
72 ?>
73 <script type='text/javascript'>
74 function <?echo $id?>_checkValid(input)
75 {
76  if (luhnCheck(input.value) && input.value.length > 12 && input.value.length < 20)
77  {
78  input.addClass('valid');
79  }
80  else
81  {
82  input.removeClass('valid');
83  }
84 }
85 </script>
86 <?
87  }
88 
89  function renderField($field)
90  {
91  $this->_startField($field);
92 
93  $disable = ($this->disable) ? "disabled='disabled'" : "";
94 
95  $id = "{$this->parent->id}_{$field}";
96 
97  $onchange = ($this->onChange) ? "onchange='{$this->onChange}(this)'" : "";
98 
99  echo "<input id='{$id}' type='text' class='credit_card_number' style='width: auto' name='$field'
100  value='".$this->parent->data->get($field)."' autocomplete='off'
101  size='{$this->size}' onkeypress='return maskInput(event, 0);' $disable $onchange onkeyup='{$id}_checkValid(this)'/>";
102 
103  $this->_endField($field);
104  }
105 
106  function renderReadOnly($field)
107  {
108  $this->_startField($field);
109 
110  echo $this->parent->data->get($field);
111 
112  $this->_endField($field);
113  }
114 }
115 ?>
CreditCardNumberFieldRenderer(&$parent, $field=null)
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
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...
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