1: <?php
2:
3: /**
4: * Description of textfield
5: *
6: * @author SARTAJ
7: */
8:
9: namespace Sphp\comp\html{
10:
11: class TextField extends \Sphp\tools\Control {
12:
13: public $maxLen = '';
14: public $minLen = '';
15: private $formName = '';
16: private $msgName = '';
17: private $errmsg = '';
18: private $password = false;
19: private $readOnly = false;
20: private $numeric = false;
21: private $email = false;
22: private $req = false;
23:
24: protected function genhelpPropList() {
25: parent::genhelpPropList();
26: $this->addHelpPropFunList('setForm','Bind with Form JS Event','','$val');
27: $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
28: $this->addHelpPropFunList('setNumeric','Only Accept Numeric Value','','');
29: $this->addHelpPropFunList('setRequired','Can not submit Empty','','');
30: $this->addHelpPropFunList('setEmail','Only Accept Email','','');
31: $this->addHelpPropFunList('setMaxLen','Maximum Accept Length','','$val');
32: $this->addHelpPropFunList('setMinLen','Minimum Accept Length','','$val');
33: $this->addHelpPropFunList('setPassword','Mask as Pasword Char','','');
34: $this->addHelpPropFunList('setReadOnly','Set as Read Only','','');
35: }
36: public function oninit() {
37: $this->tagName = "input";
38: if($this->getAttribute("type") == ""){
39: $this->setAttribute('type', 'text');
40: }
41: if($this->getAttribute("msgname") != ""){
42: $this->msgName = $this->getAttribute("msgname");
43: }
44: // $this->unsetEndTag();
45: }
46:
47: public function setForm($val) {
48: $this->formName = $val;
49: }
50:
51: // upper or lower
52: public function setCase($val) {
53: if($val == "upper"){
54: $this->value = strtoupper($this->value);
55: }else{
56: $this->value = strtolower($this->value);
57: }
58: }
59:
60: public function setMsgName($val) {
61: $this->msgName = $val;
62: $this->setAttribute('placeholder', $val);
63: }
64: public function setErrMsg($msg){
65: $this->errmsg .= '<strong class="alert-danger">' . $msg . '! </strong>';
66: setErr($this->name, $msg);
67: }
68: public function setNumeric() {
69: if ($this->issubmit) {
70: if (!is_valid_num($this->value, $this->dataType)) {
71: if ($this->dataType == "INT") {
72: $msg = "Please Fill a Whole Number";
73: } else {
74: $msg = "Please Fill a Number";
75: }
76: $this->setErrMsg( $this->getAttribute("msgname") .' ' . $msg);
77: }
78: }
79: $this->numeric = true;
80: }
81:
82: public function setRequired() {
83: if ($this->issubmit) {
84: if (strlen($this->value) < 1) {
85: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Can not submit Empty");
86: }
87: }
88: $this->req = true;
89: }
90:
91: public function setEmail() {
92: if ($this->issubmit) {
93: if (strlen($this->value) > 0 && !is_valid_email($this->value)) {
94: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Please Fill correct Email Address");
95: }
96: }
97: $this->email = true;
98: }
99:
100: public function setMaxLen($val) {
101: $this->maxLen = $val;
102: if ($this->issubmit) {
103: if (strlen($this->getValue()) > $val) {
104: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Maximum Characters should not be exceed then $val");
105: }
106: }
107: }
108:
109: public function getMaxLen() {
110: return $this->maxLen;
111: }
112:
113: public function setMinLen($val) {
114: $this->minLen = $val;
115: if ($this->issubmit) {
116: if ($this->getValue() != '' && strlen($this->getValue()) < $val) {
117: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Minimum Characters should be $val");
118: }
119: }
120: }
121:
122: public function getMinLen() {
123: return $this->minLen;
124: }
125:
126: public function setPassword() {
127: $this->password = true;
128: }
129:
130: public function unsetPassword() {
131: $this->password = false;
132: }
133:
134: public function getPassword() {
135: return $this->password;
136: }
137:
138: public function setReadOnly() {
139: $this->readOnly = true;
140: }
141:
142: public function unsetReadOnly() {
143: $this->readOnly = false;
144: }
145:
146: public function getReadOnly() {
147: return $this->readOnly;
148: }
149:
150: public function onjsrender() {
151: if ($this->formName != '') {
152: if ($this->minLen != '') {
153: addFooterJSFunctionCode("{$this->formName}_submit", "{$this->name}min", "
154: ctlMins['$this->name']= Array('$this->msgName','TextField','$this->minLen');");
155: }
156: if ($this->numeric) {
157: addFooterJSFunctionCode("{$this->formName}_submit", "{$this->name}num", "
158: ctlNums['$this->name']= Array('$this->msgName','TextField');");
159: }
160: if ($this->email) {
161: addFooterJSFunctionCode("{$this->formName}_submit", "{$this->name}email", "
162: ctlEmail['$this->name']= Array('$this->msgName','TextField');");
163: }
164: if ($this->req) {
165: addFooterJSFunctionCode("{$this->formName}_submit", "{$this->name}req", "
166: ctlReq['$this->name']= Array('$this->msgName','TextField');");
167: }
168: }
169: }
170:
171: public function onrender() {
172: if($this->errmsg!=""){
173: $this->setPostTag($this->errmsg);
174: }
175: if ($this->getAttribute("class") == "") {
176: $this->setAttribute("class","form-control");
177: }
178: if ($this->maxLen != '') {
179: $this->setAttribute('maxlength', $this->maxLen);
180: }
181: if ($this->password == true) {
182: $this->setAttribute('type', 'password');
183: } else if ($this->getAttribute('type') == 'password') {
184: $this->password = true;
185: }
186:
187: if ($this->readOnly == true) {
188: $this->setAttribute('readonly', 'readonly');
189: }
190: if ($this->value != "") {
191: $this->setAttribute('value', htmlentities($this->value, ENT_COMPAT, "UTF-8"));
192: }
193: }
194:
195: // javascript functions used by ajax control and other control
196: public function getJSValue() {
197: return "document.getElementById('$this->name').value";
198: }
199:
200: public function setJSValue($exp) {
201: return "document.getElementById('$this->name').value = $exp;";
202: }
203:
204: }
205:
206: }
207: