Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Simple form builder/validator.

    I've written this simple class as a part of a project I've been doing. It required lots of forms so I quickly saw a need for it.
    PHP Code:
    class form
    {
        var 
    $name '';
        var 
    $action '';
        var 
    $data = array();
        var 
    $error = array();
        var 
    $valid = array();
        var 
    $submit = array();

        function 
    form($name$action)
        {
            
    $this->name $name;
            
    $this->action $action;
            return 
    true;
        }

        function 
    add($name$type$human_value$value false$max false$min false$size false)
        {
            if(isset(
    $this->data[$name]))
            {
                return -
    1;
            }
            if(
    $type == 'submit')
            {
                
    $this->submit[] = $name;
            }
            if(!
    $type != 'select')
            {
                
    $this->data[$name] = array_slice(func_get_args(), 1);
                return 
    true;
            }
            
    $this->data[$name] = array(func_get_arg(1), array_slice(func_get_args(), 1));
            return 
    true;
        }

        function 
    add_option($select$value$human_value false)
        {
            if(!isset(
    $this->data[$select]) || ($this->data[$select][0] != 'select' && $this->data[$select][0] != 'radio'))
            {
                return 
    false;
            }
            
    $this->data[$select][] = array_slice(func_get_args(), 1);
            return 
    true;
        }

        function 
    raw($data)
        {
            
    $this->data[] = array('raw'$data);
            return 
    true;
        }

        function 
    check_submit()
        {
            for(
    $i 0$n count($this->submit); $i $n$i++)
            {
                if(isset(
    $_POST[$this->submit[$i]]))
                {
                    return 
    $this->submit[$i];
                    break;
                }
            }
            return 
    false;
        }

        function 
    get_valid_values()
        {
            
    $ret = array();
            for(
    $i 0$n count($this->valid); $i $n $i++)
            {
                if(
    $this->data[$this->valid[$i]][0] == 'select' || $this->data[$this->valid[$i]][0] == 'radio')
                {
                    
    $ret[$this->valid[$i]] = $this->data[$this->valid[$i]][$_POST[$this->valid[$i]] + 1][0];
                }
                else
                {
                    
    $ret[$this->valid[$i]] = $_POST[$this->valid[$i]];
                }
            }
            return 
    $ret;
        }

        function 
    make($show_errors true)
        {
            
    $ret = array();
            
    $submit $this->check_submit();
            
    $i 1;
            
    $ret[] = '<form name="' $this->name '" action="' $this->action '" method="POST">';
            foreach(
    $this->data as $name => $values)
            {
                
    $ret[$i] = (strlen($values[1]) && $values[0] != 'raw') ? '<label for="' $name '">' $values[1] . ': </label>' '';
                switch(
    $values[0])
                {
                    case 
    'raw':
                        
    $ret[$i] = $values[1];
                    break;
                    case 
    'textarea':
                        
    $ret[$i] .= '<br /><textarea name="' $name '" id="' $name '"' . ((isset($values[5]) && is_array($values[5])) ? 'cols="' $values[5][0] . '" rows="' $values[5][1] . '"' '') . '>' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '</textarea>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'select':
                        
    $ret[$i] .= '<select name="' $name '" id="' $name '">' "\n";
                        for(
    $j 2$m count($values); $j $m$j++)
                        {
                            
    $ret[$i] .= '<option value="' . ($j 1) . '"' . ((isset($_REQUEST[$name]) && ($_REQUEST[$name] == ($j 1))) ? ' selected="selected"' '') . '>' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . '</option>' "\n";
                        }
                        
    $ret[$i] .= '</select>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'radio':
                        for(
    $j 2$m count($values); $j $m$j++)
                        {
                            
    $ret[$i] .= '<input type="radio" name="' $name '" id="' $name . ($j 1) . '"' ' value="' . ($j 1) . '"' . ((isset($_POST[$name]) && ($_POST[$name] == ($j 1))) ? ' checked="checked"' '') . '/><label for="' $name . ($j 1) . '">' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . "</lable>\n";
                        }
                    break;
                    case 
    'checkbox':
                        
    $ret[$i] = '<input type="checkbox" name="' $name '" id="' $name '"' ' value="1"' . ((isset($_POST[$name]) && $_POST[$name]) ? ' checked="checked"' '') . '/><label for="' $name '">' . (isset($values[1]) ? $values[1] : $values[0]) . "</lable>\n";
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'number':
                    case 
    'email':
                        
    $ret[$i] .= '<input type="text" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' . (($values[0] == 'number') ? strlen($values[3]) : $values[3]) . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'submit':
                        
    $ret[$i] = '<input type="submit" name="' $name '" value="' $values[1] . '"/>';
                    break;
                    default:
                        
    $ret[$i] .= '<input type="' $values[0] . '" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' $values[3] . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                }
                
    $i++;
                
    $ret[] = '</form>';
            }
            return 
    $ret;
        }

        function 
    validate($filled false)
        {
            foreach(
    $this->data as $name => $values)
            {
                if(!isset(
    $_POST[$name]))
                {
                    
    $_POST[$name] = '';
                }

                
    $_POST[$name] = trim($_POST[$name]);
                
    $_POST[$name] = (get_magic_quotes_gpc()) ? stripslashes($_POST[$name]) : $_POST[$name];

                if(
    is_array($filled) && in_array($name$filled) && !strlen($_POST[$name]))
                {
                    
    $this->error[$name] = 'Value must be set.';
                    continue;
                }
                switch(
    $values[0])
                {
                    case 
    'raw':
                    break;
                    case 
    'number':
                        if(!
    is_numeric($_POST[$name]))
                        {
                            
    $this->error[$name] = 'Value must be a number.';
                        }
                        if((isset(
    $values[3]) && $values[3] !== false && (float) $_POST[$name] > (float) $values[3]) || (isset($values[4]) && $values[4] !== false && (float) $_POST[$name] < (float) $values[4]))
                        {
                            
    $this->error[$name] = 'Value is too big/small. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' $values[3] . '.';
                        }
                    break;
                    case 
    'select':
                    case 
    'radio':
                        if(!
    is_numeric($_POST[$name]))
                        {
                            
    $this->error[$name] = 'Unpredicted value.';
                        }
                    break;
                    case 
    'email':
                        if(!
    preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/'$_POST[$name]))
                        {
                            
    $this->error[$name] = 'Value is not an email address.';
                        }
                    break;
                    default:
                        if((isset(
    $values[3]) && $values[3] !== false && strlen($_POST[$name]) >= $values[3]) || (isset($values[4]) && $values[4] !== false && strlen($_POST[$name]) <= $values[4]))
                        {
                            
    $this->error[$name] = 'Value is too long/short. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' $values[3] . ' characters long.';
                        }
                    break;
                }
                if(!isset(
    $this->error[$name]))
                {
                    
    $this->valid[] = $name;
                }
            }
            if(
    count($this->error))
            {
                return 
    $this->error;
            }
            return 
    true;
        }

    Ok that's the class and now how to use it:
    PHP Code:
    $form = new form('form name''file the form should submit to'); 
    Both arguments are required for the constructor.
    It returns true.

    To add a form element you use the add() method.
    PHP Code:
    $form->add('elenent name''element type''description''default value'/* maximum length */ 10/* minimum length */ 0/* size */ 10); 
    Arguments after "description" are optional. The last three should be either int or float. If the type is "textarea" the $size argument should be a two element array (width, height).
    The types are: text, password, textarea, radio, checkbox, select, number*, email and submit.
    It returns bool(true) or int(-1) if an element with the same name exists.

    To add options to the select or radio element you use the add_option() method.
    PHP Code:
    $form->add_option('name of the select element to which this option belongs''the returned value''the value presented to the user'); 
    The last argument is optional. The value of the 2nd argument will be used if it is not set.
    It returns bool(true) or bool(false) if there is no select element with the given name.

    The make() method returns an array with the form. It return an array so you can add whatever you like inbetween the fields.
    PHP Code:
    $form->make(/* show errors */ true); 
    The argument is optional and defaults to true. It will print errors below the form elemnts that were filled in incorrectly.

    The validate() method validates the form.
    PHP Code:
    $form->validate(array('these''are''the''names''of elemnts''that''must be''filled in')); 
    The argument is optional. It must be an array.
    It returns bool(true) or an array with all the errors.

    The check_submit() method checks if a form has been submited.
    PHP Code:
    if(false !== $form->check_submit())
    {
       print 
    'Form was submitted';

    It returns the name of the submit button user or false.

    The get_valid_values() method returns an associative array with all the values that passed the validation.
    PHP Code:
    var_dump($form->get_valid_values()); 
    To add something between the form elements and not have to play with the array returned by form::make() you can add some raw HTML with the raw() method.
    PHP Code:
    $form->raw('Some HTML'); 
    An example:
    PHP Code:
    $form = new form('upload'$_SERVER['PHP_SELF']);
    $form->add('filename''text''Name'false100);
    $form->add('comment''textarea''Comments''Put comments here'102, array(10020));
    $form->add('guess''number''Guess a numer from 1 to 10'false1012);
    $form->add('order''select''Sort order');
    $form->raw('Hello there!<br>');
    $form->add_option('order''ASC''Ascending');
    $form->add_option('order''DESC''Descending');
    $form->add('email''email''eMail'false100);
    $form->add('choice''radio''Are you sure?');
    $form->add_option('choice''he is stupid''No');
    $form->add_option('choice''he is smart''Yes');
    $form->add('agreement''checkbox''I agree to be good');
    $form->add('submit''submit''Upload');
    if(
    $form->check_submit())
    {
        if(
    true === $form->validate(array('filename''agreement')))
        {
            print 
    'Form is filled out correctly.';
        }
    }
    print 
    implode("<br />\n"$form->make());
    var_dump($form->get_valid_values()); 
    I may update this if needed.

    NOTES:
    It does not support arrays (element names with []). Then again it might... to some extent... It was not needed.

    *The number type is validated by the size of the number not by the length.
    Last edited by marek_mar; 12-08-2005 at 04:10 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice! Very useful. This will definitely come in handy.
    "$question = ( to() ) ? be() : ~be();"

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Velox Letum
    This will definitely come in handy.
    I can't quite remember, but I think that was a reason why I've written it.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quite :P

    For one, it'll certainly simplify form validation and remove the large blocks of form html from between conditionals...
    "$question = ( to() ) ? be() : ~be();"

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks nice, but needs some additional things IMO.

    The use of <label> for each form element, which would also require each form element to have a unique id, would enchance this class. Plus it means I may be able to use :-)

    Not only that but if the functionality to add a fieldset/legend around a group of form inputs was added it would also be extremely useful.
    my mind is on a permanent tangent

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The class got updated.
    There is a new size argument for the add() method.
    Checkboxes got added (sort of).
    Lables were added to radio and checkbox elemnt types (Do the other need them?).
    The return values changed in the check_submit() method.
    The example was updated aswell.

    Everything which goes around any the form elemnts was left for you to add. That's why the form is returned in an array.
    Last edited by marek_mar; 12-05-2005 at 12:32 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Location
    Liverpool
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    Lables were added to radio and checkbox elemnt types (Do the other need them?).
    yep fraid so.
    Everything which goes around any the form elemnts was left for you to add. That's why the form is returned in an array.
    ahh my bad.
    my mind is on a permanent tangent

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Updated it again.
    - Everything has a label now.
    - A new method was added (form::raw()) to add things between the form elemnts (if you're too lazy to do it with the array that form::make() returns). Anything there will not be validated even if it's a form element. Adding form elements with raw is not suggested as they may break other form elements.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nice. what about html classes?

    That's cool, but it's pretty standard for me to use classes on inputs to make up for IE's lame CSS2 selector support (such as input[type='text'] {....}).
    the ones I use every time are:
    Code:
    <input type="text" class="txt" ........ />
    <input type="submit" class="btn" ....... />
    So how about an optional class attrib?

    Personally, I'd also move all the add() params to an object literal. That's just preference though, not really a technical consideration

    I take no responsibility for the above nonsense.


    Left Justified

  • #10
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Ok I'll make it add class="elent-type".
    And what was that about the params?
    I can't esit my first post anymore :/
    PHP Code:
    class form
    {
        var 
    $name '';
        var 
    $action '';
        var 
    $data = array();
        var 
    $error = array();
        var 
    $valid = array();
        var 
    $submit = array();

        function 
    form($name$action)
        {
            
    $this->name $name;
            
    $this->action $action;
            return 
    true;
        }

        function 
    add($name$type$human_value$value false$max false$min false$size false)
        {
            if(isset(
    $this->data[$name]))
            {
                return -
    1;
            }
            if(
    $type == 'submit')
            {
                
    $this->submit[] = $name;
            }
            if(!
    $type != 'select')
            {
                
    $this->data[$name] = array_slice(func_get_args(), 1);
                return 
    true;
            }
            
    $this->data[$name] = array(func_get_arg(1), array_slice(func_get_args(), 1));
            return 
    true;
        }

        function 
    add_option($select$value$human_value false)
        {
            if(!isset(
    $this->data[$select]) || ($this->data[$select][0] != 'select' && $this->data[$select][0] != 'radio'))
            {
                return 
    false;
            }
            
    $this->data[$select][] = array_slice(func_get_args(), 1);
            return 
    true;
        }

        function 
    raw($data)
        {
            
    $this->data[] = array('raw'$data);
            return 
    true;
        }

        function 
    check_submit()
        {
            for(
    $i 0$n count($this->submit); $i $n$i++)
            {
                if(isset(
    $_POST[$this->submit[$i]]))
                {
                    return 
    $this->submit[$i];
                    break;
                }
            }
            return 
    false;
        }

        function 
    get_valid_values()
        {
            
    $ret = array();
            for(
    $i 0$n count($this->valid); $i $n $i++)
            {
                if(
    $this->data[$this->valid[$i]][0] == 'select' || $this->data[$this->valid[$i]][0] == 'radio')
                {
                    
    $ret[$this->valid[$i]] = $this->data[$this->valid[$i]][$_POST[$this->valid[$i]] + 1][0];
                }
                else
                {
                    
    $ret[$this->valid[$i]] = $_POST[$this->valid[$i]];
                }
            }
            return 
    $ret;
        }

        function 
    make($show_errors true)
        {
            
    $ret = array();
            
    $submit $this->check_submit();
            
    $i 1;
            
    $ret[] = '<form name="' $this->name '" action="' $this->action '" method="POST">';
            foreach(
    $this->data as $name => $values)
            {
                
    $ret[$i] = (strlen($values[1]) && $values[0] != 'raw') ? '<label for="' $name '">' $values[1] . ': </label>' '';
                switch(
    $values[0])
                {
                    case 
    'raw':
                        
    $ret[$i] = $values[1];
                    break;
                    case 
    'textarea':
                        
    $ret[$i] .= '<br /><textarea name="' $name '" class="textarea" id="' $name '"' . ((isset($values[5]) && is_array($values[5])) ? 'cols="' $values[5][0] . '" rows="' $values[5][1] . '"' '') . '>' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '</textarea>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'select':
                        
    $ret[$i] .= '<select name="' $name '"  class="select" id="' $name '">' "\n";
                        for(
    $j 2$m count($values); $j $m$j++)
                        {
                            
    $ret[$i] .= '<option value="' . ($j 1) . '"' . ((isset($_REQUEST[$name]) && ($_REQUEST[$name] == ($j 1))) ? ' selected="selected"' '') . '>' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . '</option>' "\n";
                        }
                        
    $ret[$i] .= '</select>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'radio':
                        for(
    $j 2$m count($values); $j $m$j++)
                        {
                            
    $ret[$i] .= '<input type="radio" class="radio" name="' $name '" id="' $name . ($j 1) . '"' ' value="' . ($j 1) . '"' . ((isset($_POST[$name]) && ($_POST[$name] == ($j 1))) ? ' checked="checked"' '') . '/><label for="' $name . ($j 1) . '">' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . "</lable>\n";
                        }
                    break;
                    case 
    'checkbox':
                        
    $ret[$i] = '<input type="checkbox" class="checkbox" name="' $name '" id="' $name '"' ' value="1"' . ((isset($_POST[$name]) && $_POST[$name]) ? ' checked="checked"' '') . '/><label for="' $name '">' . (isset($values[1]) ? $values[1] : $values[0]) . "</lable>\n";
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'number':
                    case 
    'email':
                        
    $ret[$i] .= '<input type="text" class="text" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' . (($values[0] == 'number') ? strlen($values[3]) : $values[3]) . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'submit':
                        
    $ret[$i] = '<input type="submit" class="text" name="' $name '" value="' $values[1] . '"/>';
                    break;
                    default:
                        
    $ret[$i] .= '<input type="' $values[0] . '" class="' $values[0] . '" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' $values[3] . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                }
                
    $i++;
                
    $ret[] = '</form>';
            }
            return 
    $ret;
        }

        function 
    validate($filled false)
        {
            foreach(
    $this->data as $name => $values)
            {
                if(!isset(
    $_POST[$name]))
                {
                    
    $_POST[$name] = '';
                }

                
    $_POST[$name] = trim($_POST[$name]);
                
    $_POST[$name] = (get_magic_quotes_gpc()) ? stripslashes($_POST[$name]) : $_POST[$name];

                if(
    is_array($filled) && in_array($name$filled) && !strlen($_POST[$name]))
                {
                    
    $this->error[$name] = 'Value must be set.';
                    continue;
                }
                switch(
    $values[0])
                {
                    case 
    'raw':
                    break;
                    case 
    'number':
                        if(!
    is_numeric($_POST[$name]))
                        {
                            
    $this->error[$name] = 'Value must be a number.';
                        }
                        if((isset(
    $values[3]) && $values[3] !== false && (float) $_POST[$name] > (float) $values[3]) || (isset($values[4]) && $values[4] !== false && (float) $_POST[$name] < (float) $values[4]))
                        {
                            
    $this->error[$name] = 'Value is too big/small. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' $values[3] . '.';
                        }
                    break;
                    case 
    'select':
                    case 
    'radio':
                        if(!
    is_numeric($_POST[$name]))
                        {
                            
    $this->error[$name] = 'Unpredicted value.';
                        }
                    break;
                    case 
    'email':
                        if(!
    preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/'$_POST[$name]))
                        {
                            
    $this->error[$name] = 'Value is not an email address.';
                        }
                    break;
                    default:
                        if((isset(
    $values[3]) && $values[3] !== false && strlen($_POST[$name]) >= $values[3]) || (isset($values[4]) && $values[4] !== false && strlen($_POST[$name]) <= $values[4]))
                        {
                            
    $this->error[$name] = 'Value is too long/short. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' $values[3] . ' characters long.';
                        }
                    break;
                }
                if(!isset(
    $this->error[$name]))
                {
                    
    $this->valid[] = $name;
                }
            }
            if(
    count($this->error))
            {
                return 
    $this->error;
            }
            return 
    true;
        }

    Last edited by marek_mar; 12-12-2005 at 07:22 AM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #11
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Ok I did a big update to this class (mostly bugfixes though).
    - Label tags are SPELLED correctly this time.
    - Fixed the way checkbox values are returned by form::get_valid_values().
    - You can now select checked/selected by default radio/option.
    - File field has been added ($max argument checks for the filesize).
    - Probably some other details I don'tremember anymore.
    PHP Code:
    class form
    {
        var 
    $name '';
        var 
    $action '';
        var 
    $data = array();
        var 
    $error = array();
        var 
    $valid = array();
        var 
    $submit = array();

        function 
    form($name$action)
        {
            
    $this->name $name;
            
    $this->action $action;
            return 
    true;
        }

        function 
    add($name$type$human_value$value false$max false$min false$size false)
        {
            if(isset(
    $this->data[$name]))
            {
                return -
    1;
            }
            if(
    $type == 'submit')
            {
                
    $this->submit[] = $name;
            }
            if(!
    $type != 'select')
            {
                
    $this->data[$name] = array_slice(func_get_args(), 1);
                return 
    true;
            }
            
    $this->data[$name] = array(func_get_arg(1), array_slice(func_get_args(), 1));
            return 
    true;
        }

        function 
    add_option($select$value$human_value false$default false)
        {
            if(!isset(
    $this->data[$select]) || ($this->data[$select][0] != 'select' && $this->data[$select][0] != 'radio'))
            {
                return 
    false;
            }
            
    $this->data[$select][] = array_slice(func_get_args(), 1);
            return 
    true;
        }

        function 
    raw($data)
        {
            
    $this->data[] = array('raw'$data);
            return 
    true;
        }

        function 
    check_submit()
        {
            for(
    $i 0$n count($this->submit); $i $n$i++)
            {
                if(isset(
    $_POST[$this->submit[$i]]))
                {
                    return 
    $this->submit[$i];
                    break;
                }
            }
            return 
    false;
        }

        function 
    get_valid_values()
        {
            
    $ret = array();
            for(
    $i 0$n count($this->valid); $i $n $i++)
            {
                if(
    $this->data[$this->valid[$i]][0] == 'select' || $this->data[$this->valid[$i]][0] == 'radio')
                {
                    
    $ret[$this->valid[$i]] = $this->data[$this->valid[$i]][$_POST[$this->valid[$i]] + 1][0];
                }
                elseif(
    $this->data[$this->valid[$i]][0] == 'file')
                {
                    if(isset(
    $_FILES[$name]) && $_FILES[$name]['error'] == 4)
                    {
                        continue;
                    }
                    
    $ret[$this->valid[$i]] = $_FILES[$this->valid[$i]];
                }
                elseif(
    $this->data[$this->valid[$i]][0] == 'checkbox')
                {
                    if(
    $_POST[$this->valid[$i]] === '1')
                    {
                        
    $ret[$this->valid[$i]] = $_POST[$this->valid[$i]];
                    }
                }
                else
                {
                    
    $ret[$this->valid[$i]] = $_POST[$this->valid[$i]];
                }
            }
            return 
    $ret;
        }

        function 
    make($show_errors true)
        {
            
    $ret = array();
            
    $submit $this->check_submit();
            
    $i 1;
            
    $ret[] = '<form name="' $this->name '" action="' $this->action '" method="POST" enctype="multipart/form-data">';
            foreach(
    $this->data as $name => $values)
            {
                
    $ret[$i] = (strlen($values[1]) && $values[0] != 'raw') ? '<label for="' $name '">' $values[1] . ': </label>' '';
                switch(
    $values[0])
                {
                    case 
    'raw':
                        
    $ret[$i] = $values[1];
                    break;
                    case 
    'textarea':
                        
    $ret[$i] .= '<br /><textarea name="' $name '" class="textarea" id="' $name '"' . ((isset($values[5]) && is_array($values[5])) ? 'cols="' $values[5][0] . '" rows="' $values[5][1] . '"' '') . '>' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '</textarea>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'select':
                        
    $ret[$i] .= '<select name="' $name '"  class="select" id="' $name '">' "\n";
                        for(
    $j 2$m count($values); $j $m$j++)
                        {
                            
    $selected false;
                            
    $select '';

                            if(isset(
    $_REQUEST[$name]) && $_REQUEST[$name] == ($j 1))
                            {
                                
    $select ' selected="selected"';
                                
    $selected true;
                            }
                            if(!
    $selected && $values[$j][2])
                            {
                                
    $select ' selected="selected"';
                            }
                            
    $ret[$i] .= '<option value="' . ($j 1) . '"' $select '>' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . '</option>' "\n";
                        }
                        
    $select '';
                        
    $selected false;
                        
    $ret[$i] .= '</select>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'radio':
                        for(
    $j 2$m count($values); $j $m$j++)
                        {
                            
    $selected false;
                            
    $select '';
                            if(isset(
    $_REQUEST[$name]) && $_REQUEST[$name] == ($j 1))
                            {
                                
    $select ' checked="checked"';
                                
    $selected true;
                            }
                            if(!
    $selected && $values[$j][2])
                            {
                                
    $select ' checked="checked"';
                            }
                            
    $ret[$i] .= '<input type="radio" class="radio" name="' $name '" id="' $name . ($j 1) . '"' ' value="' . ($j 1) . '"' $select '/><label for="' $name . ($j 1) . '">' . (isset($values[$j][1]) ? $values[$j][1] : $values[$j][0]) . "</label>\n";
                        }
                        
    $select '';
                        
    $selected false;
                    break;
                    case 
    'checkbox':
                        
    $ret[$i] = '<input type="checkbox" class="checkbox" name="' $name '" id="' $name '"' ' value="1"' . ((isset($_POST[$name]) && $_POST[$name]) ? ' checked="checked"' '') . '/><label for="' $name '">' . (isset($values[1]) ? $values[1] : $values[0]) . "</label>\n";
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'number':
                    case 
    'email':
                        
    $ret[$i] .= '<input type="text" class="text" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' . (($values[0] == 'number') ? strlen($values[3]) : $values[3]) . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'file':
                        
    $ret[$i] .= '<input type="' $values[0] . '" class="' $values[0] . '" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' $values[3] . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                    case 
    'submit':
                        
    $ret[$i] = '<input type="submit" class="text" name="' $name '" value="' $values[1] . '"/>';
                    break;
                    default:
                        
    $ret[$i] .= '<input type="' $values[0] . '" class="' $values[0] . '" name="' $name '" id="' $name '"' . (((isset($values[2]) && $values[2]) || $submit) ? ' value="' . (($submit && isset($_POST[$name])) ? $_POST[$name] : $values[2]) . '"' '') . ((isset($values[5]) && $values[5]) ? ' size="' $values[5] . '"' '') . ((isset($values[3]) && $values[3]) ? ' maxlength="' $values[3] . '"' '') . '/>';
                        
    $ret[$i] .= ($show_errors && isset($this->error[$name])) ? "<br />\n{$this->error[$name]}\n" '';
                    break;
                }
                
    $i++;
                
    $ret[] = '</form>';
            }
            return 
    $ret;
        }

        function 
    validate($filled false)
        {
            foreach(
    $this->data as $name => $values)
            {
                if(!isset(
    $_POST[$name]))
                {
                    
    $_POST[$name] = '';
                }

                
    $_POST[$name] = trim($_POST[$name]);
                
    $_POST[$name] = (get_magic_quotes_gpc()) ? stripslashes($_POST[$name]) : $_POST[$name];

                if(
    is_array($filled) && in_array($name$filled) && ($values[0] != 'file' && !strlen($_POST[$name])))
                {
                    
    $this->error[$name] = 'Value must be set.';
                    continue;
                }
                switch(
    $values[0])
                {
                    case 
    'raw':
                    break;
                    case 
    'number':
                        if(!
    is_numeric($_POST[$name]))
                        {
                            
    $this->error[$name] = 'Value must be a number.';
                        }
                        if((isset(
    $values[3]) && $values[3] !== false && (float) $_POST[$name] > (float) $values[3]) || (isset($values[4]) && $values[4] !== false && (float) $_POST[$name] < (float) $values[4]))
                        {
                            
    $this->error[$name] = 'Value is too big/small. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' $values[3] . '.';
                        }
                    break;
                    case 
    'select':
                    case 
    'radio':
                        if(!
    is_numeric($_POST[$name]))
                        {
                            
    $this->error[$name] = 'Unpredicted value.';
                        }
                    break;
                    case 
    'email':
                        if(!
    preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/'$_POST[$name]))
                        {
                            
    $this->error[$name] = 'Value is not an email address.';
                        }
                    break;
                    case 
    'file':
                        if(
    in_array($name$filled) && isset($_FILES[$name]) && $_FILES[$name]['error'] == 4)
                        {
                            
    $this->error[$name] = 'Value must be set.';
                            break;
                        }
                        if((isset(
    $values[3]) && $values[3] !== false && $_FILES[$name]['size'] >= $values[3]))
                        {
                            
    $this->error[$name] = 'File is too large. The size limit is ' . ($values[3] / 1024) . 'kB.';
                        }
                    break;
                    default:
                        if((isset(
    $values[3]) && $values[3] !== false && strlen($_POST[$name]) >= $values[3]) || (isset($values[4]) && $values[4] !== false && strlen($_POST[$name]) <= $values[4]))
                        {
                            
    $this->error[$name] = 'Value is too long/short. Value should be between ' . ((isset($values[4])) ? $values[4] : '0') . ' and ' $values[3] . ' characters long.';
                        }
                    break;
                }
                if(!isset(
    $this->error[$name]))
                {
                    
    $this->valid[] = $name;
                }
            }
            if(
    count($this->error))
            {
                return 
    $this->error;
            }
            return 
    true;
        }

    Have fun building your forms with it.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #12
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the very useful form class. One tweak, in the validation section i needed to change:

    PHP Code:
    if(is_array($filled) && in_array($name$filled) && ($values[0] != 'file' && !strlen($_POST[$name]))) 
    to

    PHP Code:
    if(is_array($filled) && in_array($name$filled) && ($values[0] != 'file' && $values[0] != 'raw' && !strlen($_POST[$name]))) 
    so adding - && $values[0] != 'raw'

    this is in order to prevent 'raw' elements being counted as elements to be validated - thus preventing the form from being validated.
    Last edited by samudradaka; 01-24-2011 at 08:04 AM.

  • #13
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    does any one of you admins know what's up with user marek mar, initiator of this class?
    I tried to pass over a PM to him but his PM storage seems to be overloaded already.
    Last activity was about two years ago.

    Just wonder if anyone knows something as I would like to contact him due to license questions.

    Kind regards,
    Stefan

  • #14
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Esteffan View Post
    Hello,
    Just wonder if anyone knows something as I would like to contact him due to license questions.
    License? I guess this piece of code can be used without any restrictions otherwise he wouldn't paste it here to help us.

    []s


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •