2012-12-04 56 views
0

在Zend的媒體鏈接現有的數據庫表項的檢查我有一個Zend形式與元素$recipe_name如下.. 我要檢查,如果一個_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']); 

我該怎麼做?

回答

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>")) { 
//.............. 
//............. 
} 
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); 

乾杯和快樂的編碼!

+0

感謝您的這個寶貴的答案... – DjangoDev

2

對於這種驗證你要添加一個校驗這樣的..

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

    ) 
); 

這將檢查你的名字,當你從的IsValid檢查表單驗證。

+0

您的答案是最容易閱讀和理解 – Phliplip