我有一个带有元素$recipe_name
的zend表单,如下所示。
我想使用验证检查_recipename
是否已存在(Zend_Validate_DbNoRecordExists)。
$recipe_name= $this->createElement('text',$i.'_recipename',array('label'=> "Extra Item Name in ".$data['language'].'', 'class'=> 'inp-form',) );
$recipe_name->setDecorators(array( 'ViewHelper',
array(array('data'=>'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td','style')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','openOnly'=>true))));
$recipe_name->setRequired(true);
$recipe_name->addValidator('NotEmpty',true);
$recipe_name->getValidator('NotEmpty')->setMessage("Please enter Recipe section name in ".$data['language']);
我怎么能这样做?
答案 0 :(得分:3)
$clause = $db->quoteInto ( '<your column Name> ?', "<Your Value>" );
$validator = new Zend_Validate_Db_RecordExists ( array ('table' => '<Table Name>', 'field' => 'Field Name', 'exclude' => $clause ) );
if ($validator->isValid ( "<value to validate>" )) {
//..............
//.............
}
答案 1 :(得分:2)
是的! Zend_Validate_Db_NoRecordExists
将满足您的需求。你应该做类似下面的事情:
$db_lookup_validator = new Zend_Validate_Db_NoRecordExists('<your table name>', '<column name>');
$your_field = new Zend_Form_Element_Text('<your form element name>'); // you already created an element, so you can skip this line.
$your_field->addValidator($db_lookup_validator);
欢呼和快乐的编码!
答案 2 :(得分:2)
对于这种验证,您必须添加这样的验证器..
$recipe_name->addValidator(
'Db_NoRecordExists',
true,
array(
'table' => 'recipes',
'field' => 'recipe_name',
'messages' => array( "recordFound" => "Recipe Name already exist ... ! <br>") ,
)
);
当您从 isValid 检查表单验证时,这将检查您的姓名。