SitePoint Sponsor

User Tag List

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 84
  1. #26
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    Norway
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I agree with frabron. Be careful about abstracting things like HTML, which have unlimited ways of combination. Those tag-classes are just plain wrappers for HTML, and then you might as well write HTML, its simpler, shorter and more readable too!

    A form however, can be tought of as an object, because it has members (the input-tags) that you want to do lots of things with. One is validation: add a validator to a form element, and upon displaying the form, show the error messages under the element if there are any errors. Another is filtering: add filters to the form elements so that the data is filtered before you pass it on to your buisiness logic. And then there is form populating (setting the data) and redisplaying the data when validation failed.. and so on.

    Take a look at Zend_Form: http://framework.zend.com/manual/en/zend.form.html

    Another approach could be to create a form class, some element classes, some validation classes and a form-renderer which takes as input only the form-object. The renderer would loop through the elements and produce the HTML. The renderer is plug-able (given to the form class as a parameter) and therefore one can switch between different renderers.

    What I have noticed is that whenever one tries to abstract someting complex like HTML or SQL, you always end up with either having to write just as much code or not being able to specify all the details. You end up loosing the details, and then whenever you want to do something slightly different, you must either do it "manually" or add functionality to your classes...

    I'm not saying that you should not abstract anything, but certain things gets really complicated when you try to abstract it. At least create a backdoor for each abstraction so that you may do it manually if neccessary.

    Happy coding!

  2. #27
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    frabron and oeyvind, I agree that you can go overboard and the trick is to find a happy mid-point. That mid-point will not be the same for everyone. If you write a form now and then you might as well hand code the thing. If you spend most of your time writing forms then automation is going to be a big help. The way I decide it's time to create some automation is when I say to myself: "Gee, I don't want to write that code again!" I use all sorts of things from BBEdit text factories to functions that validate email and classes that write selects, radio buttons and checkboxes, which are the more complex input devices. The more complex an object is, the more it benefits from OOP.

    You might ask why create a class for something as simple as a line break (<br />). For the inheritance! Everything in HTML is a tag so you might as well start there. Then if someone invents a new tag you can use your generic Tag class for it.

    There is another consideration, you can use the OOP code in your live application or you can use it as a "FormBuilder." It can do more for you than a template. I use something like that to write the articles for my personal website. I write the article as a heredoc string and the essay template converts it into a complete static page. It also producs the text and links for my index page. All I have to do is copy and past.

    Just find your own happy medium!
    Denny Schlesinger
    web services

  3. #28
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Gee, I don't want to write that code again!"
    That's why I use templates

    But I didn't want to distract you from building your Form class or convince you to let it be - I was just posting my experiences as a side note, well knowing that content of my post wasn't much aid to your problems.

    ... or you can use it as a "FormBuilder." ...
    This is really one case where such a class is useful - because you build your form more or less by some definition, wether by other users or some other rule (db, file etc.)
    I never tried this tho since mapping the form fields from such a form to a database is beyond my capabilites

  4. #29
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frabron View Post
    That's why I use templates

    But I didn't want to distract you from building your Form class or convince you to let it be - I was just posting my experiences as a side note, well knowing that content of my post wasn't much aid to your problems.
    Not to worry, I'm having a lot of fun playing with this class. It's early on so I don't know where it will wind up but all the guys here have been very helpful to get me thinking in the right direction.


    This is really one case where such a class is useful - because you build your form more or less by some definition, wether by other users or some other rule (db, file etc.)
    I never tried this tho since mapping the form fields from such a form to a database is beyond my capabilites
    It doesn't have to that complicated. You can use your browser as the rendering engine. If you copy my Tag class example from this thread and run it as a php script (after removing the invisible characters the forum software adds for spaces ), the browser's "source view" has the finished template.

    Or you can save the page as a source file.
    Denny Schlesinger
    web services

  5. #30
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't know about anyone else but I know I'd find:
    HTML Code:
    <form action="/" method="get">
        <label for="username">Username</label>
            <input type="text" name="username" />
        <label for="password">Password</label>
            <input type="password" name="password" />
        <input type="submit" value="Login" />
    </form>
    Much, much better than:

    PHP Code:
    <?php
    Tag
    ::Get('form')->addAttribute('action''/')->addAttribute('method''get')->AddTags(
        
    Tag::Get('label')->addAttribute('for''username')->setBody('Username'),
            
    Tag::Get('input')->addAttribute('type''text')->addAttribute('name''username'),
        
    Tag::Get('label')->addAttribute('for''password')->setBody('Password'),
            
    Tag::Get('input')->addAttribute('type''password')->addAttribute('name''password'),
        
    Tag::Get('input')->addAttribute('type''submit')->addAttribute('value''Login')
    )->
    Render();
    XML-style text is much more efficient and readable than we could do with object-based programming.

    Theoretically this kind of stuff is good to mess around with, but realistically it just doesn't have a point.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #31
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Theoretically this kind of stuff is good to mess around with, but realistically it just doesn't have a point.
    If all you are going to do is render html tags, of course, you might as well just write the tags themselves. I hope you don't think that is the purpose of this exercise.

    Mostly I do web apps and the user interface is mostly HTML forms that have to be populated with data and the incoming data has to be validated and stored or otherwise processed. Eventually the Form class or whatever results from this exercise will do a lot of heavy lifting, not just drawing pretty tags.

    Next weekend I'll get around to writing a few more classes that actually do some useful work.
    Denny Schlesinger
    web services

  7. #32
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by arkinstall View Post
    I don't know about anyone else but I know I'd find:
    HTML Code:
    <form action="/" method="get">
        <label for="username">Username</label>
            <input type="text" name="username" />
        <label for="password">Password</label>
            <input type="password" name="password" />
        <input type="submit" value="Login" />
    </form>
    Much, much better than:

    PHP Code:
    <?php
    Tag
    ::Get('form')->addAttribute('action''/')->addAttribute('method''get')->AddTags(
        
    Tag::Get('label')->addAttribute('for''username')->setBody('Username'),
            
    Tag::Get('input')->addAttribute('type''text')->addAttribute('name''username'),
        
    Tag::Get('label')->addAttribute('for''password')->setBody('Password'),
            
    Tag::Get('input')->addAttribute('type''password')->addAttribute('name''password'),
        
    Tag::Get('input')->addAttribute('type''submit')->addAttribute('value''Login')
    )->
    Render();
    XML-style text is much more efficient and readable than we could do with object-based programming.

    Theoretically this kind of stuff is good to mess around with, but realistically it just doesn't have a point.

    My form class works in a similar fashion to the solution posted (passing attributes).

    The logic of the form should not be handled in the display logic. Why is maxlength set there? Why is whether it's disabled or not set there? It makes no sense controlling the behaviour of the form in the display routine. Similarly, you need to be able to control the display of a form on a per-form level.

    Advantages of the form class:

    repopulating from $_POST if it failed validation:
    PHP Code:
    //rebuild $form
    foreach ($_POST as $key => $value) {
        
    $form->get('key')->setAttribute('value'$value);
    }

    //reshow the form 
    Add validation to the elements:
    PHP Code:
    $phone->validation '^[0-9]+';
    $phone->validationWarning 'Your phone number can only contain numbers'
    as long as you rebuild the same form object to deal with the result, you have a much more flexible system.


    The problem you will run into with such a form class is the display of the form. Unless you have a very generic looking form with simple label/element on each line you'll want to do some styling on it.

    I solved this by placing forms in the my template class like so:

    HTML Code:
    [myform]
    <div>Name: [myform field="name"]</div>
    <div>Address: [myform field="address"]</div>
    <div>Description:</div>
    <div style="width: 100&#37;">[myform field="description" style="height: 100px"]</div>
    [/myform]
    Doing it like this allows keeping the behaviour of the form and the display of it entirely separate, I pass the form object to my template class which does the relevant replacements based on form name. The template has control over a few of the attributes such as style, and can override some others providing they're not set on the object itself.

    Without the template, displaying the forms in a non-generic way is impossible. This method allows ultimate flexibility for structure, display and separating the business logic from the display logic.

    Ok, it's a little extra set up but when you're dealing with sites with hundreds of forms, this certainly makes life a lot easier.

  8. #33
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    build HTML form visual part in PHP is a bad idea. just build the form use HTML not PHP.

    just think about "html form " without visual part, then you probably do not want to build HTML Form visual part in php anymore.

    from web server point of view, HTML form is just a string of key,value pair, no button, no text field, no maxlenght, not any thing related to visual part of HTML form.

  9. #34
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frabron View Post
    I find it easier to build my forms in HTML and implement them through templates than writing a set of classes instead.
    This is how I do it too.

    That said, you may want to consider how HTML elements are usually handled as objects, namely via DOM methods. The DOM already has createElement(), setAttribute(), etc. So you're basically reinventing the wheel when you create an OOP library for this. Why not use a DOM implementation in PHP to build the form and dump the underlying X(HT)ML?

  10. #35
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    build HTML form visual part in PHP is a bad idea. just build the form use HTML not PHP.

    just think about "html form " without visual part, then you probably do not want to build HTML Form visual part in php anymore.
    This is what I'm doing now. The problem is that with a complex form there is a lot of clutter. Interspaced with the formatting tags there is quite a bit of php that corresponds to the form input elements. TomB is suggesting a Template class but that too is more work and it can be done instead with a heredoc variable:

    PHP Code:
    <?php
    $form 
    = <<<FORMFORM
    <form action="{$form->action}" method="{$form-> method}">
      <fieldset>
      <legend>Data</legend>
        <label class="form1Names" for="
    $form->input1Name}">Your name: </label>
        <input type="
    {$form->input1Type}" id="$form->input1Name}" name="$form->input1Name}" value="$form->input1Value}" />
      </fieldset >
      <br />
      <fieldset >
      <legend>Action</legend>
        <button type="submit" name="submit" value="submit">Click me!</button>
      </fieldset >
    </form>
    FORMFORM;

    echo 
    $form;
    ?>
    For each form element there are one or more layout tags! The form actually has more layout than elements! Instead of a template I prefer to have the form in a separate file and use an include. At this point I'm just experimenting and appreciating all the input.

    If the form's layout is a variable, there is no reason for that variable not to be one of the form's properties.


    from web server point of view, HTML form is just a string of key,value pair, no button, no text field, no maxlenght, not any thing related to visual part of HTML form.
    From a php point of view the form is the associative array $_POST or $_GET and $_FILE
    Denny Schlesinger
    web services

  11. #36
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    Instead of a template I prefer to have the form in a separate file and use an include.
    This is what I think of when I say "template", actually. I use include files written in HTML with PHP inline syntax for inserting variables. I do it to keep all the markup out of my class definitions. For example, a login method might look like this:

    Session.php:

    PHP Code:
    class Session {

    ...

    public function 
    loginForm() {
    ob_start();
    include(
    'Session.loginForm.php');
    return 
    ob_get_clean();
    }

    ...


    Session.loginForm.php:

    PHP Code:
    <form ... >
    <input name="var1" value="<?=$this->var1?>" />
    <input name="var2" value="<?=$this->var2?>" />
    <input type="submit" value="Login" />
    </form>

  12. #37
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    This is what I think of when I say "template", actually. I use include files written in HTML with PHP inline syntax for inserting variables. I do it to keep all the markup out of my class definitions.
    I think we are on the same age. TomB suggested a "real" template class.

    I like to think of php as "cascading includes"
    Denny Schlesinger
    web services

  13. #38
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    I think we are on the same age. TomB suggested a "real" template class.

    I like to think of php as "cascading includes"
    Template systems are superfluous in PHP, since PHP has its own "template" syntax. Smarty tags aren't much easier to read than PHP template tags, so why bother?

  14. #39
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by world Wide Weird View Post
    template Systems Are Superfluous In Php, Since Php Has Its Own "template" Syntax. Smarty Tags Aren't Much Easier To Read Than Php Template Tags, So Why Bother?
    Oh Dear God! Please Not Another Php Is A Template Language Smarty Is Redundant Argument Argument. It'll Never End!!! Nooooo!!! >.>
    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!

  15. #40
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    Oh Dear God! Please Not Another Php Is A Template Language Smarty Is Redundant Argument Argument. It'll Never End!!! Nooooo!!! >.>
    LMAO!

    Who's arguing? I stated a simple fact. The fact that it's a fact makes any argument moot. So argue away, if that's your idea of a good time, but I won't participate.

  16. #41
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    LMAO!

    Who's arguing? I stated a simple fact. The fact that it's a fact makes any argument moot. So argue away, if that's your idea of a good time, but I won't participate.
    Please no . I've been in the trenches on this one far to often.
    Off Topic:

    There is actually one thread where someone flat out flamed me for posting some of Ramus' opinions on the issue. Deep wounds... deep wounds.
    lol
    In any case, the argument never goes anywhere. It's like a massive circular argument ... it has no beginning or end.... x_x... I'm actually thinking of rewriting smarty in C++ just to end the endless debating.... But I'm not that desperate yet >.>
    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!

  17. #42
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    Please no . I've been in the trenches on this one far to often.
    Off Topic:

    There is actually one thread where someone flat out flamed me for posting some of Ramus' opinions on the issue. Deep wounds... deep wounds.
    lol
    In any case, the argument never goes anywhere. It's like a massive circular argument ... it has no beginning or end.... x_x... I'm actually thinking of rewriting smarty in C++ just to end the endless debating.... But I'm not that desperate yet >.>
    Let's agree to let each developer use what he finds most suitable and leave it at that. More interesting than the tired argument is your remark about rewriting Smarty in C++. What would that accomplish besides making it faster?

  18. #43
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    build HTML form visual part in PHP is a bad idea. just build the form use HTML not PHP.

    just think about "html form " without visual part, then you probably do not want to build HTML Form visual part in php anymore.

    from web server point of view, HTML form is just a string of key,value pair, no button, no text field, no maxlenght, not any thing related to visual part of HTML form.
    However a form is *much* more than the HTML.

    My form class handles validation by setting $field->validation on the object itself. If you're just outputting HTML there's no clean way of doing this.

    If you use a as an object you can use the same form for both "Add" and "Edit".

    My Form class looks like this, it's extended on a per form basis.

    PHP Code:
    abstract class Form {
        private 
    $fields = array();
        abstract public function 
    save($id);
        abstract public function 
    load($id);
        abstract public function 
    insert();

        public function 
    populate($data) {
           ... 
    populate e.gfrom $_REQUEST
        
    }



    $form->load(123);

    which will load the correct record(s) from the database to populate the form.

    My forms get auto-populated from $_GET/POST depening on form method if the relevant values are set, and I can then do $form->save($_GET['id']); to save the data for the current record being edited.

    This is a far cleaner way of keeping all aspects of form control together.

  19. #44
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Tom:

    I'm sure to borrow some of your ideas for my Form class. I think what ctx2002 didn't like was the template class. I'd like to do the job without an additional Template class myself. I think I can get the Form class to do the display as well as the processing.
    Denny Schlesinger
    web services

  20. #45
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've messed around a bit with form generation and automatic validation, in an XHTML-based file actually. It was basically the form but with validation as attributes too; these were stripped before output and used by PHP in the validation process.

    I even ventured into the automated process via attribute method too, where attributes could make the form auto update, insert into or delete a record from a database table.

    Might be an idea for you to try.

    For example:
    HTML Code:
    <form action="/someaction" method="post">
        <input type="text" name="Username" format="[A-Za-z ]+" min="3" max="20" />
        <input type="password" name="Password" min="20" />
        <verification>
             <query>SELECT id FROM Users WHERE Username=:Username AND Password = :Password</query>
             <success>SessionSet(array('loggedin', true), array('userID', :id))</success>
             <fail>redirect(HOME)</fail>
        </verification>
    </form>
    I got it to work, but I didn't like using it, so scrapped it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  21. #46
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    For example:
    HTML Code:
    <form action="/someaction" method="post">
        <input type="text" name="Username" format="[A-Za-z ]+" min="3" max="20" />
        <input type="password" name="Password" min="20" />
        <verification>
             <query>SELECT id FROM Users WHERE Username=:Username AND Password = :Password</query>
             <success>SessionSet(array('loggedin', true), array('userID', :id))</success>
             <fail>redirect(HOME)</fail>
        </verification>
    </form>
    I got it to work, but I didn't like using it, so scrapped it.
    That's mixing layout and process, no wonder you didn't like using it.
    Denny Schlesinger
    web services

  22. #47
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Exactly
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  23. #48
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question

    Off Topic:

    I unsubscribed from this thread by mistake. How do you re-subscribe without posting a reply?
    Last edited by captainccs; Mar 5, 2009 at 14:27. Reason: To mark it off topic
    Denny Schlesinger
    web services

  24. #49
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Top of the thread view, Thread Tools and 'Subscribe to this thread'.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  25. #50
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    I've messed around a bit with form generation and automatic validation, in an XHTML-based file actually. It was basically the form but with validation as attributes too; these were stripped before output and used by PHP in the validation process....
    Could skip XHTML and go with plain old XML and use XSLT to output (X)HTML using PHP. It is also easy as pie to use the DOM to get what you need as well.

    Code XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <form action="..." method="post">
        <input type="text">
            <name>username</name>
            <label>Username:</label>
            <validation>
                <format>/^\w[\w ]+$/</format>
                <!-- Other options too.. -->
            </validation>
        </input>
        <input type="password">
            <name>password</name>
            <label>Password:</label>
        </input>
    </form>
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •