z recji, że pisze pewien projekcik w ktory bede musial stworzyc kilka duuuzych formularzy napisalem pewna klase do ich generowania:
http://nie.bbgroup.pl/test/
na dole stronki
index.php - czyli wywolanie klasy i przygotowanie formularza:
<?php require_once('functions/functions.php'); function puste($t){ } $smarty = new Smarty; $smarty -> template_dir = './tpl'; $smarty -> compile_dir = './tpl_c'; $smarty -> cache_dir = './tpl_cache'; $smarty -> config_dir = './tpl_configs'; $form = new form('formularz_testowy', 'post', true, $_POST); /* rozpoczynanie formularza */ $formularz_testowy[begin] = $form -> formBegin(); /* generowanie pola typu input */ $formularz_testowy[login] = $form -> genInput('puste','text', 'login', $_POST[formularz_testowy][login], 'test', 'Pole Login nie mo�e by� puste', 'a tutaj na przyklad jakies javascriptowegowienko'); /* za pomoca genInput generujemy tez pole typu checbox ;) */ $formularz_testowy[checked] = $form -> genInput('puste','checkbox', 'sex', $_POST[formularz_testowy][sex], '', 'teeeeeeeeeeeeeeerreeeeeeee', ''); $formularz_testowy[wyslij] = $form -> genInput('', 'submit', 'go', 'wy�lij', ''); /* generowanie pola typu texarea w warto�� $line[4] podajemy np <p>%</p> wtedy tam gdzie % zostanie wstawione pole <input type="radio" ..... taka o dekoracyjna funkcyjka jakby trzeba bylo dodac jakies <br> albo <p> przy radiodobry jestem co nie ;p
*/ $formularz_testowy[tresc] = $form -> genTextarea('puste', 'tresc', $_POST[formularz_testowy][tresc], 'as', 'Tre�� nie mo�e by� pusta', ''); /* generowanie pola typu radio */ /* 0 - value, 1 - class, 2 - add(jakas java), 3 - checked, 4 - decorate */ $formularz_testowy[radio] = $form -> genRadio('', 'radio', $items, 'wiadomosc bledu'); /* generowanie pola typu select */ /* 0 - value, 1 - tresc, 2 - class , 3 - add(jakas java), 4 - selected */ $formularz_testowy[select] = $form -> genSelect('puste', 'select', 'class', $items, 'wiadomosc bledu'); /* przes�anie ewentualnych b��d�w */ $formularz_testowy[errors] = $form -> errorMsg; /* musi byc wywolane jako ostatnie */ if($form -> parse){ } else{ $smarty -> assign('formularz_testowy', $formularz_testowy); } $smarty -> display('index.tpl'); ?>
form.class.php - klasa formularza
<?php class form{ var $form_id; var $method; var $debug; var $data; var $parse = false; $this -> form_id = $form_id; $this -> method = $method; $this -> debug = $debug; $this -> data = $data; } function formBegin($class =''){ $form_begin = '<form id="'.$this -> form_id.'" method="'.$this -> method.'" '.($class ? 'class="'.$class.'"' : '').'>'; return $form_begin; } function genInput($checkFunction = '',$type, $name, $value = '', $class = '', $error_msg= '', $add =''){ if($checkFunction($this -> data[$this -> form_id][$name])){ $this -> errorMsg[] = $error_msg; $error = 'style="border: 1px solid red"'; } } if($type == 'checkbox'){ $input = '<input type="'.$type.'" name="'.$this -> form_id.'['.$name.']" '.($value == 'on' ? 'checked' : '').' '.($class ? 'class="'.$class.'"' : '').' '.$add.' '.$error.'/>'; } else{ $input = '<input type="'.$type.'" name="'.$this -> form_id.'['.$name.']" '.($value ? 'value="'.$value.'"' : '').''.($class ? 'class="'.$class.'"' : '').' '.$add.' '.$error.'/>'; } return $input; } function genTextarea($checkFunction = '', $name, $value ='', $class ='', $error_msg = '', $add = ''){ if($checkFunction($this -> data[$this -> form_id][$name])){ $this -> errorMsg[] = $error_msg; $error = 'style="border: 1px solid red"'; } } $textarea = '<textarea name="'.$this -> form_id.'['.$name.']" '.($class ? 'class="'.$class.'"' : '').' '.$add.' '.$error.'>'.($value ? ''.$value.'' : '').'</textarea>'; return $textarea; } function genRadio($checkFunction = '', $name,$items , $error_msg = ''){ if($checkFunction($this -> data[$this -> form_id][$name])){ $this -> errorMsg[] = $error_msg; $error = 'style="border: 1px solid red"'; } } $radio = ''; foreach ($items as $a => $lines){ $radio .= '<input type="radio" name="'.$this -> form_id.'['.$name.']" value="'.$lines[0].'" '.(!empty($lines[1]) ? 'class="'.$lines[1].'"' : 'as').' '.$lines[3].' '.$error.'/>'; } else { $radio .= str_replace('%','<input type="radio" name="'.$this -> form_id.'['.$name.']" value="'.$lines[0].'" '.($lines[1] ? 'class="'.$lines[1].'"' : '').' '.$lines[3].' '.$error.'/>', $lines[4]); } } return $radio; } function genSelect($checkFunction,$name, $class,$items, $error_msg = ''){ if($checkFunction($this -> data[$this -> form_id][$name])){ $this -> errorMsg[] = $error_msg; $error = 'style="border: 1px solid red"'; } } $select = '<select name="'.$this -> form_id.'['.$name.']" '.$error.'>'; foreach ($items as $a => $lines){ $select .= '<option value="'.$lines[0].'" '.($lines[0] ? 'class="'.$lines[0].'"' : '').' '.$lines[3].' '.$lines[4].'>'.$lines[1].'</option>'; } $select .= '</select>'; return $select; } function formEnd(){ $form_end = '</form>'; $this -> parse = true; } return $form_end; } } ?>
index.tpl - czyli umieszczenie formularza na www
wszelkie opinie mile widziane
