0

我有一个带有$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']);  

我怎么能这样做?

4

3 回答 3

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>" )) {
//..............
//.............
}
于 2012-12-05T13:38:30.620 回答
2

对于这种验证,您必须添加一个像这样的验证器..

$recipe_name->addValidator(
    'Db_NoRecordExists', 
    true, 
    array(
        'table' => 'recipes',
        'field' => 'recipe_name',
        'messages' => array( "recordFound" => "Recipe Name already exist ... ! <br>") ,

    )
);

当您从isValid检查表单验证时,这将检查您的姓名。

于 2013-02-13T09:34:26.117 回答
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);

干杯和快乐的编码!

于 2012-12-04T14:03:22.563 回答