SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I need some suggestions please ....

    hi ...

    An application I'm working on uses a ini file to store configuration variables. The client wants a web interface to add or modify the variables in the ini files. So basically I have to map the ini variables to a html form.

    Example

    [GLOBAL]
    somevar = somevalue
    someothervar = someothervalue

    would translate into

    <form>
    <input type="text" name="somevar" value="somevalue" />
    <input type="text" name="someothervar" value="someothervalue" />
    <input type="submit" value="Save" />
    </form>

    So far so good. The ini variables need to be validated. The solution I have right now for validation uses the Specification pattern for that. Something like this:

    PHP Code:
    $v =& new Validator();
    $v->addRule( new RegexpMatchRule'somevar''#^[a-zA-Z0-9]{3,24}$#''The %s variable must contain alphanumeric characters only and must be between 3 and 24 characters in length' ) );
    $v->addRule( new InListRule'someotherval'range010 ), 'The %s field must be an integer between 0 and 10' ) );
    if ( !
    $v->isValid$data ) ) {
        foreach ( 
    $v->getErrors() as $error ) {
            echo 
    $error '<br />';
        }

    The problem is that the client, when adding new variables to the ini file, wants to be able to specify the validation rules from the web interface ... so i have to save those rules somewhere (like an xml file or something) and when saving (from the web interface) parse the xml file and create the code above dynamically.

    My question is: how? (please consider that some of the validation rules can be ( and are ) more complex than the ones in the example (like
    PHP Code:
    $v->addRule( new AndRule( new RegexpMatchRule, new OrRule( new InListRule(), new CsvStringRule() ) ) ); 
    and that i also need to pass parameters to those rules ).

    I did a search on this forum but nothing useful came up so ... please help.

    Thanks.
    Last edited by acid24ro; Jul 23, 2007 at 05:41.

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it need to be an XML file you use though, to determine what available rule(s) you have? I envisage that you use a Factory to generate your specification, either on a rule by rule basis, or collectively (where a series of rules is seen as one from the client script point of view).

    Going by your script example, you are looking at decoration; Here is an example of decoration on the fly for you,

    PHP Code:
    $plugin_manager = new QApplication_Plugin_Manager( new QPlugin_Logging() );
        
    $plugin_manager -> attach( ... other plugin ... );
        
    $plugin_manager -> attach( new QPlugin_Something( new QPlugin_Other_Something() ) ); 
    The result returned is of a decorated structure,

    PHP Code:
    abstract class QApplication_Plugin implements QApplication_Plugin_Interface {
            protected 
    $plugin null;
            
            public function 
    __construct() {}
            public function 
    setPluginQApplication_Plugin_Interface $plugin ) {
                
    $this -> plugin $plugin;
            }
            
            
    // ...
        
    }

    final class 
    QApplication_Plugin_Manager implements QApplication_Plugin_Manager_Interface {
            private 
    $plugins = array();
            private 
    $counter = -1;
            
            public function 
    __constructQApplication_Plugin_Interface $plugin ) {
                
    $this -> plugins[++$this -> counter] = $plugin;
            }
            
            public function 
    attachQApplication_Plugin_Interface $plugin ) {
                
    $temp $this -> plugins[$this -> counter];
                
    $temp -> setPlugin$plugin );
                
    $this -> plugins[++$this -> counter] = $plugin;
            }
            
            public function 
    getPlugins() {
                return 
    array_shift$this -> plugins );
            }
        } 
    If you use Factory then you can prepackage everything up nicely, including the Factory API it's self, which would be a typeof Specification also.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response. No, it does not have to be an XML file. I was just thinking about it and it seemed like something that would work. Will take a look at your suggestion and come back with more questions . Thanks again.

  4. #4
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If someone has other suggestions please post them.

    Thanks in advance

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are essentially two ways to make an expression builder: the visual aproach and a DSL. You can build a tree-alike interface for the user, where she can insert "and", "or" and "group" (sub-expressions) nodes and "attach" atomic validation rules to them, or you can invent your own language, like this
    Code:
    customer_id = regexp([a-z]+) or (integer and lesser_than(10))
    and have the user to use it (perhaps also with some kind of visual guidance).

    No matter which way you choose, you end up with some kind of tree structure, the validation consists of traversing the tree, "executing" each node on the way.


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
  •