Skip to end of metadata
Go to start of metadata

<p>This little object is intended as a canned and generic validator for Zend Framework. It is intended for use with Form Elements, but it should work fairly broadly. The basic need it addresses is to validate that one field value matches another (e.g. password/email and and associated validation field, a captcha challenge, etc.). I looked hard for a ready made answer to this issue, but after much searching and failed experimentation with canned solutions I decided to write my OWN generic answer to the problem.</p>

<p>An example call to this function might look something like this:</p>

<ac:macro ac:name="code"><ac:parameter ac:name="title">PasswordForm</ac:parameter><ac:parameter ac:name="borderStyle">solid</ac:parameter><ac:plain-text-body><![CDATA[
<?php
class PasswordForm extends Zend_Form
{
public function __construct($options = null)

Unknown macro: { parent}

}
?>
]]></ac:plain-text-body></ac:macro>

<p>Aaaaaand the valididator itself:</p>
<ac:macro ac:name="code"><ac:parameter ac:name="title">Zend_Validate_IdenticalField</ac:parameter><ac:parameter ac:name="borderStyle">solid</ac:parameter><ac:plain-text-body><![CDATA[

<?php

require_once 'Zend/Validate/Abstract.php';

class Zend_Validate_IdenticalField extends Zend_Validate_Abstract {
const NOT_MATCH = 'notMatch';
const MISSING_FIELD_NAME = 'missingFieldName';
const INVALID_FIELD_NAME = 'invalidFieldName';

/**

  • @var array
    */
    protected $_messageTemplates = array(
    self::MISSING_FIELD_NAME =>
    'DEVELOPMENT ERROR: Field name to match against was not provided.',
    self::INVALID_FIELD_NAME =>
    'DEVELOPMENT ERROR: The field "%fieldName%" was not provided to match against.',
    self::NOT_MATCH =>
    'Does not match %fieldTitle%.'
    );

/**

  • @var array
    */
    protected $_messageVariables = array(
    'fieldName' => '_fieldName',
    'fieldTitle' => '_fieldTitle'
    );

/**

  • Name of the field as it appear in the $context array.
    *
  • @var string
    */
    protected $_fieldName;

/**

  • Title of the field to display in an error message.
    *
  • If evaluates to false then will be set to $this->_fieldName.
    *
  • @var string
    */
    protected $_fieldTitle;

/**

  • Sets validator options
    *
  • @param string $fieldName
  • @param string $fieldTitle
  • @return void
    */
    public function __construct($fieldName, $fieldTitle = null)
    Unknown macro: { $this->setFieldName($fieldName); $this->setFieldTitle($fieldTitle); }

/**

  • Returns the field name.
    *
  • @return string
    */
    public function getFieldName()
    Unknown macro: { return $this->_fieldName; }

/**

  • Sets the field name.
    *
  • @param string $fieldName
  • @return Zend_Validate_IdenticalField Provides a fluent interface
    */
    public function setFieldName($fieldName)
    Unknown macro: { $this->_fieldName = $fieldName; return $this; }

/**

  • Returns the field title.
    *
  • @return integer
    */
    public function getFieldTitle()
    Unknown macro: { return $this->_fieldTitle; }

/**

  • Sets the field title.
    *
  • @param string:null $fieldTitle
  • @return Zend_Validate_IdenticalField Provides a fluent interface
    */
    public function setFieldTitle($fieldTitle = null)
    Unknown macro: { $this->_fieldTitle = $fieldTitle ? $fieldTitle }

/**

  • Defined by Zend_Validate_Interface
    *
  • Returns true if and only if a field name has been set, the field name is available in the
  • context, and the value of that field name matches the provided value.
    *
  • @param string $value
    *
  • @return boolean
    */
    public function isValid($value, $context = null) {
    $this->_setValue($value);
    $field = $this->getFieldName();

if (empty($field))

Unknown macro: { $this->_error(self}

elseif (!isset($context[$field]))

Unknown macro: { $this->_error(self}

elseif (is_array($context)) {
if ($value == $context[$field])

Unknown macro: { return true; }

} elseif (is_string($context) && ($value == $context))

Unknown macro: { return true; }

$this->_error(self::NOT_MATCH);
return false;
}
}
?>
]]></ac:plain-text-body></ac:macro>

<p>For the sake of simplicity I have renamed the objects here to use the Zend namespace as explaining the eccentricities of extending objects and registering paths in Zend goes well beyond the scope of what I wish to cover in this article. In practice, one should not plop this object into the Zend/Validate directory but should create a different path in the library and register that path with the appropriate Zend objects.</p>

<p><strong>Back Reference:</strong> ref: <a class="external-link" href="http://www.emanaton.com/node/38">http://www.emanaton.com/node/38</a> </p>

Labels:
validate validate Delete
form form Delete
element element Delete
example example Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.