www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2

Thread: PHP Variable

  1. #1
    Join Date
    Aug 2009
    Posts
    75

    Exclamation PHP Variable

    Okay so here is what i'm trying to do I'm making a Form Validation class that works with my Form Generation
    and I would like to have default error messages and the ability to have custom error messages so I have
    an array that has all the default error messages however certain error messages have a variable that isn't
    defined till the validation function is ran so is there a way to have the variable take the value of the defined variable by the function

    Default Array:
    PHP Code:
    class formValidation{
        public 
    $rules;
        public 
    $error_messages = array(
                 
    "alpha" => "This field must contain letters only.",
                 
    "numeric" => "This field must contain numbers only.",
                 
    "alphaNumeric" => "This field must contain letters and numbers only.",
                 
    "minLength" => "This field must contain at least $length characters.",
                 
    "maxLength" => "This field can not contain no more than $length characters."
                 
    );
        public 
    $errors
    as you can see minLength and maxLength need $length but $length isn't defined until it is validated like so:
    PHP Code:
    private function minLength($field,$length){
            if(
    strlen($field) <= $length){
                
    $this->errors[$field][] = $this->error_messages["minLenght"];
            }
        }
        private function 
    maxLength($field,$lenght){
            if(
    strlen($field) >= $lenght){
                
    $this->errors[$field][] = $this->error_messages["maxLenght"];
            }
        } 
    Purely Me - So Be Selfish Here!
    http://purelyme.co.cc

    Purely Fashion
    http://purelyfashion.co.cc

    Purely Weddings
    http://purelyweddings.co.cc

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,039
    Rather than embed a variable in a string, you'll probably just want to embed some sort of place-holder, e.g. "This field must contain at least {length} characters". Then when you actually use it, you would pass in your $length variable as a replacement value, e.g.:
    PHP Code:
    $errorMsg str_replace('{length}'$length$error_messages['max_length']); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

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
HTML5 Development Center



Recent Articles