Results 1 to 6 of 6

Thread: [RESOLVED] Validation Class: Trying to Work Our Custom Message

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Resolved [RESOLVED] Validation Class: Trying to Work Our Custom Message

    I'm new to OOP....

    Here's part of my register.php that has a form for user registration...

    Code:
    If(Input::exists()) {
    	$validate = new Validate;
    	$validation = $validate->check($_POST, array(
    		'username'		 => array(
    			'name'		 => 'Username', //display name if error occurs
    			'required' 	 => true,
    			'min' 		 => 2,
    			'max' 		 => 20,
    			'unique' 	 => 'users' //fieldname that must be matched to see if fieldname is unique
    			),
    		'password'		 => array(
    			'name'		 => 'First Password',
    			'required' 	 => true,
    			'min'		 => 6,
    			),
    		'password_again' => array(
    			'name'		 => 'Second Password',
    			'required' 	 => 'true',
    			'matches' 	 => 'password'
    			),
    		'name'			 => array(
    			'name' 		 => 'Name',
    			'required' 	 => true,
    			'min'		 => 2,
    			'max'		 => 50
    			)
    		));
    The form must be validated first, so my validation class is used to use the check() method.

    Here's my check method in my validation class:

    Code:
    public function check($source, $items = array()) {
    		foreach($items as $item => $rules) {
    			
    			foreach($rules as $rule => $rule_value) {
    
    				$value = trim($source[$item]);  
    				$name = $rules['name'];  
    				$item = escape($item);
    
    				if($rule === 'required' && empty($value)) {
    					$this->addError("{$name} is required.");
    				} else if(!empty($value)) {
    					switch($rule) {
    						case 'min':
    							if(strlen($value) < $rule_value) {
    								$this->addError("{$name} must be a minimum of {$rule_value} characters.");
    							}
    						break;
    
    						case 'max':
    							if(strlen($value) > $rule_value) {
    								$this->addError("{$name} must be a maximum of {$rule_value} characters.");
    							}
    						break;
    
    						case 'matches':
    							if($value != $source[$rule_value]) {
    								$this->addError("{$name} must match {$name}.");
    							}
    						break;
    
    						case 'unique':
    							$check = $this->_db->get($rule_value, array($item, '=', $value));
    							if($check->count()){
    								$this->addError("{$name} already exists.");
    							}
    						break;
    					}
    
    				}
    			}
    		}
    
    		if(empty($this->_errors)) {
    			$this->_passed = true;
    		}
    
    		return $this;
    	}
    I added $name = $rules['name']; in the foreach part of the method so that when the error message is echoed to the user, it will use the 'name' from the registration page that is assigned for a nicer string. I have an addError() method that does this...


    Things like this are echoed nicely: Username is required. Name must be a minimum of 2 characters.

    It was working just great until I use a 'match' check validation.

    In my 'match' switch statement:

    Code:
    case 'matches':
    
    if($value != $source[$rule_value]) {
    $this->addError("{$name} must match {$name}.");
    					}
    break;
    .. I cannot do: $this->addError("{$name} must match {$name}.");

    When it's checking password_again against password it displays this error message:

    "Second Password must match Second Password".

    In actuality it SHOULD say, "Second Password should match First Password".

    I hope I explained this well... can anyone offer advice how to get this to work better?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Validation Class: Trying to Work Our Custom Message

    If the passowrd is the only thing that goes through the matches validation, then just write out the message:

    Code:
    case 'matches':
    
    if($value != $source[$rule_value]) {
    $this->addError("Second password must match the first passwords.");
    					}
    break;
    or

    Code:
    case 'matches':
    
    if($value != $source[$rule_value]) {
    $this->addError("The two passwords do not match.");
    					}
    break;
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Validation Class: Trying to Work Our Custom Message

    Good idea but I'll have other fields use the match validation... like captcha.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Validation Class: Trying to Work Our Custom Message

    where does the "Second password" and "First password" texts come from?

    I wonder if you can do something like this:
    Code:
    case 'matches':
    
    if($value != $source[$rule_value]) {
                  $this->addError("The {$name} fields do not match.");
    }
    break;
    then as long as $name is either Password or Captcha ... or what ever field name... would that work?
    "The Password fields do not match."


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Validation Class: Trying to Work Our Custom Message

    The "Second password" and "First password" texts come from register.php.... you can see I addded a 'name' key to the array. I'm trying to pull that to use in the error.

    I'll have to mess around with it more once I'm home tonight... thanks for your help.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: Validation Class: Trying to Work Our Custom Message

    Got it!!
    Code:
    case 'matches':
    	if ($value != $source[$rule_value]) {
    		$this->addError("{$items[$rule_value]['name']} must match {$rules['name']}.");
    	}
    break;
    Instead of using {$name} all I had to do was use {$rules['name']}.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width