| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: |
|
| 11: | namespace Sphp\Comp\Form{
|
| 12: |
|
| 13: | class Radio extends \Sphp\tools\Component{
|
| 14: | private $formName = '';
|
| 15: | private $msgName = '';
|
| 16: | private $req = false;
|
| 17: | private $options = array();
|
| 18: |
|
| 19: | protected function genhelpPropList() {
|
| 20: | parent::genhelpPropList();
|
| 21: | $this->addHelpPropFunList('setForm','Bind with Form JS Event','','$val');
|
| 22: | $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
|
| 23: | $this->addHelpPropFunList('setRequired','Can not submit Empty','','');
|
| 24: | }
|
| 25: |
|
| 26: | protected function oninit() {
|
| 27: | $this->tagName = "input";
|
| 28: | if(!$this->element->hasAttribute("name")){
|
| 29: | $this->HTMLName = $this->name;
|
| 30: | }else{
|
| 31: | $this->HTMLName = $this->getAttribute("name");
|
| 32: | }
|
| 33: |
|
| 34: | $this->setAttribute('type', 'radio');
|
| 35: | }
|
| 36: |
|
| 37: | public function fi_setForm($val) { $this->formName = $val;}
|
| 38: | public function fu_setMsgName($val) { $this->msgName = $val;}
|
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: |
|
| 45: | public function fu_setOptions($val) {
|
| 46: | $opt1 = array();
|
| 47: | if($val[0] == '{'){
|
| 48: | $opt2 = json_decode($val,true);
|
| 49: | foreach($opt2 as $key=>$val){
|
| 50: | $opt1[] = [$key,$val];
|
| 51: | }
|
| 52: | $this->options = $opt1;
|
| 53: | }else{
|
| 54: | $this->options = explode(",",$val);
|
| 55: | }
|
| 56: |
|
| 57: | }
|
| 58: | public function fi_setRequired() {
|
| 59: | if($this->issubmit){
|
| 60: | if(strlen($this->value) < 1){
|
| 61: | setErr($this->name,"Can not submit Empty");
|
| 62: | }
|
| 63: | }
|
| 64: | $this->req = true;
|
| 65: | }
|
| 66: |
|
| 67: | protected function onprejsrender(){
|
| 68: | if($this->msgName == "") $this->msgName = $this->getAttribute("placeholder");
|
| 69: | if($this->formName !='' && $this->req){
|
| 70: | $jscode = "var f1 = ". $this->getJSValue() ."; var v1 = f1(); if(blnSubmit==true && v1[0]==-1){blnSubmit = false ; alert('Please Select ". $this->msgName . "'); document.getElementById('" . $this->name ."0').focus();}";
|
| 71: | addHeaderJSFunctionCode("{$this->formName}_submit", "$this->name",$jscode);
|
| 72: | }
|
| 73: | }
|
| 74: |
|
| 75: | protected function onrender(){
|
| 76: | if($this->getAttribute('class')==''){
|
| 77: | $class = "form-check form-check-inline";
|
| 78: | }else{
|
| 79: | $class = $this->element->getAttribute("class");
|
| 80: | }
|
| 81: | $chk = false;
|
| 82: | if($this->getAttribute('checked')!='') $chk = true;
|
| 83: |
|
| 84: | $stro = "";
|
| 85: | if(strlen($this->msgName) > 2) $stro = "<h2 class=\"\">{$this->msgName}</h2>";
|
| 86: |
|
| 87: | $blnchkone = true;
|
| 88: | foreach($this->options as $i=>$v){
|
| 89: | $v0 = "";
|
| 90: | $label = "";
|
| 91: | if(is_array($v)){
|
| 92: | $v0 = $v[0];
|
| 93: | $label = $v[1];
|
| 94: | }else{
|
| 95: | $v0 = $v;
|
| 96: | $label = $v;
|
| 97: | }
|
| 98: | $checked = "";
|
| 99: | $disabled = "";
|
| 100: | if($blnchkone){
|
| 101: | if($this->value != "" && $v0 == $this->value){
|
| 102: | $checked = 'checked="checked"';
|
| 103: | $blnchkone = false;
|
| 104: | }else if($chk && $this->getAttribute("checked") == $v0){
|
| 105: | $checked = 'checked="checked"';
|
| 106: | $blnchkone = false;
|
| 107: | }
|
| 108: | }
|
| 109: | if($this->getAttribute("disabled") == $v0){
|
| 110: | $disabled = 'disabled="disabled"';
|
| 111: | }
|
| 112: | $stro .= '<div class="'. $class .'">
|
| 113: | <input class="form-check-input" type="radio" value="'. $v0 .'" name="'. $this->name .'" id="'. $this->name . $i .'" '. $checked .' '. $disabled .'>
|
| 114: | <label class="form-check-label" for="'. $this->name .'">
|
| 115: | '. $label .'
|
| 116: | </label>
|
| 117: | </div>';
|
| 118: | }
|
| 119: |
|
| 120: |
|
| 121: | $parentdiv = $this->element->wrapTag('div');
|
| 122: |
|
| 123: | $parentdiv->setInnerHTML($stro);
|
| 124: | $parentdiv->setAttribute("class","px-2 py-2");
|
| 125: |
|
| 126: |
|
| 127: | }
|
| 128: |
|
| 129: |
|
| 130: |
|
| 131: | public function getJSValue(){
|
| 132: | return " function(){var v1 = []; v1[0] = -1; v1[1]=''; for(var c=0;c<". count($this->options) ."; c++){ if(document.getElementById('$this->name' + c).checked){v1[0] = c;v1[1] = \$('#$this->name' + c).val(); } } return v1;}" ;
|
| 133: | }
|
| 134: |
|
| 135: |
|
| 136: | }
|
| 137: | }
|
| 138: | |