Form Gen.
Ein wahnsinns praktisches Teil wenn mal schnell ein Formular benötigt das auch noch gleich mit validiert werden soll.
|
Script: |
<?php
class form_o_mat
{
var $id; //Form name (to identify if it was send)
var $action; //Where should this form post? (if nothing, it self)
var $caption; //Caption to show the user
var $target = "_self"; //What should the target be?
var $style; //If there is a css class for this form, put the name here
var $enctype; //Will automaticly be set to multipart/form-data if there is a form with a file obj.
var $hide = FALSE; //Shuld the form be hidden if there are no errors in the send data?
var $_error=""; //Intern error trigger
var $_error_msg=""; //Intern error message to be displayed
var $_data=""; //Intern usage for storing the form data
function insert_object( $name, $caption, $needed=FALSE, $type="text", $size="", $value="" )
{
$needed_signal = "";
if( isset( $_POST[$this->id] ) )
{
$this->_check_data( $name, $type, $needed );
$value = $_POST[$name];
}
if( $needed == TRUE )
{
$needed_signal = "* ";
}
$return = '<tr><td valign="top"><b>'. $caption .'</b>'. $needed_signal .'</td><td valign="top">';
$value = htmlspecialchars( $value );
switch( $type )
{
case 'password':
$size = preg_replace( "/\D/i", "", $size );
if( !empty( $size ) )
{
$size = 'size="'. $size .'"';
}
$attribs = 'type="password" '. $size;
if( !empty( $value ) )
{
$value = 'value="'. $value .'"';
}
$return .= '<input name="'. $name .'" '. $attribs .' '. $value .'>';
break;
case 'file':
$this->enctype = 'multipart/form-data';
$size = preg_replace( "/\D/i", "", $size );
if( !empty( $size ) )
{
$size = 'size="'. $size .'"';
}
$attribs = 'type="file" '. $size;
if( !empty( $value ) )
{
$value = 'value="'. $value .'"';
}
$return .= '<input name="'. $name .'" '. $attribs .' '. $value .'>';
break;
case 'textarea':
$size = explode( 'x', $size );
if( !empty( $size[0] ) AND !empty( $size[1] ) )
{
$size = 'cols="'. $size[0] .'" rows="'.$size[1] .'"';
}
$return .= '<textarea name="'. $name .'" '. $size .'">'. $value .'</textarea>';
break;
default:
$size = preg_replace( "/\D/i", "", $size );
if( !empty( $size ) )
{
$size = 'size="'. $size .'"';
}
$attribs = 'type="text" '. $size;
if( !empty( $value ) )
{
$value = 'value="'. $value .'"';
}
$return .= '<input name="'. $name .'" '. $attribs .' '. $value .'>';
}
$return .= '</td><td valign="top">'. $this->_error_msg .'</td></tr>';
$this->_data .= $return;
return true;
}
function insert_object_group( $name, $caption, $value, $needed="", $type="", $description="" )
{
if( isset( $_POST[$this->id] ) )
{
$this->_check_data( $name, $type, $needed );
}
if( $needed == TRUE )
{
$needed_signal = "* ";
}
$return = '<tr><td valign="top"><b>'. $caption .'</b>'. $needed_signal .'</td><td valign="top">';
if( !is_array( $value ) OR ( !empty( $description ) AND !is_array( $description ) ) )
{
return false;
}
switch( $type )
{
case 'radio':
if( empty( $description ) )
{
$description = $value;
}
foreach( $value as $key => $item )
{
if( isset( $_POST[$this->id] ) AND $_POST[$name] == $item )
{
$checked = "checked";
}
else
{
$checked = "";
}
$return .= '<lable><input type="radio" name="'. $name .'" value="'. $item .'" '. $checked .'>'. $description[$key] .'</lable><br/>';
}
break;
case 'check':
if( empty( $description ) )
{
$description = $value;
}
foreach( $value as $key => $item )
{
if( @in_array( $item, $_POST[$name] ) )
{
$checked = "checked";
}
else
{
$checked = "";
}
$return .= '<lable><input type="checkbox" name="'. $name .'[]" value="'. $item .'" '. $checked .'>'. $description[$key] .'</lable><br/>';
}
break;
default:
$return .= '<select name="'. $name .'">';
if( empty( $description ) )
{
$description = $value;
}
$return .= '<option value="">-- select what is true --</option>';
foreach( $value as $key => $item )
{
if( isset( $_POST[$this->id] ) AND $_POST[$name] == $item )
{
$selected = "selected";
}
else
{
$selected = "";
}
$return .= '<option value="'. $item .'" '. $selected .'>'. $description[$key] .'</option>';
}
$return .= '</select>';
}
$return .= '</td><td valign="top">'. $this->_error_msg .'</td></tr>';
$this->_data .= $return;
return true;
}
function _check_data( $name, $type, $needed )
{
if( $type == TRUE AND $needed != FALSE )
{
if( $type == 'check' )
{
$string = "";
$i = "";
if( isset( $_POST[$name] ) )
{
$i = count( $_POST[$name] );
}
while( $i > 0 )
{
$i--;
$string .= 'i';
}
}
else
{
$string = $_POST[$name];
}
$count = strlen( $string );
if( $needed == 1 )
{
if( $count >= $needed )
{
$this->_error_msg = "";
return true;
}
else
{
$this->_error_msg = "this information must be entered";
$this->_error = TRUE;
return false;
}
}
else
{
$needed = explode( "-", $needed );
if( !empty( $needed[1] ) )
{
if( ( $count <= max( $needed[0], $needed[1] ) ) AND ( $count >= min( $needed[0], $needed[1] ) ) )
{
$this->_error_msg = "";
return true;
}
else
{
$this->_error_msg = 'must be '. min( $needed[0], $needed[1] ) .' to '. max( $needed[0], $needed[1] ) .' characters long!';
$this->_error = TRUE;
return false;
}
}
else
{
if( empty( $needed[1] ) )
{
if( $count >= $needed[0] )
{
$this->_error_msg = "";
return true;
}
else
{
$this->_error_msg = 'this information must be atleast '. $needed[0] .' characers long!';
$this->_error = TRUE;
return false;
}
}
}
}
}
else
{
$this->_error_msg = "";
return true;
}
}
function dump_form()
{
$style = "";
if( !empty( $this->style ) )
{
$style = 'class="'. $this->style .'"';
}
if( empty( $this->action ) )
{
$this->action = $_SERVER['REQUEST_URI'];
}
if( !empty( $this->caption ) )
{
$result = '<h2>'. $this->caption .'</h2>';
}
if( !empty( $this->enctype ) )
{
$enctype = 'enctype="'. $this->enctype .'"';
}
$result .= '<table>'. $this->_data .'<tr><td valign="top"> </td><td valign="top">* This information is required<td valign="top"></td></tr></table>';
$return = '<form action="'. $this->action .'" method="post" target="'. $this->target .'" '. $style .'>';
$return .= $result;
$return .= '<input name="'. $this->id .'" type="submit"><input type="reset"></form>';
if( $this->_error == FALSE AND $this->hide == TRUE AND isset( $_POST[$this->id] ) )
{
return true;
}
else
{
return $return;
}
}
}
?>
|
|