1: <?php
2:
3: /**
4: * Description of textfield
5: *
6: * @author SARTAJ
7: */
8:
9: namespace Sphp\Comp\Form{
10:
11: class TextField extends \Sphp\tools\Component{
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: private $match = "";
24:
25: protected function genhelpPropList() {
26: parent::genhelpPropList();
27: $this->addHelpPropFunList('setForm','Bind with Form JS Submit Event','','$val');
28: $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
29: $this->addHelpPropFunList('setNumeric','Only Accept Numeric Value','','');
30: $this->addHelpPropFunList('setRequired','Can not submit Empty','','');
31: $this->addHelpPropFunList('setEmail','Only Accept Email','','');
32: $this->addHelpPropFunList('setMaxLen','Maximum Accept Length','','$val');
33: $this->addHelpPropFunList('setMinLen','Minimum Accept Length','','$val');
34: $this->addHelpPropFunList('setPassword','Mask as Pasword Char','','');
35: $this->addHelpPropFunList('setReadOnly','Set as Read Only','','');
36: }
37: protected function oninit() {
38: $this->tagName = "input";
39: if(!$this->element->hasAttribute("name")){
40: $this->HTMLName = $this->name;
41: }else{
42: $this->HTMLName = $this->getAttribute("name");
43: }
44: if($this->getAttribute("type") == ""){
45: $this->setAttribute('type', 'text');
46: }
47: if($this->getAttribute("msgname") != ""){
48: $this->msgName = $this->getAttribute("msgname");
49: }
50:
51: // $this->unsetEndTag();
52: }
53:
54: public function fi_setForm($val) {
55: $this->formName = $val;
56: }
57:
58: // upper or lower
59: public function fi_setCase($val) {
60: if($val == "upper"){
61: $this->value = strtoupper($this->value);
62: }else{
63: $this->value = strtolower($this->value);
64: }
65: }
66:
67: public function fu_setMsgName($val) {
68: $this->msgName = $val;
69: $this->setAttribute('placeholder', $val);
70: }
71: public function setErrMsg($msg){
72: $this->errmsg .= '<strong class="alert-danger">' . $msg . '! </strong>';
73: if(\SphpBase::sphp_request()->isAJAX()){
74: \SphpBase::JSServer()->addJSONJSBlock('$("#'. $this->name .'").after("<strong class=\"alert-danger\">' . $msg . '! </strong>");');
75: }
76: setErr($this->name, $msg);
77: }
78: public function fi_setNumeric() {
79: if ($this->issubmit) {
80: if (!is_valid_num($this->value, $this->dataType)) {
81: if ($this->dataType == "INT") {
82: $msg = "Please Fill a Whole Number";
83: } else {
84: $msg = "Please Fill a Number";
85: }
86: $this->setErrMsg( $this->getAttribute("msgname") .' ' . $msg);
87: }
88: }
89: $this->numeric = true;
90: }
91:
92: public function fi_setMatch($compid) {
93: if ($this->issubmit) {
94: if ($this->value != $this->frontobj->getComponent($compid)->value) {
95: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Value is not Match");
96: }
97: }
98: $this->match = $compid;
99: }
100:
101: public function fi_setRequired() {
102: if ($this->issubmit) {
103: if (strlen($this->value) < 1) {
104: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Can not submit Empty");
105: }
106: }
107: $this->req = true;
108: }
109:
110: public function fi_setEmail() {
111: if ($this->issubmit) {
112: if (strlen($this->value) > 0 && !is_valid_email($this->value)) {
113: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Please Fill correct Email Address");
114: }
115: }
116: $this->email = true;
117: }
118:
119: public function fi_setMaxLen($val) {
120: $this->maxLen = $val;
121: if ($this->issubmit) {
122: if (strlen($this->getValue()) > $val) {
123: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Maximum Characters should not be exceed then $val");
124: }
125: }
126: }
127:
128: public function getMaxLen() {
129: return $this->maxLen;
130: }
131:
132: public function fi_setMinLen($val) {
133: $this->minLen = $val;
134: if ($this->issubmit) {
135: if ($this->getValue() != '' && strlen($this->getValue()) < $val) {
136: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Minimum Characters should be $val");
137: }
138: }
139: }
140:
141: public function getMinLen() {
142: return $this->minLen;
143: }
144:
145: public function fu_setPassword() {
146: $this->password = true;
147: }
148:
149: public function unsetPassword() {
150: $this->password = false;
151: }
152:
153: public function getPassword() {
154: return $this->password;
155: }
156:
157: public function fu_setReadOnly() {
158: $this->readOnly = true;
159: }
160:
161: public function unsetReadOnly() {
162: $this->readOnly = false;
163: }
164:
165: public function getReadOnly() {
166: return $this->readOnly;
167: }
168:
169: protected function onjsrender() {
170: if ($this->formName != '') {
171: if ($this->msgName == "") {
172: $this->fu_setMsgName($this->getAttribute('placeholder'));
173: }
174: if ($this->minLen != '') {
175: addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}min", "
176: ctlMins['$this->name']= Array('$this->msgName','TextField','$this->minLen');");
177: }
178: if ($this->numeric) {
179: addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}num", "
180: ctlNums['$this->name']= Array('$this->msgName','TextField');");
181: }
182: if ($this->email) {
183: addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}email", "
184: ctlEmail['$this->name']= Array('$this->msgName','TextField');");
185: }
186: if ($this->req) {
187: addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}req", "
188: ctlReq['$this->name']= Array('$this->msgName','TextField');");
189: }
190: if ($this->match != "") {
191: addHeaderJSFunctionCode("{$this->formName}_submit", "{$this->name}match", '
192: if($("#'. $this->name .'").val() != $("#'. $this->match .'").val()){
193: displayValidationError(document.getElementById("'. $this->name .'"),"Value isn\'t match with " + $("#'. $this->match .'").attr("placeholder"));
194: blnSubmit = false ;
195: }');
196: }
197:
198: }
199: }
200:
201: protected function onrender() {
202: if($this->errmsg!=""){
203: $this->setPostTag($this->errmsg);
204: }
205: $this->setAttributeDefault("class","form-control");
206: if ($this->maxLen != '') {
207: $this->setAttribute('maxlength', $this->maxLen);
208: }
209: if ($this->password == true) {
210: $this->setAttribute('type', 'password');
211: } else if ($this->getAttribute('type') == 'password') {
212: $this->password = true;
213: }
214:
215: if ($this->readOnly == true) {
216: $this->setAttribute('readonly', 'readonly');
217: }
218: if ($this->password == true) {
219: $this->value = "";
220: }
221: if ($this->value != "") {
222: $this->setAttribute('value', htmlentities($this->value, ENT_COMPAT, "UTF-8"));
223: }
224: if($this->getVisible()){
225: switch($this->styler){
226: case 1:{
227: $this->setPreTag($this->getPreTag() . '<div class="form-floating mb-3">');
228: $this->setPostTag('<label for="'. $this->HTMLID .'" class="form-label">'. $this->msgName .'</label></div>' . $this->getPostTag());
229: break;
230: }case 2:{
231: $this->setPreTag($this->getPreTag() .'<div class="mb-3">
232: <label for="'. $this->HTMLID .'" class="form-label">'. $this->msgName .'</label>');
233: $this->setPostTag('<div id="'. $this->HTMLID .'Help" class="form-text">'. $this->helptext .'</div></div>'. $this->getPostTag());
234: break;
235: }
236: }
237: }
238: }
239:
240: // javascript functions used by ajax control and other control
241: public function getJSValue() {
242: return "document.getElementById('$this->name').value";
243: }
244:
245: public function setJSValue($exp) {
246: return "document.getElementById('$this->name').value = $exp;";
247: }
248:
249: }
250:
251: }
252: