1

I want to add check for more than one values in where clause. Db_RecordExists just check for one filed. I have read that you can extend zend validate abstract class and you can have your own validator. can I have one little example about this please.

Thank you...

Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62

2 Answers2

4

What are you trying to do exactly? To me, you don't even need a custom validator.

If you read carefully the source code of Zend_Validate_Db_Abstract you will notice this phpDoc above the constructor:

Provides basic configuration for use with Zend_Validate_Db Validators Setting $exclude allows a single record to be excluded from matching. Exclude can either be a String containing a where clause, or an array with field and value keys to define the where clause added to the sql. A database adapter may optionally be supplied to avoid using the registered default adapter.

The following option keys are supported:

  1. 'table' => The database table to validate against
  2. 'schema' => The schema keys
  3. 'field' => The field to check for a match
  4. 'exclude' => An optional where clause or field/value pair to exclude from the query
  5. 'adapter' => An optional database adapter to use

Which means that if you want to check if a record exists using more than one value, you can do it simply in passing the where clause to the validator instead of a pair field/value:

$where = 'user_id != 110 AND email != "email@example.com"';
$where = array('users', 'email', $where);

$element->addValidator('db_NoRecordExists', true, $where)

This will basically check if a record exists in the users table, and exclude rows where user id != 110 or email@example.com. Naturally, I recommend you to use Zend_Db methods such as quoteIdentifier() in order to generate a fully escaped query expression.

You can add as many fields as you want of course.

You can find more information about Db_NoRecordExists in the documentation.

Liyali
  • 5,643
  • 2
  • 26
  • 40
  • thank you. I was hoping your answer here. Please spare some time for me. I want to know about Bootstrap.php. Thank you. – Rajan Rawal Mar 02 '12 at 08:52
  • Hey @Liyali I want user entered password to match against hashed password in table. How to do it? – Rajan Rawal Mar 02 '12 at 08:59
  • Since you did not indicate where you want to do this, and I presume you don't want to do it in a form validator, so you can use `Zend_Auth` ;) [Read more about it here](http://framework.zend.com/manual/en/zend.auth.html). – Liyali Mar 02 '12 at 09:03
1

All you usually have to do is override the isValid() method to make a custom validator,
here is an example of a custom validator:

<?php

class My_Validator_Usphone extends Zend_Validate_Abstract {
    const PHONE = 'phone';

    protected $_messageTemplates = array(
        self::PHONE => "'%value%' is not a valid U.S. phone number.
            Phone number must be entered in (xxx)xxx-xxxx or xxx-xxx-xxxx format."
    );

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

        $isValid = TRUE;
        $pattern = ('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/');
        if (!preg_match($pattern, $value)) {
            $this->_error(self::PHONE);
            $isValid = FALSE;
        }
        return $isValid;
    }

}

Db_RecordExists is pretty simple but it extends Zend_Validate_Db_Abstract and should be pretty easy to modify to validate against two fields, but you may have to override isValid() and getSelect() or _query() to accept more then one value.

class Zend_Validate_Db_RecordExists extends Zend_Validate_Db_Abstract
{
    public function isValid($value)
    {
        $valid = true;
        $this->_setValue($value);

        $result = $this->_query($value);
        if (!$result) {
            $valid = false;
            $this->_error(self::ERROR_NO_RECORD_FOUND);
        }

        return $valid;
    }
}
RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • I have three questions 1. Where to put this class in zend structure? 2. I am using a form by extending zend form. So how to use it there? 3. Once form is post how can I pass required values to this class? Thank you for your time and help. – Rajan Rawal Mar 02 '12 at 08:54