Pergunta

I'm a beginner and I followed the tutorial on zend framework website to create a simple web application from a skeleton one. I'm trying to add further elements to the form proposed by the tutorial, expecially an 'image' capctha, but, when I try to render it in the appropriate view, I get this fatal error:

Fatal error: Call to undefined method Zend\Form\Element::getCaptcha() in C:\xampp\htdocs\new\vendor\zendframework\zendframework\library\Zend\Form\View\Helper\FormCaptcha.php on line 45.

This is the code related to the albumform class:

 <?php

    namespace Album\Form;



     use Zend\Form\Element;
     use Zend\Form\Form;
     use Zend\Captcha\Image;
     use Zend\Captcha\AdapterInterface;

     class AlbumForm extends Form
     {
         protected $captcha; 
         public function __construct($name = null)
         {
             // we want to ignore the name passed
             parent::__construct('album');

             $this->captcha = new Image(array(
                'expiration' => '300',
                'wordlen' => '7',
                'font' => 'public/fonts/glyphicons-halflings-regular.tff',
                'fontSize' => '20',
                'imgDir' => 'public/captcha',
                'imgUrl' => '/captcha'
            ));

             $this->add(array(
                 'name' => 'id',
                 'type' => 'Hidden',
             ));
             $this->add(array(
                 'name' => 'title',
                 'type' => 'Text',
                 'options' => array(
                     'label' => 'Title',
                 ),
                 'attributes' => array(
                     'value' => 'Scrivi il titolo',
                     'class' => 'prova',
                 ),
             ));
             $this->add(array(
                 'name' => 'artist',
                 'type' => 'Text',
                 'options' => array(
                     'label' => 'Artist',
                 ),
             ));

              $this->add(array(
                'name' => 'captcha',
                'attributes' => array(
                   'type' => 'Captcha'
                ),
                'options' => array(
                   'label' => 'Please verify you are human.',
                   'captcha' => $this->captcha
                )
             ));

             $this->add(array(
                 'name' => 'submit',
                 'type' => 'Submit',
                 'attributes' => array(
                     'value' => 'Go',
                     'id' => 'submitbutton',
                 ),
             ));

         }
     }

This is the code within the view:

     <?php
     $form->setAttribute('action', $this->url('album', array('action' => 'add')));
     $form->prepare();

     echo $this->form()->openTag($form) . "\n";
     echo $this->formHidden($form->get('id')) . "\n";
     echo $this->formLabel($form->get('title')) . "\n";
     ?>
     <br/>
     <?php
     echo $this->formInput($form->get('title')) . "\n";
     ?>
     <br/><br/>
     <?php
     echo $this->formLabel($form->get('artist')) . "\n";
     ?>
     <br/>
     <?php
     echo $this->formInput($form->get('artist')) . "\n"; 
     ?>
     <br/><br/>
     <?php
     echo $this->formCaptcha($form->get('captcha')) . "\n"; 
     echo $this->formSubmit($form->get('submit')) . "\n"; 
     echo $this->form()->closeTag() . "\n";
     ?>

Can you please help me? What is missing?

Thanks!

Foi útil?

Solução

The problem is in your element definition. You have:

$this->add(array(
    'name' => 'captcha',
    'attributes' => array(
        'type' => 'Captcha'
    ),
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha
    )
));

but the 'type' is in the wrong place. It should be:

$this->add(array(
    'name' => 'captcha',
    'type' => 'Captcha'
    'options' => array(
        'label' => 'Please verify you are human.',
        'captcha' => $this->captcha
    )
));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top