How to modify zend Db_RecordExists validator where clause?

断了今生、忘了曾经 提交于 2019-12-23 03:31:06

问题


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...


回答1:


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.




回答2:


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;
    }
}


来源:https://stackoverflow.com/questions/9529500/how-to-modify-zend-db-recordexists-validator-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!