SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Form Validation

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Form Validation

    I am trying to create a simple validation class for my registration form. I am having great difficulty with my logic (among some things). I can't get to "count" the variable to do as I want. I am basically using the function countErrors () to check and see if there were any errors incurred. Thanks for any help you can offer... I know it's a mess.

    PHP Code:
    class ValidateReg {

        var 
    $db;
        var 
    $errors;
        var 
    $count;
        
        function 
    ValidateReg (&$db) {
            
    $this->db=& $db;
            
    $this->errors" ";
            
    $this->count0;
            }
        

        function 
    username ($username " ") {
             if     ( 
    $username == " " )
                return 
    false;
            else
                return 
    true;
        }
        
        function 
    uniquename ($username " ") {
            
    $sql "SELECT * FROM
                       "
    .person."
                    WHERE
                       "
    .username."='".$username."'";
            
    $result $this->db->query($sql);

            if     ( 
    $result->size() > 
                return 
    false;
            else
                return 
    true
        }
        
        function 
    displayErrors () {
            if ( !
    $this->username () ) {
                
    $this->errors '<strong>Username required!</strong><br />';
                
    $this->count++;
                }
            if ( !
    $this->uniquename () ) {
                
    $this->errors .= '<strong>Username already registered!</strong>';
                
    $this->count++;
                }
                echo 
    $this->errors;
        }            
        
        function 
    countErrors () {
            if (
    $this->count 0
                return 
    false;
            else
                return 
    true;
            }

    -Lewis

  2. #2
    SitePoint Zealot sonic_2k_uk's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    i think the problem is this...


    PHP Code:
        function displayErrors () { 
            if ( !
    $this->username () ) { 
                
    $this->errors '<strong>Username required!</strong><br />'
                
    $this->count++; 
                } 
            if ( !
    $this->uniquename () ) { 
                
    $this->errors .= '<strong>Username already registered!</strong>'
                
    $this->count++; 
                } 
                echo 
    $this->errors
        }             
         
        function 
    countErrors () { 
            if (
    $this->count 0
                return 
    false
            else 
                return 
    true
            } 
    when you say $this->count++; in the function displayErrors, your basically saying... displayErrors->count++, and then when you call it in countErrors your trying to call countErrors->count, which isnt assigned.

    hope that helps

    Andy Buron

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the click reply. Sounds good, but I'm not sure how I would fix it :-)

    In another post, I think I am studying much better logic:
    http://www.sitepoint.com/forums/show...05&postcount=8

    So I'm going to rework the class.
    -Lewis

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't figure this one out:
    Fatal error: Call to a member function on a non-object in /home/virtual/site299/fst/var/www/html/Validate.php on line 27
    PHP Code:
    class Validation {

        var 
    $db;
        var 
    $Errors;
        
        function 
    ValidateReg (&$db) {
            
    $this->db=& $db;
        }
        
          function 
    AddError($description) {
              
    $this->Errors[] = $description;
           }
        
        function 
    username ($username " ") {
             if     ( 
    $username == " " ) {
                
    $this->addError('Username required!');
            } else {
                
    $sql "SELECT * FROM
                       "
    .person."
                    WHERE
                       "
    .username."='".$username."'";
                
    $result $this->db->query($sql);

                if     ( 
    $result->size() > 
                    
    $this->addError('Username already registered!');
            }
        }
        
        function 
    IsError() {
              return (
    is_array($this->Errors) && count($this->Errors) > 0);
           }
        
        function 
    GetErrors() {
              return 
    $this->Errors;
           }

    -Lewis

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lewis
    Can't figure this one out:
    Fatal error: Call to a member function on a non-object in /home/virtual/site299/fst/var/www/html/Validate.php on line 27
    PHP Code:
    class Validation {

        var 
    $db;
        var 
    $Errors;
        
        function 
    ValidateReg (&$db) {
            
    $this->db=& $db;
        }
        
          function 
    AddError($description) {
              
    $this->Errors[] = $description;
           }
        
        function 
    username ($username " ") {
             if     ( 
    $username == " " ) {
                
    $this->addError('Username required!');
            } else {
                
    $sql "SELECT * FROM
                       "
    .person."
                    WHERE
                       "
    .username."='".$username."'";
                
    $result $this->db->query($sql);

                if     ( 
    $result->size() > 
                    
    $this->addError('Username already registered!');
            }
        }
        
        function 
    IsError() {
              return (
    is_array($this->Errors) && count($this->Errors) > 0);
           }
        
        function 
    GetErrors() {
              return 
    $this->Errors;
           }

    It sounds to me like the database query is failing and you are not getting a result back. I assume you are using PEAR for your abstraction layer. If so, try doing something like this:

    PHP Code:
    $result =& $this->db->query($sql);
    if (
    DB::isError($result)) {
        die(
    $result->getMessage());

    JT

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I'm using MySQL.php from Harry's A1.
    -Lewis

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stupid me! I changed the name of the class but forgot to change the constructor name as well! Sorry to waste anyone's time but I had spent two days looking at this!
    -Lewis

  8. #8
    SitePoint Guru
    Join Date
    Dec 2003
    Location
    oz
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol ... yeh **** happens


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
  •