0

i have a zend form with an element $recipe_name as follows.. i want to check if a _recipename already exists using validation(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']);  

How I can do that?

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
DjangoDev
  • 889
  • 6
  • 16
  • 25

3 Answers3

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

Yes! Zend_Validate_Db_NoRecordExists will serve your need. You should do something like bellow:

    $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);

Cheers and happy coding!

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
2

For this kind of validation you have to add a validator like this..

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

    )
);

This will check your name when you are check form validation from isValid.

Nilesh Gupta
  • 367
  • 1
  • 2