| 1: | <?php
|
| 2: |
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: |
|
| 9: | namespace Sphp\Comp\Form {
|
| 10: |
|
| 11: | class CheckBox extends \Sphp\tools\Component {
|
| 12: |
|
| 13: | private $formName = '';
|
| 14: | private $msgName = '';
|
| 15: | private $errmsg = '';
|
| 16: | private $req = false;
|
| 17: | private $label = "";
|
| 18: |
|
| 19: | protected function oninit() {
|
| 20: | $Client = \SphpBase::sphp_request();
|
| 21: | $this->tagName = "input";
|
| 22: | if(!$this->element->hasAttribute("name")){
|
| 23: | $this->HTMLName = $this->name;
|
| 24: | }else{
|
| 25: | $this->HTMLName = $this->getAttribute("name");
|
| 26: | }
|
| 27: |
|
| 28: | $this->setAttribute('type', 'checkbox');
|
| 29: | if ($this->issubmit) {
|
| 30: | $this->setAttribute('checked', 'checked');
|
| 31: | } else if ($Client->request('chktxt' . $this->name) == '1') {
|
| 32: | $this->value = '0';
|
| 33: | $this->setDataBound();
|
| 34: | }
|
| 35: | if ($this->getAttribute("msgname") != "") {
|
| 36: | $this->msgName = $this->getAttribute("msgname");
|
| 37: | $this->label = $this->msgName;
|
| 38: | }else if ($this->getAttribute("placeholder") != "") {
|
| 39: | $this->msgName = $this->getAttribute("placeholder");
|
| 40: | $this->label = $this->msgName;
|
| 41: | }
|
| 42: | }
|
| 43: |
|
| 44: | public function fu_setLabel($param) {
|
| 45: | $this->label = $param;
|
| 46: | }
|
| 47: |
|
| 48: | public function setErrMsg($msg) {
|
| 49: | $this->errmsg .= '<strong class="alert-danger">' . $msg . '</strong>';
|
| 50: | if(\SphpBase::sphp_request()->isAJAX()){
|
| 51: | \SphpBase::JSServer()->addJSONJSBlock('$("#'. $this->name .'").after("<strong class=\"alert-danger\">' . $msg . '! </strong>");');
|
| 52: | }
|
| 53: | setErr($this->name, $msg);
|
| 54: | }
|
| 55: |
|
| 56: | protected function genhelpPropList() {
|
| 57: | $this->addHelpPropFunList('setForm', 'Bind with Form JS Event', '', '$val');
|
| 58: | $this->addHelpPropFunList('setMsgName', 'Name Display in placeholder and Error', '', '$val');
|
| 59: | $this->addHelpPropFunList('setRequired', 'Can not submit Empty', '', '');
|
| 60: | }
|
| 61: |
|
| 62: | public function fi_setForm($val) {
|
| 63: | $this->formName = $val;
|
| 64: | }
|
| 65: |
|
| 66: | public function fu_setMsgName($val) {
|
| 67: | $this->msgName = $val;
|
| 68: | $this->label = $this->msgName;
|
| 69: | $this->setAttribute('placeholder', $val);
|
| 70: | }
|
| 71: |
|
| 72: | public function fi_setRequired() {
|
| 73: | if ($this->issubmit) {
|
| 74: | if (strlen($this->value) < 1) {
|
| 75: | $this->setErrMsg($this->getAttribute("msgname") . ' ' . "Can not submit Empty");
|
| 76: | }
|
| 77: | }
|
| 78: | $this->req = true;
|
| 79: | }
|
| 80: |
|
| 81: | protected function onprejsrender() {
|
| 82: | if ($this->formName != '' && $this->req) {
|
| 83: | $jscode = "if(blnSubmit==true && " . $this->getJSValue() . "==false){
|
| 84: | blnSubmit = false ;
|
| 85: | alert('Please Accept " . $this->msgName . "');
|
| 86: | document.getElementById('$this->name').focus();
|
| 87: | }";
|
| 88: | addHeaderJSFunctionCode("{$this->formName}_submit", "$this->name", $jscode);
|
| 89: | }
|
| 90: | }
|
| 91: |
|
| 92: | protected function onrender() {
|
| 93: | if ($this->errmsg != "") {
|
| 94: | $this->setPostTag($this->errmsg);
|
| 95: | }
|
| 96: |
|
| 97: | $this->class .= " form-check-input";
|
| 98: |
|
| 99: | $this->setPostTag('<input type="hidden" name="chktxt' . $this->name . '" value="1" />');
|
| 100: |
|
| 101: | if ($this->value != '1') {
|
| 102: | $this->setAttribute('value', '1');
|
| 103: | } else {
|
| 104: | $this->setAttribute('value', '1');
|
| 105: | $this->setAttribute('checked', 'checked');
|
| 106: | }
|
| 107: |
|
| 108: | switch($this->styler){
|
| 109: | case 1:{
|
| 110: | $this->setPreTag($this->getPreTag() . '<div class="form-floating mb-3 form-check">');
|
| 111: | $this->setPostTag('<label for="'. $this->HTMLID .'" class="form-check-label">'. $this->msgName .'</label></div>' . $this->getPostTag());
|
| 112: | break;
|
| 113: | }case 2:{
|
| 114: | $this->setPreTag($this->getPreTag() .'<div class="mb-3 form-check">
|
| 115: | <label for="'. $this->HTMLID .'" class="form-check-label">'. $this->msgName .'</label>');
|
| 116: | $this->setPostTag('<div id="'. $this->HTMLID .'Help" class="form-text">'. $this->helptext .'</div></div>'. $this->getPostTag());
|
| 117: | break;
|
| 118: | }case 3:{
|
| 119: | $this->element->getParent()->setAttribute("class","input-group mb-3");
|
| 120: | $this->setPreTag($this->getPreTag() .'<div class="input-group-text">
|
| 121: | <label for="'. $this->HTMLID .'" class="form-check-label">'. $this->msgName .' </label>');
|
| 122: | $this->setPostTag('<div id="'. $this->HTMLID .'Help" class="form-text">'. $this->helptext .'</div></div>'. $this->getPostTag());
|
| 123: | break;
|
| 124: | }
|
| 125: | default:{
|
| 126: | if ($this->label != "") {
|
| 127: | $this->setPreTag($this->getPreTag() . '<div class="mb-3 form-check">');
|
| 128: | $this->setPostTag('<label class="form-check-label" for="' . $this->HTMLID . '">' . $this->label . '</label></div>' . $this->getPostTag());
|
| 129: | }
|
| 130: | break;
|
| 131: | }
|
| 132: | }
|
| 133: |
|
| 134: | }
|
| 135: |
|
| 136: |
|
| 137: | public function getJSValue() {
|
| 138: | return "document.getElementById('$this->name').checked";
|
| 139: | }
|
| 140: |
|
| 141: | public function setJSValue($exp) {
|
| 142: | $jsOut = "document.getElementById('$this->name').checked = $exp;";
|
| 143: | writeGlobal("jsOut", $jsOut);
|
| 144: | }
|
| 145: |
|
| 146: | }
|
| 147: |
|
| 148: | }
|
| 149: | |