Problem combining validation and templates

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

    Problem combining validation and templates

    Feelin' a bit guilty about monopolising the group (3 posts in 3 days) but my
    question and it's solution could interest others.

    Anyhow I am having great difficulty getting my head around the below code.
    Basically I want to display the form and if the username textbox is then
    submitted empty, I want the form redisplayed but this time textbox
    background color to be red and not yellow.

    The code is something I simply knocked up so I could see how I can use
    stylesheet settings to highlight boxes (or anything really) which have
    failed validation. I am just wondering if I have approached this from
    completely the wrong angle? I used to create forms within the page itself
    but specifically want to use templates now.

    Be grateful if something could have a look and give me some pointers. I know
    the code below is incorrect (that's the problem).

    Cheers

    Phil

    ------- testpage02.php ---------
    <?php
    //testpage02.php

    $newTextBox = new myTextBox("user name", "standardTextBo x");
    $newSubmitButto n = new mySubmitButton( "Submit");
    include ("testpage02.tp l.php");

    if (!empty($_POST['username']))
    {

    echo "Success";
    exit;
    }
    else
    {
    $newTextBox->setStyle("erro rTextBox");
    include ("testpage02.tp l.php");
    exit;
    }


    class myTextBox
    {
    private $textboxName;
    private $style;

    function __construct($te xtboxName, $style)
    {
    $this->textboxName = $textboxName;
    $this->style= $style;
    }

    function setStyle($style )
    {
    $this->style= $style;
    }

    function getTextBox()
    {
    $line = "<input type=\"text\" ";
    $line.= "name=\"";
    $line.= $this->textboxName;
    $line.= "\" ";
    $line.= "class=\"";
    $line.= $this->style;
    $line.= "\" />";
    return $line;
    }
    }

    class mySubmitButton
    {
    private $value;

    function __construct($va lue)
    {
    $this->value = $value;
    }

    function getSubmitButton ()
    {
    return "<input type=\"submit\" value=\"".$this->value."\"/>";
    }
    }

    ?>

    ------- testpage02.tpl. php ---------
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html>
    <head>
    <!-- testpage02.tpl. php -->
    <title>Test Page 02</title>

    <style type="text/css">

    ..standardTextB ox
    {
    background-color: yellow;
    }

    ..errorTextBox
    {
    background-color: red;
    }
    </style>
    </head>

    <body>
    <form action="<?php print $PHP_SELF ?>" method="POST">

    <?php echo $newTextBox->getTextBox() ?>
    <br />
    <?php echo $newSubmitButto n->getSubmitButto n() ?>

    </form>

    </body>
    </html>


  • Phil Latio

    #2
    Re: Problem combining validation and templates

    Got it working. Damn obvious to use isSet, can't think why I did not think
    of it before. Not exactly happy with the code but at least I can now think
    how to do this more efficiently since it works.

    Cheers

    Phil


    <?php
    //testpage02.php

    $newTextBox = new myTextBox("user name", "standardTextBo x");
    $newSubmitButto n = new mySubmitButton( "Submit");


    if (!isSet($_POST['username']))
    {
    include ("testpage02.tp l.php");
    exit;
    }

    if (!empty($_POST['username']))
    {

    echo "Success";
    exit;
    }
    else
    {
    $newTextBox->setStyle("erro rTextBox");
    include ("testpage02.tp l.php");
    exit;
    }


    class myTextBox
    {
    private $textboxName;
    private $style;

    function __construct($te xtboxName, $style)
    {
    $this->textboxName = $textboxName;
    $this->style= $style;
    }

    function setStyle($style )
    {
    $this->style= $style;
    }

    function getTextBox()
    {
    $line = "<input type=\"text\" ";
    $line.= "name=\"";
    $line.= $this->textboxName;
    $line.= "\" ";
    $line.= "class=\"";
    $line.= $this->style;
    $line.= "\" />";
    return $line;
    }
    }

    class mySubmitButton
    {
    private $value;

    function __construct($va lue)
    {
    $this->value = $value;
    }

    function getSubmitButton ()
    {
    return "<input type=\"submit\" value=\"".$this->value."\"/>";
    }
    }

    ?>


    Comment

    • Toby A Inkster

      #3
      Re: Problem combining validation and templates

      Phil Latio wrote:
      function getTextBox()
      {
      $line = "<input type=\"text\" ";
      $line.= "name=\"";
      $line.= $this->textboxName;
      $line.= "\" ";
      $line.= "class=\"";
      $line.= $this->style;
      $line.= "\" />";
      return $line;
      }
      What's all this mess all over the place?!

      function getTextBox ()
      {
      return sprintf('<input type="text" name="%s" class="%s" />',
      htmlentities($t his->textboxName) ,
      htmlentities($t his->style)
      );
      }

      Go and tidy your room Phil!

      --
      Toby A Inkster BSc (Hons) ARCS
      Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

      Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

      Comment

      • Phil Latio

        #4
        Re: Problem combining validation and templates

        What's all this mess all over the place?!
        >
        function getTextBox ()
        {
        return sprintf('<input type="text" name="%s" class="%s" />',
        htmlentities($t his->textboxName) ,
        htmlentities($t his->style)
        );
        }
        >
        Go and tidy your room Phil!
        Hahaha... thanks Toby :-))

        Actually I have recently seen %s but didn't understand it's useage. It was
        used in QCodo framework

        As for htmlentitities, that's something I've also seen but never really
        investigated it's use. I shall wander over to the PHP manual and take a
        look. I agree your solution is a much tidier than mine.

        Cheers

        Phil


        Comment

        Working...