Hi Jens
No, just the one validator. Each validator can be passed a 'context'
variable to make the validator aware of other values within the form.
A quick 'n' dirty example:-
<?php
class My_Validate_Login extends Zend_Validate_Abstract
{
const NOT_FOUND = 'notFound';
protected $_messageTemplates = array(
self::NOT_FOUND => 'The supplied username is invalid'
);
public function isValid($value, $context = null)
{
if (!isset($context['password'])) {
throw new Exception('Password field is not present');
}
$adapter = new Zend_Auth_Adapter_DbTable($dbAdapter); // You
could get $dbAdapter from the registry, for example
$adapter
->setTableName('users')
->setIdentityColumn($value)
->setCredentialColumn($context['password']);
$result = $adapter->authenticate();
switch ($result->getCode()) {
// ...your code goes here... for example:-
case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
return self::NOT_FOUND;
break;
}
return true;
}
}
And then simply add this validator to the form's validator path and
attach it to your username field.
Cheers
Wouldn't I need two custom validators, one each for the username and
password
fields?
Also, are you suggesting that I perform the authentication inside
the custom
validator? That doesn't feel very flexible but I don't know, maybe I'm
missing something. Care to provide an example?
I've been trying to think of other use cases for having setters for
the
element errors but I can't say I've thought of any so far. Would it
hurt
though?
/Jens Ljungblad
Simon Mundy wrote:
Hi Jens
As an alternative, you could write your own validator and attach it
to
the username element - the benefit being that you can automatically
return those error messages and also transparently login the user at
the same time.
Cheers
Hello List (and Matthew)
Just started working with Zend_Form and I'm liking it so far. One
thing I'm
wondering about though, which came up when I was working with a
login form
using Zend_Auth. It would be great if there was a method for adding
error
messages to an element with a public method. This example should
explain it
pretty well:
// Execute authentication and save the result
$result = Zend_Auth::getInstance()->authenticate($adapter);
// Handle authentication results
switch ($result->getCode()) {
case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
$form->getElement('username')->addError('The supplied
username is
invalid');
break;
case Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS:
$form->getElement('username')->addError('The supplied
username is
not unique');
break;
case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
$form->getElement('password')->addError('The supplied
password is
invalid');
break;
case Zend_Auth_Result::SUCCESS:
$this->_helper->Redirector('index', 'index');
break;
}
Thank you for an otherwise excellent component!
/Jens Ljungblad
--
View this message in context:
http://www.nabble.com/Zend_Form%3A-adding-error-messages-to-elements-tp17993924p17993924.html
Sent from the Zend Framework mailing list archive at Nabble.com.
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654
4124
http://www.peptolab.com
--
View this message in context:
http://www.nabble.com/Zend_Form%3A-adding-error-messages-to-elements-tp17993924p18003607.html
Sent from the Zend Framework mailing list archive at Nabble.com.
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654
4124
http://www.peptolab.com