SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form validation in configuration files?

    Hello. I like to store the form validation details in flat files. I am having a little dilemma though.

    I am trying to come up with some simple and easy to understand syntax for use in the config files.

    So far, I am using something like this:

    Code:
    username:
        minlength  = 3
        maxlength  = 255
        type       = 
    password:
        minlength  = 3
        maxlength  = 255
        type       = 
    passwordAgain:
        minlength  = 3
        maxlength  = 255
        type       = 
        match      = password
    email:
        minlength  = 3
        maxlength  = 255
        type       = email
    emailAgain:
        minlength  = 3
        maxlength  = 255
        type       = email
        match      = email
    birthdate:
        type       = date
    Do you think this is good approach. If not, how would you do it?

    The lines without indentation are fieldnames and the lines with indentation are attributes and values of the fieldname. So for example:

    Code:
    email:
        minlength  = 3
        maxlength  = 255
        type       = email
    Says that $_POST['email'] field submitted from form should be between 3 and 255 characters long and its type should be email.

    Then, in PHP code, it would look like this:

    PHP Code:
    $form = new Form(); // initialize the validation class

    // validate the form submitted with $_POST['register'] button
    // this will return null if the form hasn't been submitted
    $isValid $form->validate('register');

    if (isset(
    $isValid))
    {
        if (
    $isValid)
        {
            
    // If the submitted data is all valid
        
    }


  2. #2
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use json_encode()/json_decode() or something like spyc (google "spyc" for more info). These are something like de facto standards for config languages. Then again, there's always XML.

    To be honest though, I'd rather configure everything using PHP code. First, there's no translation step, so it's faster. Second, if you use a decent IDE, you can take advantage of the autocomplete features. This comes in handy when you have options that vary, especially if you're working as part of a team, since the options present themselves rather than having to be looked up somewhere.

  3. #3
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that form validation is only one kind of data validation. You should describe all characteristics in data dictionary and use it not only for validation, but for SQL generating, backups and so on.

    Kenneth Downs wrote a few good articles about this topic with examples in YAML:
    data dictionary - http://database-programmer.blogspot....ictionary.html
    all articles - http://database-programmer.blogspot....-contents.html

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    I think that form validation is only one kind of data validation. You should describe all characteristics in data dictionary and use it not only for validation, but for SQL generating, backups and so on.

    Kenneth Downs wrote a few good articles about this topic with examples in YAML:
    data dictionary - http://database-programmer.blogspot....ictionary.html
    all articles - http://database-programmer.blogspot....-contents.html
    Well that is a bit impractical don't you think? I would have to change the data dictionary at each website with different database structure.

    I am looking just for something simple and portable for forms that could work without making any changes to it everywhere.

  5. #5
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you prefer more schemas for one database, go on.

    I would have to change the data dictionary at each website with different database structure.

    Now you have to change the configuration file for each form

  6. #6
    SitePoint Zealot
    Join Date
    May 2008
    Location
    Montreal
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want it stored in a text format then why not choose INI files? PHP has built-in functions to parse the files. Your text would thus look like:
    Code:
    [username]
    minlength = 3
    maxlength = 255
    type = 
    
    [password]
    minlength = 3
    maxlength = 255
    type =

  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Peter Goodman View Post
    If you want it stored in a text format then why not choose INI files? PHP has built-in functions to parse the files. Your text would thus look like:
    Code:
    [username]
    minlength = 3
    maxlength = 255
    type = 
    
    [password]
    minlength = 3
    maxlength = 255
    type =
    Oh that's a brilliant idea, I am really dumb not to think of that. I was parsing the files with fopen() etc...

  8. #8
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more question. Is this how the validation should look in PHP?

    PHP Code:
    $form = new Form(); // initialize the validation class 

    // validate the form submitted with $_POST['register'] button 
    // this will return null if the form hasn't been submitted 
    $isValid $form->validate('register'); 

    if (isset(
    $isValid)) 

        if (
    $isValid
        { 
            
    // If the submitted data is all valid 
        



  9. #9
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you don't need to check if the variable isset and check its value. Its just repetition. if $form->validate('register') returns a boolean value then if($isValid) is sufficient. Since you've already set the variable, you know that it isset so there's no point in including that line.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  10. #10
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    you don't need to check if the variable isset and check its value. Its just repetition. if $form->validate('register') returns a boolean value then if($isValid) is sufficient. Since you've already set the variable, you know that it isset so there's no point in including that line.
    Actually my $form->validate('register') method returns null when the form has not been submitted, when it has been submitted it returns true or false. That's why there is the isset.

  11. #11
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    Actually my $form->validate('register') method returns null when the form has not been submitted, when it has been submitted it returns true or false. That's why there is the isset.
    That doesn't seem like a good coding practice.

    When you see $isValid you automatically assume its valid values are boolean, and does not include a null value.

    Here is a fairly standard example of form validation:

    PHP Code:
    if ($form->isValid($_POST)) {
        
    // Valid!

    Keep it simple.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  12. #12
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that is reasonable. I have overcomplicated it a bit. I can check whether the form has been submitted or not inside the method, when it's not been submitted, it will just return false.

  13. #13
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    Actually my $form->validate('register') method returns null when the form has not been submitted, when it has been submitted it returns true or false. That's why there is the isset.
    Try the following.
    PHP Code:
    echo null == false 'ooo looky':'>.>'
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!


Bookmarks

Posting Permissions

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