/ Published in: PHP
If you had seen some errors in the code, please send me notice about it on the: [email protected]
Thanks, K.Karpenko
Thanks, K.Karpenko
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class forms implements iforms{ var $_inited=false; /**_elems: array( array('id'=>'','name'=>'login','value'=>'','validate'=>true,'params'=>'','on'=>array('click'=>'event')) );**/ /** _fields: array('id'=>'','label'=>"Enter your login:","value"=>"el:21551","type"=>"pair","style"=>""); **/ var $_data=''; /** * Constructor. Make new instance of the form container. * @return * @param $method Object * @param $action Object * @param $encoding Object * @param $params Object[optional] */ { if($this->correctMethod($method) && !$this->_inited){ $this->_data=''; $this->_form['method']=$method; $this->_form['action']=$action; $this->_form['encoding']=$encoding; $this->_form['params']=$params; $this->_form['id']=$id; $this->_form['name']=$name; $this->initOthers(); $this->initPatterns(); $this->_inited=true; }else{ return IFORM_WRONG_METHOD; } } protected function initOthers(){ //CSS $this->css['pairFieldLabelClass']='label'; $this->css['singleFieldLabelClass']='label label1 center'; $this->css['sinleFieldLabelStyle']='width:100%;clear:both;'; $this->css['formLegendClass']='legend'; $this->css['pairFieldValueClass']='value value1'; $this->css['pairFieldLabelClass']='label'; $this->css['rowClass']='row'; $this->css['formMainClass']='form'; $this->css['submit']='submit center'; //JS $this->js['validateFunct']='Invis.tools.checkValue(this.id);'; } public function addLegend($text){ } protected function correctMethod($method) { switch($method){ case 'GET': case 'POST': case 'PUT': return true; } return false; } /** * Append eventHandler to added to form input element * @return integer * @param $name String * @param $event String * @param $handler String */ public function addEventHandler($name,$event,$handler){ if($this->_elems['name']==$name){ break; } } }else{ return IFORM_WRONG_PARAMS; } return 1; } /** * Add submit control to the form * @return void * @param $name Object * @param $value Object * @param $style Object[optional] */ { }else{ return IFROM_WRONG_PARAMS; } } /** * Check that element with value of type $type equals $value * @return integer * @param $type String * @param $value String */ protected function elemExists($type,$value) { switch($type) { case 'name': if($this->_elems[$i]['name']==$value)return 1; break; case 'id': if($this->_elems[$i]['id']==$value)return 1; break; } } return 0; } /** * Set "important" parameter to the $id - input element * @return integer * @param $name String */ public function validateElement($id) { return IFORM_WRONG_PARAMS; }else{ if(!$this->elemExists("id",$id)){ return IFORM_WRONG_ELEM; }else{ //FIX ERROR } } return 1; } /** * Add input element to the current form * @return String * @param $type String * @param $name String * @param $parent String * @param $id String[optional] * @param $value String[optional] * @param $validate Boolean[optional] * @param $params String[optional] */ public function addInput($type,$name,$parent,$id='',$value='',$validate=false,$params='') { 'id'=>$id, 'type'=>$type, 'parent'=>$parent, 'name'=>$name, 'value'=>$value, 'validate'=>$validate, 'params'=>$params ); }else{ return IFROM_WRONG_PARAMS; } return $id; } /** * Add field to the form * @return String * @param $label String * @param $type String[optional] * @param $el String[optional] * @param $id String[optional] * @param $params String[optional] */ public function addField($label,$type="pair",$value="",$id='',$params="") { $this->_fields[]=array("id"=>$id,"label"=>$label,"type"=>$type,"value"=>$value,"type"=>$type,"style"=>$params); return $id; } /** * Initialize system patterns * @return void */ protected function initPatterns() { $this->_patterns['form']='<form action="%s" method="%s" encoding="%s" %s>'; $this->_patterns['inputI']='<input type="%s" name="%s" id="%s" %s %s %s/>'; $this->_patterns['inputD']='<%s name="%s" id="%s" %s %s %s>%s</%s>'; $this->_patterns['fieldsingle']='<div class="%s" style="width:100%;clear:both;" id="%s" %s%s</div>'; $this->_patterns['fieldpair']='<div class="%s" %s id="%s"><span class="%s" style="%s">%s</span><span style="%s">%s</span></div>'; $this->_patterns['formLegend']='<div class="'.$this->css['formLegendClass'].'">%s</div>'; $this->_patterns['mainContainer']='<div class="'.$this->css['formMainClass'].'">'; $this->_patterns['endContainer']='</div></form>'; $this->_patterns['submit']='<div class="row"><span class=" '.$this->css['submitClass'].'" style="clear:both;"><input type="submit" name="%s" value="%s" %s/></span></div>'; } /** * Get inputs which referers to $parent-field * @return String * @param $parent Object */ protected function _getChildsInputs($parent) { $res=''; { if($this->_elems[$i]['parent']==$parent){ switch($this->_elems[$i]['type']){ case 'password': case 'checkbox': case 'text': $this->_patterns['inputI'], $this->_elems[$i]['type'], $this->_elems[$i]['name'], $this->_elems[$i]['id'], ($this->_elems[$i]['validate'])?'onclick="'.$this->js['validateFunct'].'"':'', $this->_getEvents($i), $this->_prepareParams($this->_elems[$i]['params']) ); break; case 'select': $this->_patterns['inputI'], $this->_elems[$i]['type'], $this->_elems[$i]['name'], $this->_elems[$i]['id'], ($this->_elems[$i]['validate'])?'onclick="'.$this->js['validateFunct'].'"':'', $this->_getEvents($i), $this->_prepareParams($this->_elems[$i]['params']) ); break; default: $res=IFORM_WRONG_TYPE; } } } return $res; } /** * * @return String * @param $elId Integer */ protected function _getEvents($elId){ $result=null; foreach($this->_elems[$elId]['on'] as $k=>$v) { $result.=' on'.$k.'="return '.$v.'();" '; } }else{ $result=IFORM_NOT_EXISTS; } return $result; } private function _prepareParams($data) { $res=null; foreach($data as $k=>$v) { $res.=$k."=\"".$data."\""; } } return $res; } /** * * @return String */ public function render($output=true) {//<form action="%s" method="%s" encoding="%s" style="%s" %s> #print_rbr($this->_elems); #die_br($this->_fields); $this->_data=sprintf($this->_patterns['form'],$this->_form['action'],$this->_form['method'],$this->_form['encoding'],$this->_prepareParams($this->_form['params'])); $this->_data.=($this->_form['legend'])?$this->_form['legend']:''; #die($this->_data); {#die_r($this->_fields[$i]); switch($this->_fields[$i]['type']) { case 'pair': //<div class="%s" id="%s"><span class="%s" style="%s">%s</span><span style="%s">%s(value)</span></div>' $this->_patterns['fieldpair'], $this->css['rowClass'], $this->_fields[$i]['params'], $this->_fields[$i]['id'], $this->css['pairFieldLabelClass'], $this->css['pairFieldLabelStyle'], $this->_fields[$i]['label'], $this->css['pairFieldValueClass'], ($this->_fields[$i]['value']=='')?$this->_getChildsInputs($this->_fields[$i]['id']):$this->_fields[$i]['value'] ); break; case 'single': break; } } $this->_data.=$this->_submit; return ($output)?$this->_data:''; } function getData() { return $this->_data; } } ?>