At what point should I implement validation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Latio

    At what point should I implement validation?

    I am designing a relatively simple CMS with the usual Item, Category and
    User scenario. My question here relates to how I implement the User
    registration on this application. Currently I have the following:

    register.php
    classes/class.Form.php
    classes/class.Validatio n.php
    classes/class.User.php
    classes/class.MySQL.php
    templates/register.tpl.ph p

    The thing that is really bugging me is how I perform the validation on what
    is entered on the register.php page?

    My first version (which I actually coded) simply created a new instance of
    the Validation class on the register.php page then performed numerous if
    statements with an error increment counter and if that returns zero (once
    all the tests are completed) then proceed else redisplay registration.ph p
    page. Works OK but looks terribly ugly and I am convinced there are far
    better approaches.

    My second idea is to use CRUD methods within the User class and have the
    User class call the validation directly.

    My third idea was to somehow apply the validation rules to the form objects.
    I have seen that some frameworks use this method but my problem is that I am
    really bad at understanding other people's code.

    I keep hearing the term "design patterns" and just wondering if reading a
    book or two on that subject might clear my head on how to progress forward.
    The problem is not that I cannot code this, it's the approach I should take
    and would value some opinions by the piers of the group.

    Cheers

    Phil


  • gosha bine

    #2
    Re: At what point should I implement validation?

    On 22.06.2007 07:17 Phil Latio wrote:
    [skip]
    I keep hearing the term "design patterns" and just wondering if reading a
    book or two on that subject might clear my head on how to progress forward.
    ISBN 0201633612
    ISBN 0321127420

    hope this helps ;)


    --
    gosha bine

    extended php parser ~ http://code.google.com/p/pihipi
    blok ~ http://www.tagarga.com/blok

    Comment

    • Jerry Stuckle

      #3
      Re: At what point should I implement validation?

      Phil Latio wrote:
      I am designing a relatively simple CMS with the usual Item, Category and
      User scenario. My question here relates to how I implement the User
      registration on this application. Currently I have the following:
      >
      register.php
      classes/class.Form.php
      classes/class.Validatio n.php
      classes/class.User.php
      classes/class.MySQL.php
      templates/register.tpl.ph p
      >
      The thing that is really bugging me is how I perform the validation on what
      is entered on the register.php page?
      >
      My first version (which I actually coded) simply created a new instance of
      the Validation class on the register.php page then performed numerous if
      statements with an error increment counter and if that returns zero (once
      all the tests are completed) then proceed else redisplay registration.ph p
      page. Works OK but looks terribly ugly and I am convinced there are far
      better approaches.
      >
      My second idea is to use CRUD methods within the User class and have the
      User class call the validation directly.
      >
      My third idea was to somehow apply the validation rules to the form objects.
      I have seen that some frameworks use this method but my problem is that I am
      really bad at understanding other people's code.
      >
      I keep hearing the term "design patterns" and just wondering if reading a
      book or two on that subject might clear my head on how to progress forward.
      The problem is not that I cannot code this, it's the approach I should take
      and would value some opinions by the piers of the group.
      >
      Cheers
      >
      Phil
      >
      >
      Phil,

      Class objects should validate themselves - they shouldn't have to rely
      on outside code. So in your case I would suggest validating in the user
      class.

      Also, beware - your "validation " class is probably not an appropriate
      class. Does it have its own members (which aren't members of other
      classes)? Or is it just validating some other class? If the former, it
      would be a valid class. However, it's easy to fall into a trap of the
      latter. In that case it should be a method, not an object.

      It's easy to get confused, especially when you get something like
      "validate" (a verb) and "validation " (a noun). One is a method, one is
      a classes.

      And btw - "display" is even worse! :-)

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Phil Latio

        #4
        Re: At what point should I implement validation?

        Phil,
        >
        Class objects should validate themselves - they shouldn't have to rely on
        outside code. So in your case I would suggest validating in the user
        class.
        >
        Also, beware - your "validation " class is probably not an appropriate
        class. Does it have its own members (which aren't members of other
        classes)? Or is it just validating some other class? If the former, it
        would be a valid class. However, it's easy to fall into a trap of the
        latter. In that case it should be a method, not an object.
        >
        It's easy to get confused, especially when you get something like
        "validate" (a verb) and "validation " (a noun). One is a method, one is a
        classes.
        >
        And btw - "display" is even worse! :-)
        Jerry

        Many thanks for your thoughts. You make some interesting points and I hadn't
        considered taking that approach of validating within the user class or the
        form class.

        Cheers

        Phil


        Comment

        Working...