问题
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
andvalue
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:
- 'table' => The database table to validate against
- 'schema' => The schema keys
- 'field' => The field to check for a match
- 'exclude' => An optional where clause or field/value pair to exclude from the query
- '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