SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create one instance or many instances?

    I'm working on a form validator class and I don't know if I should create one instance of it and use arrays and handles to access the forum fields in the class, or create a new instance for each form field. If I created multiple instances that could be inefficient for a large form, but if I have one class that handles all fields in the form that would make it an over complicated class. As I've seen if the class is too complicated itís obviously doing too much. Which way would be better? Or is there an alternative way that I'm missing. Thanks.
    Escape Your Mind
    Director of Technology
    www.boxgods.com

  2. #2
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by EscapeYourMind
    If I created multiple instances that could be inefficient for a large form
    Making decisions in the hope to save a few cpu cycles before your design is not a good practice.

    Quote Originally Posted by EscapeYourMind
    but if I have one class that handles all fields in the form that would make it an over complicated class.
    The class would be as complicated as you allow it to be. Imho, you could write a FormValidator class which has multiple Validator objects. For example: DataValidator, RegexValidator, ...

    But you can also add more complex Validators. For example: check if the combination (postal code, city) is valid. It would be hard to implement this in a validator that accepts only 1 field )

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should check http://www.sitepoint.com/forums/showthread.php?t=277808 as we had a discussion of Validators and there is code for you to download and look at.
    Christopher

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your input.

    Arborint thanks for the link.. I have read through some of that stuff several times, but at this point its beyond me. I'm still trying to get a grasp on the patterns and stuff.

    However I don't think my form validator needs to be as complicated or as flexible as whats in the application controller thread. I mainly just need to make sure the data from the form is filled in if its required, and then clean the input data through several levels, such as addslashes and removing html, or removing any symbol or such if its a username or password.

    Would it be a better idea to create a general form validator class and then abstract some of its functions to more specific child classes, such has have a different validate class if its using a general striphtml() way, or using a regex way?
    Escape Your Mind
    Director of Technology
    www.boxgods.com

  5. #5
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Validation classes validate data...
    Formatter classes format data...

    A Form class could have Validation and Formatter classes... But adding formatting methods to the validation class doesn't seem a good idea..

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm good point
    Escape Your Mind
    Director of Technology
    www.boxgods.com

  7. #7
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by EscapeYourMind
    Arborint thanks for the link.. I have read through some of that stuff several times, but at this point its beyond me. I'm still trying to get a grasp on the patterns and stuff.

    However I don't think my form validator needs to be as complicated or as flexible as whats in the application controller thread. I mainly just need to make sure the data from the form is filled in if its required, and then clean the input data through several levels, such as addslashes and removing html, or removing any symbol or such if its a username or password.
    If you take a look at the Validator and FilterChain classes. They are very small and easy to understand. The accociated Rules and Filter classes are as simple. With them you can filter and validate incoming data with a couple of lines per var, provide good error messages, and get rid the nested ifs that validation code often creates. It is a proven and flexible way to do what you want.
    Christopher

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for pointing that out arborint. I took a closer look at the classes you guys have developed and they really do seem quite easy. I think the biggest thing that messed me up was the UML diagram on the first page, and the interfaces(which I still dont understand). I just know they are a class of all abstract functions, but I don't know what good that would do. Either way it looks like I'm on the right track now. Thanks.
    Escape Your Mind
    Director of Technology
    www.boxgods.com

  9. #9
    SitePoint Addict timvw's Avatar
    Join Date
    Jan 2005
    Location
    Belgium
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem i have with the "Rules" is that they seem to be bound to a particular field in the form.. Am i missing something or is it impossible to add a Rule like sum(article1 + article2) < 100 ?

  10. #10
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by timvw
    The problem i have with the "Rules" is that they seem to be bound to a particular field in the form.. Am i missing something or is it impossible to add a Rule like sum(article1 + article2) < 100 ?
    You are correct that they are bound to a particular field. I originally started with just passing a value which seems cleaner but does not provide the functionality to make atomic rules that can work on multiple fields. The goal is to be able to defer the evaluation of a condition. To do your example you would do:
    PHP Code:
    class SumLessThan100Rule extends Rule {
    var 
    $article1;
    var 
    $article2;

    function 
    SumLessThan100Rule($article1$article2$errorMsg) {
        
    $this->article1 $article1;
        
    $this->article2 $article2;
        
    $this->errorMsg $errorMsg;
    }
    function 
    isValid($dataspace) {
        return (
    $dataspace->get($this->article1) + $dataspace->get($this->article2)) < 100;
    }

    Christopher

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Reading up on the book Domain Driven Design,

    The goal is to be able to defer the evaluation of a condition.
    It would appear that the Specification design pattern would be most helpful to you in that regard? Jason has done some stuff with the pattern, if your in the mood to do a search for it...

    But I've not really looked into it yet, as I've had no need to


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
  •