witam
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" .....
*/
$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 */
$items[] = array('pierwsza', 'class', '', 'jakis java', '%<br />'); $items[] = array('drua', 'aasd', '', 'checked', '%<hr>'); $items[] = array('pierwsza', 'asd', ''); $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 */
$items[] = array('', '-= wybierz =-', '', ''); $items[] = array('1', 'tresc', 'asd','', 'selected'); $items[] = array('2', 'tresc 1', ''); $items[] = array('3', 'tresc 2', ''); $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 */
$formularz_testowy[end] = $form -> formEnd
();
if($form -> parse){
echo "formulaz przeszed� test prawid�owo"; }
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;
function __construct
($form_id, $method, $debug, $data = array()){ $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($this -> debug
&& !empty($this -> data
) && !empty($checkFunction)){ 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($this -> debug
&& !empty($this -> data
) && !empty($checkFunction)){ 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($this -> debug
&& !empty($this -> data
) && !empty($checkFunction)){ 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($this -> debug
&& !empty($this -> data
) && !empty($checkFunction)){ 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>';
if(empty($this -> errorMsg
) && !empty($this -> data
)){ $this -> parse = true;
}
return $form_end;
}
}
?>
index.tpl - czyli umieszczenie formularza na www
{if $formularz_testowy}
{$formularz_testowy.begin}
{if $formularz_testowy.errors}
{section name=a loop=$formularz_testowy.errors}
<p style="color:red">{$formularz_testowy.errors[a]}
</p> {/section}
{/if}
<td>{$formularz_testowy.login}
</td>
<td>{$formularz_testowy.tresc}
</td> <td>{$formularz_testowy.wyslij}
</td> <td>{$formularz_testowy.radio}
</td> <td>{$formularz_testowy.select}
</td> <td>{$formularz_testowy.checked}
</td> {$formularz_testowy.end}
{/if}
wszelkie opinie mile widziane (IMG:
http://forum.php.pl/style_emoticons/default/smile.gif)
Ten post edytował AcidBurnt 19.05.2006, 16:48:40