www.pokeroconnor.com

Zend Form Password

May16

When using Zend Form, and you have a password field and confirm password field, it is very handy to use Validation Context. It is described in the Zend documentation, just search for “My_Validate_PasswordConfirmation”. As is typical in Zend docs, the example is shit, they just give a class definition. So, here’s a brief example of I used it.

Basically what you need to do is attach the validator to both the password and the confirm password field. 

So firstly, add the class:

class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';

protected $_messageTemplates = array(
self::NOT_MATCH => 'Password confirmation does not match'
);

public function isValid($value, $context = null)
{
$value = (string) $value;
$this->_setValue($value);

if (is_array($context)) {
if (isset($context['password_confirm'])
&& ($value == $context['password_confirm']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}

 

 

$this->_error(self::NOT_MATCH);
return false;
}
}

Then, in your form file, use it something like follows:

class RegisterForm extends Zend_Dojo_Form

{

public function init()

{
$form = new Zend_Form();
....
....
....
$custom_pass = new MyValidatePasswordConfirmation();

...
...
$password = $form->createElement('password', 'password')
->addValidator('StringLength', false, array(1,24))
->setLabel('Choose your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

 

 

$password_confirm = $form->createElement('password', 'password_confirm')
->addValidator('StringLength', false, array(1,24))
->setLabel('Confirm your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

It’s fairly straightforward, but of course there’s very few examples I could find, this is probably the best.

//
posted under MySQL, php | 1 Comment »

Zend Form Validators

May1

I have another post with info on Zend validators, but here’s a very handy class to specify a maximum and minimum value to validate against. E.g. you have a form value that must be between 1 and 10 – Zend Validate doesn’t have a validator for that, but this class will allow you to. Just set $minimum and $maximum to your limits.


class MyValidNumericBetween extends Zend_Validate_Abstract
{
const MSG_NUMERIC = 'msgNumeric';
const MSG_MINIMUM = 'msgMinimum';
const MSG_MAXIMUM = 'msgMaximum';

public $minimum = 1;
public $maximum = 45;

protected $_messageVariables = array(
'min' => 'minimum',
'max' => 'maximum'
);

protected $_messageTemplates = array(
self::MSG_NUMERIC => "'%value%' is not numeric",
self::MSG_MINIMUM => "'%value%' must be at least '%min%'",
self::MSG_MAXIMUM => "'%value%' must be no more than '%max%'"
);

public function isValid($value)
{
$this->_setValue($value);

if (!is_numeric($value)) {
$this->_error(self::MSG_NUMERIC);
return false;
}

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

if ($value > $this->maximum) {
$this->_error(self::MSG_MAXIMUM);
return false;
}

 

 

 

return true;
}
}

And to use this class, assuming you have a normal form instantiated with $form = new Zend_Form(), just do:


$custom_valid8r = new MyValidNumericBetween();

$ball1 = $form->createElement('text', 'ball1')
->addValidator($custom_valid8r)
->setRequired(true);

And to set a custom message here are 2 links with good examples, some of them re-printed below.
Firstly:

$validator = new Zend_Validate_StringLength(8);

$validator->setMessage(
'The string \'%value%\' is too short; it must be at least %min% ' .
'characters',
Zend_Validate_StringLength::TOO_SHORT);

if (!$validator->isValid('word')) {
$messages = $validator->getMessages();
echo current($messages);

// "The string 'word' is too short; it must be at least 8 characters"
}


Secondly:

$validator = new Zend_Validate_StringLength(8, 12);

$validator->setMessages( array(
Zend_Validate_StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
Zend_Validate_StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
));

$validator = new Zend_Validate_StringLength(8, 12);

 

 

if (!validator->isValid('word')) {
echo 'Word failed: '
. $validator->value
. '; its length is not between '
. $validator->min
. ' and '
. $validator->max
. "\n";
}

//
posted under php | 3 Comments »

jQuery Access Last Element

May1

Just a quick one, if you want to manipulate the last instance of an element (or any element) on a page, you can do this SO easily and quickly with jQuery.

$(function() {
	  $("dt:last").addClass("last");
	});

This (obviously!) adds the class ‘last’ to the final dt element in the document. The cool thing is you could use $(“dt:last-1″) to access the second dt element, so flexible and easy to use…

//