SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)

    Zend: Use of validator from within a controller

    What I have here works great except I can only retrieve a boolean response and not the error message. This exact validator sends the errors to a form when repopulated somehow. How can I access these errors from within the controller?

    Controller:
    Code PHP:
    $loader = new Zend_Loader_PluginLoader();
    $loader->addPrefixPath('My_Validate', 'My/Validate/');
    $loader->load('Username');
    $validUser = new My_Validate_Username();
    $result = $validUser->isValid($user); //boolean response, works fine

    Validator:
    Code PHP:
    class My_Validate_Username extends Zend_Validate_Abstract {
     
        const MSG_TAKEN = 'msgTaken';
        const MSG_MINIMUM = 'msgMinimum';
        const MSG_MAXIMUM = 'msgMaximum';
        public $minimum = 4;
        public $maximum = 32;
     
        protected $_messageVariables = array(
            'min' => 'minimum',
            'max' => 'maximum'
        );
     
        protected $_messageTemplates = array(
            self::MSG_TAKEN => "'%value%' is already taken.",
            self::MSG_MINIMUM => "'%value%' must be at least %min% characters long.",
            self::MSG_MAXIMUM => "'%value%' must be no more than %max% characters long."
        );
     
        public function isValid($value) {
            $this->_setValue($value);
     
            $query = new Application_Model_DbTable_Users();
            $result = $query->getUser($value);
            if(count($result) > 0) {
                $this->_error(self::MSG_TAKEN);
                return false;
            }
     
            if (strlen($value) < $this->minimum) {
                $this->_error(self::MSG_MINIMUM);
                return false;
            }
     
            if (strlen($value) > $this->maximum) {
                $this->_error(self::MSG_MAXIMUM);
                return false;
            }
     
            return true;
        }
    }

  2. #2
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are returning "true" or "false" in your validator instead of an actual variable.

    Try...

    return $this->_error(self::MSG_MINIMUM);

    instead of "return false;"

    same for "return true;".. use an actual variable.

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    This is part of the Zend Framework. That validator is set up exactly how it has to be to return a boolean response AS WELL AS the error message to a form, and it DOES work. I'd like to not make another function with the exact some code if I don't have to.

  4. #4
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand but if your validator is only returning boolean variables then how do you expect to get a string out of it?

    Your only other option would be to create another variable inside the validator and call it seperately.

    For example...

    if (strlen($value) < $this->minimum){
    $this->_error(self::MSG_MINIMUM);
    $newvariable = $this->_error(self::MSG_MINIMUM);
    return false;
    }

    Then use $newvariable wherever you need it.

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    $this->_error has to be sent somewhere for the form to recover it. No chance it can be recovered from the controller?


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
  •