Zend_Form without MVC problem

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend_Form without MVC problem

Paul Cook-4
I'm trying to use components of the Framework in a project that currently is not MVC.  Eventually I'd like to refactor to MVC but that is not possible right now.

I'm using Zend_Form and ran into an error that I can't figure out.  I'm trying to use the ViewScript decorator and I've read through the Decorators with Zend Form arcticle and scoured the web but now I'm stuck.

The error I get is: Warning: ViewHelper decorator cannot render without a registered view object

Some code: formt_test.php (in tests folder and main script)

require_once('Zend/View.php');
require_once('TestForm.php');

$form = new TestForm();

echo $form->render(new Zend_View(array('basePath'=>'/path/to/tests/views')));

TestForm.php

require_once 'Zend/Form.php';
require_once 'Zend/Form/Element/Select.php';

class TestForm extends Zend_Form
{
        public function init()
        {
            $this->setAction("/test_action");
            $this->setMethod("post");
           
            $statuses = array(
                'Booked'=>'Booked',
                'Cancelled'=>'Cancelled',
                'Pending'=>'Pending',
                );

            $types = array(
                'Public'=>'Public',
                'Private'=>'Private'
            );
           
            $this->addElement(new Zend_Form_Element_Select('status'));
            $this->addElement(new Zend_Form_Element_Select('event_type'));
           
            $this->getElement('status')->addMultiOptions($statuses);
            $this->getElement('event_type')->addMultiOptions($types);
           
            $this->setDecorators(array(
                array('ViewScript', array('viewScript'=>'test_form.phtml'))
            ));
           
        }
}

test_form.phtml

<form action="<?= $this->escape($this->element->getAction()) ?>" method="<?= $this->escape($this->element->getMethod()) ?>">

            <fieldset>
            <legend>Test Form</legend>
                    <table id='edit_test' style='width: 650px;'>
                    <tr>
                        <td class='edit_mem'>Event Status:<br /><?= $this->element->status ?></td>
                        <td class='edit_mem'>Event Type:<br /><?= $this->element->event_type ?></td>
                    </tr>
                    </table>
                    </fieldset>
                </fieldset>
</form>


Any help would be greatly appreciated.  Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

weierophinney
Administrator
-- Paul Cook <[hidden email]> wrote
(on Wednesday, 21 May 2008, 02:00 PM -0400):

> I'm trying to use components of the Framework in a project that currently is
> not MVC.  Eventually I'd like to refactor to MVC but that is not possible right
> now.
>
> I'm using Zend_Form and ran into an error that I can't figure out.  I'm trying
> to use the ViewScript decorator and I've read through the Decorators with Zend
> Form arcticle and scoured the web but now I'm stuck.
>
> The error I get is: Warning: ViewHelper decorator cannot render without a
> registered view object
>
> Some code: formt_test.php (in tests folder and main script)
>
> require_once('Zend/View.php');
> require_once('TestForm.php');
>
> $form = new TestForm();
>
> echo $form->render(new Zend_View(array('basePath'=>'/path/to/tests/views')));

Hmm. I see what the issue is here. Override the setView() method of your
form object with the following:

    public function setView(Zend_View_Interface $view)
    {
        parent::setView($view);
        foreach ($this as $item) {
            $item->setView($view);
        }
        return $this;
    }

That _should_ correct the issue for you.

The problem you're running into is that the form object has the view
object, but it's not injecting into its elements, display groups, etc.
The above should take care of that.


> TestForm.php
>
> require_once 'Zend/Form.php';
> require_once 'Zend/Form/Element/Select.php';
>
> class TestForm extends Zend_Form
> {
>         public function init()
>         {
>             $this->setAction("/test_action");
>             $this->setMethod("post");
>            
>             $statuses = array(
>                 'Booked'=>'Booked',
>                 'Cancelled'=>'Cancelled',
>                 'Pending'=>'Pending',
>                 );
>
>             $types = array(
>                 'Public'=>'Public',
>                 'Private'=>'Private'
>             );
>            
>             $this->addElement(new Zend_Form_Element_Select('status'));
>             $this->addElement(new Zend_Form_Element_Select('event_type'));
>            
>             $this->getElement('status')->addMultiOptions($statuses);
>             $this->getElement('event_type')->addMultiOptions($types);
>            
>             $this->setDecorators(array(
>                 array('ViewScript', array('viewScript'=>'test_form.phtml'))
>             ));
>            
>         }
> }
>
> test_form.phtml
>
> <form action="<?= $this->escape($this->element->getAction()) ?>" method="<?=
> $this->escape($this->element->getMethod()) ?>">
>
>             <fieldset>
>             <legend>Test Form</legend>
>                     <table id='edit_test' style='width: 650px;'>
>                     <tr>
>                         <td class='edit_mem'>Event Status:<br /><?= $this->
> element->status ?></td>
>                         <td class='edit_mem'>Event Type:<br /><?= $this->
> element->event_type ?></td>
>                     </tr>
>                     </table>
>                     </fieldset>
>                 </fieldset>
> </form>
>
>
> Any help would be greatly appreciated.  Thanks in advance.

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend - The PHP Company   | http://www.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

drj201
Hello,

I am having the same problem as first outlined in this thread.

The solution Matthew posted does not work. Can anyone help?

Thanks,

David


Matthew Weier O'Phinney-3 wrote
-- Paul Cook <paul.pwc@gmail.com> wrote
(on Wednesday, 21 May 2008, 02:00 PM -0400):
> I'm trying to use components of the Framework in a project that currently is
> not MVC.  Eventually I'd like to refactor to MVC but that is not possible right
> now.
>
> I'm using Zend_Form and ran into an error that I can't figure out.  I'm trying
> to use the ViewScript decorator and I've read through the Decorators with Zend
> Form arcticle and scoured the web but now I'm stuck.
>
> The error I get is: Warning: ViewHelper decorator cannot render without a
> registered view object
>
> Some code: formt_test.php (in tests folder and main script)
>
> require_once('Zend/View.php');
> require_once('TestForm.php');
>
> $form = new TestForm();
>
> echo $form->render(new Zend_View(array('basePath'=>'/path/to/tests/views')));

Hmm. I see what the issue is here. Override the setView() method of your
form object with the following:

    public function setView(Zend_View_Interface $view)
    {
        parent::setView($view);
        foreach ($this as $item) {
            $item->setView($view);
        }
        return $this;
    }

That _should_ correct the issue for you.

The problem you're running into is that the form object has the view
object, but it's not injecting into its elements, display groups, etc.
The above should take care of that.


> TestForm.php
>
> require_once 'Zend/Form.php';
> require_once 'Zend/Form/Element/Select.php';
>
> class TestForm extends Zend_Form
> {
>         public function init()
>         {
>             $this->setAction("/test_action");
>             $this->setMethod("post");
>            
>             $statuses = array(
>                 'Booked'=>'Booked',
>                 'Cancelled'=>'Cancelled',
>                 'Pending'=>'Pending',
>                 );
>
>             $types = array(
>                 'Public'=>'Public',
>                 'Private'=>'Private'
>             );
>            
>             $this->addElement(new Zend_Form_Element_Select('status'));
>             $this->addElement(new Zend_Form_Element_Select('event_type'));
>            
>             $this->getElement('status')->addMultiOptions($statuses);
>             $this->getElement('event_type')->addMultiOptions($types);
>            
>             $this->setDecorators(array(
>                 array('ViewScript', array('viewScript'=>'test_form.phtml'))
>             ));
>            
>         }
> }
>
> test_form.phtml
>
> <form action="<?= $this->escape($this->element->getAction()) ?>" method="<?=
> $this->escape($this->element->getMethod()) ?>">
>
>             <fieldset>
>             <legend>Test Form</legend>
>                     >                     >                         >                         >                     >                    
Event Status:<br /><?= $this->
> element->status ?>
Event Type:<br /><?= $this->
> element->event_type ?>
>                     </fieldset>
>                 </fieldset>
> </form>
>
>
> Any help would be greatly appreciated.  Thanks in advance.

--
Matthew Weier O'Phinney
Software Architect       | matthew@zend.com
Zend - The PHP Company   | http://www.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

weierophinney
Administrator
-- drj201 <[hidden email]> wrote
(on Friday, 10 April 2009, 06:14 AM -0700):
> I am having the same problem as first outlined in this thread.
>
> The solution Matthew posted does not work. Can anyone help?

Can you provide more information? How are you setting up your form, what
kind of view object are you using (if any)? What error messages are you
getting...?


> Matthew Weier O'Phinney-3 wrote:
> >
> > -- Paul Cook <[hidden email]> wrote
> > (on Wednesday, 21 May 2008, 02:00 PM -0400):
> >> I'm trying to use components of the Framework in a project that currently
> >> is
> >> not MVC.  Eventually I'd like to refactor to MVC but that is not possible
> >> right
> >> now.
> >>
> >> I'm using Zend_Form and ran into an error that I can't figure out.  I'm
> >> trying
> >> to use the ViewScript decorator and I've read through the Decorators with
> >> Zend
> >> Form arcticle and scoured the web but now I'm stuck.
> >>
> >> The error I get is: Warning: ViewHelper decorator cannot render without a
> >> registered view object
> >>
> >> Some code: formt_test.php (in tests folder and main script)
> >>
> >> require_once('Zend/View.php');
> >> require_once('TestForm.php');
> >>
> >> $form = new TestForm();
> >>
> >> echo $form->render(new
> >> Zend_View(array('basePath'=>'/path/to/tests/views')));
> >
> > Hmm. I see what the issue is here. Override the setView() method of your
> > form object with the following:
> >
> >     public function setView(Zend_View_Interface $view)
> >     {
> >         parent::setView($view);
> >         foreach ($this as $item) {
> >             $item->setView($view);
> >         }
> >         return $this;
> >     }
> >
> > That _should_ correct the issue for you.
> >
> > The problem you're running into is that the form object has the view
> > object, but it's not injecting into its elements, display groups, etc.
> > The above should take care of that.
> >
> >
> >> TestForm.php
> >>
> >> require_once 'Zend/Form.php';
> >> require_once 'Zend/Form/Element/Select.php';
> >>
> >> class TestForm extends Zend_Form
> >> {
> >>         public function init()
> >>         {
> >>             $this->setAction("/test_action");
> >>             $this->setMethod("post");
> >>            
> >>             $statuses = array(
> >>                 'Booked'=>'Booked',
> >>                 'Cancelled'=>'Cancelled',
> >>                 'Pending'=>'Pending',
> >>                 );
> >>
> >>             $types = array(
> >>                 'Public'=>'Public',
> >>                 'Private'=>'Private'
> >>             );
> >>            
> >>             $this->addElement(new Zend_Form_Element_Select('status'));
> >>             $this->addElement(new
> >> Zend_Form_Element_Select('event_type'));
> >>            
> >>             $this->getElement('status')->addMultiOptions($statuses);
> >>             $this->getElement('event_type')->addMultiOptions($types);
> >>            
> >>             $this->setDecorators(array(
> >>                 array('ViewScript',
> >> array('viewScript'=>'test_form.phtml'))
> >>             ));
> >>            
> >>         }
> >> }
> >>
> >> test_form.phtml
> >>
> >> <form action="<?= $this->escape($this->element->getAction()) ?>"
> >> method="<?=
> >> $this->escape($this->element->getMethod()) ?>">
> >>
> >>             <fieldset>
> >>             <legend>Test Form</legend>
> >>                     <table id='edit_test' style='width: 650px;'>
> >>                     <tr>
> >>                         <td class='edit_mem'>Event Status:<br /><?=
> >> $this->
> >> element->status ?></td>
> >>                         <td class='edit_mem'>Event Type:<br /><?= $this->
> >> element->event_type ?></td>
> >>                     </tr>
> >>                     </table>
> >>                     </fieldset>
> >>                 </fieldset>
> >> </form>
> >>
> >>
> >> Any help would be greatly appreciated.  Thanks in advance.
> >
> > --
> > Matthew Weier O'Phinney
> > Software Architect       | [hidden email]
> > Zend - The PHP Company   | http://www.zend.com/
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p22987792.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

--
Matthew Weier O'Phinney
Software Architect      | [hidden email]
Zend Framework          | http://framework.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

drj201
Hi Matthew,

Thank you for your quick reply. I am using Zend_Form in an almost identical fashion as outlined in the original post of this thread.

I have a class My_Form that extends Zend_Form. My_Form constructs the form elements as well as setting up the form to use a ViewScript i.e.:

$this->setDecorators(array(array('ViewScript', array('viewScript'=>'contact.phtml'))));

The My_Form class also instructs the form to render using an instance of Zend_View which simply has set the basePath.

In my standard PHP page file I create a new instance of My_Form and echo/render the result.

The error I receive when trying to render my form is the same as the original post:

Warning: ViewHelper decorator cannot render without a registered view object

My ViewScript echos my elements as follows:

<?= $this->element->firstname ?>

If I remove the instruction to use a ViewScript the form renders and displays corrected. I wish to use a ViewScript however to better construct my form.

Any help greatly appreciated. Has anyone managed to use a ViewScript with Zend_Form without MVC?

Thanks.

David





Matthew Weier O'Phinney-3 wrote
-- drj201 <drj201@gmail.com> wrote
(on Friday, 10 April 2009, 06:14 AM -0700):
> I am having the same problem as first outlined in this thread.
>
> The solution Matthew posted does not work. Can anyone help?

Can you provide more information? How are you setting up your form, what
kind of view object are you using (if any)? What error messages are you
getting...?


> Matthew Weier O'Phinney-3 wrote:
> >
> > -- Paul Cook <paul.pwc@gmail.com> wrote
> > (on Wednesday, 21 May 2008, 02:00 PM -0400):
> >> I'm trying to use components of the Framework in a project that currently
> >> is
> >> not MVC.  Eventually I'd like to refactor to MVC but that is not possible
> >> right
> >> now.
> >>
> >> I'm using Zend_Form and ran into an error that I can't figure out.  I'm
> >> trying
> >> to use the ViewScript decorator and I've read through the Decorators with
> >> Zend
> >> Form arcticle and scoured the web but now I'm stuck.
> >>
> >> The error I get is: Warning: ViewHelper decorator cannot render without a
> >> registered view object
> >>
> >> Some code: formt_test.php (in tests folder and main script)
> >>
> >> require_once('Zend/View.php');
> >> require_once('TestForm.php');
> >>
> >> $form = new TestForm();
> >>
> >> echo $form->render(new
> >> Zend_View(array('basePath'=>'/path/to/tests/views')));
> >
> > Hmm. I see what the issue is here. Override the setView() method of your
> > form object with the following:
> >
> >     public function setView(Zend_View_Interface $view)
> >     {
> >         parent::setView($view);
> >         foreach ($this as $item) {
> >             $item->setView($view);
> >         }
> >         return $this;
> >     }
> >
> > That _should_ correct the issue for you.
> >
> > The problem you're running into is that the form object has the view
> > object, but it's not injecting into its elements, display groups, etc.
> > The above should take care of that.
> >
> >
> >> TestForm.php
> >>
> >> require_once 'Zend/Form.php';
> >> require_once 'Zend/Form/Element/Select.php';
> >>
> >> class TestForm extends Zend_Form
> >> {
> >>         public function init()
> >>         {
> >>             $this->setAction("/test_action");
> >>             $this->setMethod("post");
> >>            
> >>             $statuses = array(
> >>                 'Booked'=>'Booked',
> >>                 'Cancelled'=>'Cancelled',
> >>                 'Pending'=>'Pending',
> >>                 );
> >>
> >>             $types = array(
> >>                 'Public'=>'Public',
> >>                 'Private'=>'Private'
> >>             );
> >>            
> >>             $this->addElement(new Zend_Form_Element_Select('status'));
> >>             $this->addElement(new
> >> Zend_Form_Element_Select('event_type'));
> >>            
> >>             $this->getElement('status')->addMultiOptions($statuses);
> >>             $this->getElement('event_type')->addMultiOptions($types);
> >>            
> >>             $this->setDecorators(array(
> >>                 array('ViewScript',
> >> array('viewScript'=>'test_form.phtml'))
> >>             ));
> >>            
> >>         }
> >> }
> >>
> >> test_form.phtml
> >>
> >> <form action="<?= $this->escape($this->element->getAction()) ?>"
> >> method="<?=
> >> $this->escape($this->element->getMethod()) ?>">
> >>
> >>             <fieldset>
> >>             <legend>Test Form</legend>
> >>                     > >>                     > >>                         > >>                         > >>                     > >>                    
Event Status:<br /><?=
> >> $this->
> >> element->status ?>
Event Type:<br /><?= $this->
> >> element->event_type ?>
> >>                     </fieldset>
> >>                 </fieldset>
> >> </form>
> >>
> >>
> >> Any help would be greatly appreciated.  Thanks in advance.
> >
> > --
> > Matthew Weier O'Phinney
> > Software Architect       | matthew@zend.com
> > Zend - The PHP Company   | http://www.zend.com/
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p22987792.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

--
Matthew Weier O'Phinney
Software Architect      | matthew@zend.com
Zend Framework          | http://framework.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

weierophinney
Administrator
-- drj201 <[hidden email]> wrote
(on Monday, 13 April 2009, 01:04 PM -0700):

> Thank you for your quick reply. I am using Zend_Form in an almost identical
> fashion as outlined in the original post of this thread.
>
> I have a class My_Form that extends Zend_Form. My_Form constructs the form
> elements as well as setting up the form to use a ViewScript i.e.:
>
> $this->setDecorators(array(array('ViewScript',
> array('viewScript'=>'contact.phtml'))));
>
> The My_Form class also instructs the form to render using an instance of
> Zend_View which simply has set the basePath.
>
> In my standard PHP page file I create a new instance of My_Form and
> echo/render the result.
>
> The error I receive when trying to render my form is the same as the
> original post:
>
> Warning: ViewHelper decorator cannot render without a registered view object
>
> My ViewScript echos my elements as follows:
>
> <?= $this->element->firstname ?>
>
> If I remove the instruction to use a ViewScript the form renders and
> displays corrected. I wish to use a ViewScript however to better construct
> my form.
>
> Any help greatly appreciated. Has anyone managed to use a ViewScript with
> Zend_Form without MVC?

Are you calling $form->setView(new Zend_View()) at any point (or the
equivalent)?


> Matthew Weier O'Phinney-3 wrote:
> >
> > -- drj201 <[hidden email]> wrote
> > (on Friday, 10 April 2009, 06:14 AM -0700):
> >> I am having the same problem as first outlined in this thread.
> >>
> >> The solution Matthew posted does not work. Can anyone help?
> >
> > Can you provide more information? How are you setting up your form, what
> > kind of view object are you using (if any)? What error messages are you
> > getting...?
> >
> >
> >> Matthew Weier O'Phinney-3 wrote:
> >> >
> >> > -- Paul Cook <[hidden email]> wrote
> >> > (on Wednesday, 21 May 2008, 02:00 PM -0400):
> >> >> I'm trying to use components of the Framework in a project that
> >> currently
> >> >> is
> >> >> not MVC.  Eventually I'd like to refactor to MVC but that is not
> >> possible
> >> >> right
> >> >> now.
> >> >>
> >> >> I'm using Zend_Form and ran into an error that I can't figure out.
> >> I'm
> >> >> trying
> >> >> to use the ViewScript decorator and I've read through the Decorators
> >> with
> >> >> Zend
> >> >> Form arcticle and scoured the web but now I'm stuck.
> >> >>
> >> >> The error I get is: Warning: ViewHelper decorator cannot render
> >> without a
> >> >> registered view object
> >> >>
> >> >> Some code: formt_test.php (in tests folder and main script)
> >> >>
> >> >> require_once('Zend/View.php');
> >> >> require_once('TestForm.php');
> >> >>
> >> >> $form = new TestForm();
> >> >>
> >> >> echo $form->render(new
> >> >> Zend_View(array('basePath'=>'/path/to/tests/views')));
> >> >
> >> > Hmm. I see what the issue is here. Override the setView() method of
> >> your
> >> > form object with the following:
> >> >
> >> >     public function setView(Zend_View_Interface $view)
> >> >     {
> >> >         parent::setView($view);
> >> >         foreach ($this as $item) {
> >> >             $item->setView($view);
> >> >         }
> >> >         return $this;
> >> >     }
> >> >
> >> > That _should_ correct the issue for you.
> >> >
> >> > The problem you're running into is that the form object has the view
> >> > object, but it's not injecting into its elements, display groups, etc.
> >> > The above should take care of that.
> >> >
> >> >
> >> >> TestForm.php
> >> >>
> >> >> require_once 'Zend/Form.php';
> >> >> require_once 'Zend/Form/Element/Select.php';
> >> >>
> >> >> class TestForm extends Zend_Form
> >> >> {
> >> >>         public function init()
> >> >>         {
> >> >>             $this->setAction("/test_action");
> >> >>             $this->setMethod("post");
> >> >>            
> >> >>             $statuses = array(
> >> >>                 'Booked'=>'Booked',
> >> >>                 'Cancelled'=>'Cancelled',
> >> >>                 'Pending'=>'Pending',
> >> >>                 );
> >> >>
> >> >>             $types = array(
> >> >>                 'Public'=>'Public',
> >> >>                 'Private'=>'Private'
> >> >>             );
> >> >>            
> >> >>             $this->addElement(new Zend_Form_Element_Select('status'));
> >> >>             $this->addElement(new
> >> >> Zend_Form_Element_Select('event_type'));
> >> >>            
> >> >>             $this->getElement('status')->addMultiOptions($statuses);
> >> >>             $this->getElement('event_type')->addMultiOptions($types);
> >> >>            
> >> >>             $this->setDecorators(array(
> >> >>                 array('ViewScript',
> >> >> array('viewScript'=>'test_form.phtml'))
> >> >>             ));
> >> >>            
> >> >>         }
> >> >> }
> >> >>
> >> >> test_form.phtml
> >> >>
> >> >> <form action="<?= $this->escape($this->element->getAction()) ?>"
> >> >> method="<?=
> >> >> $this->escape($this->element->getMethod()) ?>">
> >> >>
> >> >>             <fieldset>
> >> >>             <legend>Test Form</legend>
> >> >>                     <table id='edit_test' style='width: 650px;'>
> >> >>                     <tr>
> >> >>                         <td class='edit_mem'>Event Status:<br /><?=
> >> >> $this->
> >> >> element->status ?></td>
> >> >>                         <td class='edit_mem'>Event Type:<br /><?=
> >> $this->
> >> >> element->event_type ?></td>
> >> >>                     </tr>
> >> >>                     </table>
> >> >>                     </fieldset>
> >> >>                 </fieldset>
> >> >> </form>
> >> >>
> >> >>
> >> >> Any help would be greatly appreciated.  Thanks in advance.
> >> >
> >> > --
> >> > Matthew Weier O'Phinney
> >> > Software Architect       | [hidden email]
> >> > Zend - The PHP Company   | http://www.zend.com/
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p22987792.html
> >> Sent from the Zend Framework mailing list archive at Nabble.com.
> >>
> >
> > --
> > Matthew Weier O'Phinney
> > Software Architect      | [hidden email]
> > Zend Framework          | http://framework.zend.com/
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p23027118.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

Mon Zafra
In reply to this post by drj201
If I'm not mistaken, the form's view is not propagated into its elements unless there's a FormElements or PrepareElements in its decorator list. So either add PrepareElements or manually loop through each element and call setView($view) on them.

   -- Mon


On Tue, Apr 14, 2009 at 4:04 AM, drj201 <[hidden email]> wrote:

Hi Matthew,

Thank you for your quick reply. I am using Zend_Form in an almost identical
fashion as outlined in the original post of this thread.

I have a class My_Form that extends Zend_Form. My_Form constructs the form
elements as well as setting up the form to use a ViewScript i.e.:

$this->setDecorators(array(array('ViewScript',
array('viewScript'=>'contact.phtml'))));

The My_Form class also instructs the form to render using an instance of
Zend_View which simply has set the basePath.

In my standard PHP page file I create a new instance of My_Form and
echo/render the result.

The error I receive when trying to render my form is the same as the
original post:

Warning: ViewHelper decorator cannot render without a registered view object

My ViewScript echos my elements as follows:

<?= $this->element->firstname ?>

If I remove the instruction to use a ViewScript the form renders and
displays corrected. I wish to use a ViewScript however to better construct
my form.

Any help greatly appreciated. Has anyone managed to use a ViewScript with
Zend_Form without MVC?

Thanks.

David






Matthew Weier O'Phinney-3 wrote:
>
> -- drj201 <[hidden email]> wrote
> (on Friday, 10 April 2009, 06:14 AM -0700):
>> I am having the same problem as first outlined in this thread.
>>
>> The solution Matthew posted does not work. Can anyone help?
>
> Can you provide more information? How are you setting up your form, what
> kind of view object are you using (if any)? What error messages are you
> getting...?
>
>
>> Matthew Weier O'Phinney-3 wrote:
>> >
>> > -- Paul Cook <[hidden email]> wrote
>> > (on Wednesday, 21 May 2008, 02:00 PM -0400):
>> >> I'm trying to use components of the Framework in a project that
>> currently
>> >> is
>> >> not MVC.  Eventually I'd like to refactor to MVC but that is not
>> possible
>> >> right
>> >> now.
>> >>
>> >> I'm using Zend_Form and ran into an error that I can't figure out.
>> I'm
>> >> trying
>> >> to use the ViewScript decorator and I've read through the Decorators
>> with
>> >> Zend
>> >> Form arcticle and scoured the web but now I'm stuck.
>> >>
>> >> The error I get is: Warning: ViewHelper decorator cannot render
>> without a
>> >> registered view object
>> >>
>> >> Some code: formt_test.php (in tests folder and main script)
>> >>
>> >> require_once('Zend/View.php');
>> >> require_once('TestForm.php');
>> >>
>> >> $form = new TestForm();
>> >>
>> >> echo $form->render(new
>> >> Zend_View(array('basePath'=>'/path/to/tests/views')));
>> >
>> > Hmm. I see what the issue is here. Override the setView() method of
>> your
>> > form object with the following:
>> >
>> >     public function setView(Zend_View_Interface $view)
>> >     {
>> >         parent::setView($view);
>> >         foreach ($this as $item) {
>> >             $item->setView($view);
>> >         }
>> >         return $this;
>> >     }
>> >
>> > That _should_ correct the issue for you.
>> >
>> > The problem you're running into is that the form object has the view
>> > object, but it's not injecting into its elements, display groups, etc.
>> > The above should take care of that.
>> >
>> >
>> >> TestForm.php
>> >>
>> >> require_once 'Zend/Form.php';
>> >> require_once 'Zend/Form/Element/Select.php';
>> >>
>> >> class TestForm extends Zend_Form
>> >> {
>> >>         public function init()
>> >>         {
>> >>             $this->setAction("/test_action");
>> >>             $this->setMethod("post");
>> >>
>> >>             $statuses = array(
>> >>                 'Booked'=>'Booked',
>> >>                 'Cancelled'=>'Cancelled',
>> >>                 'Pending'=>'Pending',
>> >>                 );
>> >>
>> >>             $types = array(
>> >>                 'Public'=>'Public',
>> >>                 'Private'=>'Private'
>> >>             );
>> >>
>> >>             $this->addElement(new Zend_Form_Element_Select('status'));
>> >>             $this->addElement(new
>> >> Zend_Form_Element_Select('event_type'));
>> >>
>> >>             $this->getElement('status')->addMultiOptions($statuses);
>> >>             $this->getElement('event_type')->addMultiOptions($types);
>> >>
>> >>             $this->setDecorators(array(
>> >>                 array('ViewScript',
>> >> array('viewScript'=>'test_form.phtml'))
>> >>             ));
>> >>
>> >>         }
>> >> }
>> >>
>> >> test_form.phtml
>> >>
>> >> <form action="<?= $this->escape($this->element->getAction()) ?>"
>> >> method="<?=
>> >> $this->escape($this->element->getMethod()) ?>">
>> >>
>> >>             <fieldset>
>> >>             <legend>Test Form</legend>
>> >>                     <table id='edit_test' style='width: 650px;'>
>> >>                     <tr>
>> >>                         <td class='edit_mem'>Event Status:<br /><?=
>> >> $this->
>> >> element->status ?></td>
>> >>                         <td class='edit_mem'>Event Type:<br /><?=
>> $this->
>> >> element->event_type ?></td>
>> >>                     </tr>
>> >>                     </table>
>> >>                     </fieldset>
>> >>                 </fieldset>
>> >> </form>
>> >>
>> >>
>> >> Any help would be greatly appreciated.  Thanks in advance.
>> >
>> > --
>> > Matthew Weier O'Phinney
>> > Software Architect       | [hidden email]
>> > Zend - The PHP Company   | http://www.zend.com/
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p22987792.html
>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>
>
> --
> Matthew Weier O'Phinney
> Software Architect      | [hidden email]
> Zend Framework          | http://framework.zend.com/
>
>

--
View this message in context: http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p23027118.html
Sent from the Zend Framework mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

Mon Zafra
Ah, sorry, I missed that part about you trying Matthew's custom setView() method. In that case, I have no idea what's happening.

   -- Mon


On Tue, Apr 14, 2009 at 9:49 AM, Mon Zafra <[hidden email]> wrote:
If I'm not mistaken, the form's view is not propagated into its elements unless there's a FormElements or PrepareElements in its decorator list. So either add PrepareElements or manually loop through each element and call setView($view) on them.

   -- Mon



On Tue, Apr 14, 2009 at 4:04 AM, drj201 <[hidden email]> wrote:

Hi Matthew,

Thank you for your quick reply. I am using Zend_Form in an almost identical
fashion as outlined in the original post of this thread.

I have a class My_Form that extends Zend_Form. My_Form constructs the form
elements as well as setting up the form to use a ViewScript i.e.:

$this->setDecorators(array(array('ViewScript',
array('viewScript'=>'contact.phtml'))));

The My_Form class also instructs the form to render using an instance of
Zend_View which simply has set the basePath.

In my standard PHP page file I create a new instance of My_Form and
echo/render the result.

The error I receive when trying to render my form is the same as the
original post:

Warning: ViewHelper decorator cannot render without a registered view object

My ViewScript echos my elements as follows:

<?= $this->element->firstname ?>

If I remove the instruction to use a ViewScript the form renders and
displays corrected. I wish to use a ViewScript however to better construct
my form.

Any help greatly appreciated. Has anyone managed to use a ViewScript with
Zend_Form without MVC?

Thanks.

David






Matthew Weier O'Phinney-3 wrote:
>
> -- drj201 <[hidden email]> wrote
> (on Friday, 10 April 2009, 06:14 AM -0700):
>> I am having the same problem as first outlined in this thread.
>>
>> The solution Matthew posted does not work. Can anyone help?
>
> Can you provide more information? How are you setting up your form, what
> kind of view object are you using (if any)? What error messages are you
> getting...?
>
>
>> Matthew Weier O'Phinney-3 wrote:
>> >
>> > -- Paul Cook <[hidden email]> wrote
>> > (on Wednesday, 21 May 2008, 02:00 PM -0400):
>> >> I'm trying to use components of the Framework in a project that
>> currently
>> >> is
>> >> not MVC.  Eventually I'd like to refactor to MVC but that is not
>> possible
>> >> right
>> >> now.
>> >>
>> >> I'm using Zend_Form and ran into an error that I can't figure out.
>> I'm
>> >> trying
>> >> to use the ViewScript decorator and I've read through the Decorators
>> with
>> >> Zend
>> >> Form arcticle and scoured the web but now I'm stuck.
>> >>
>> >> The error I get is: Warning: ViewHelper decorator cannot render
>> without a
>> >> registered view object
>> >>
>> >> Some code: formt_test.php (in tests folder and main script)
>> >>
>> >> require_once('Zend/View.php');
>> >> require_once('TestForm.php');
>> >>
>> >> $form = new TestForm();
>> >>
>> >> echo $form->render(new
>> >> Zend_View(array('basePath'=>'/path/to/tests/views')));
>> >
>> > Hmm. I see what the issue is here. Override the setView() method of
>> your
>> > form object with the following:
>> >
>> >     public function setView(Zend_View_Interface $view)
>> >     {
>> >         parent::setView($view);
>> >         foreach ($this as $item) {
>> >             $item->setView($view);
>> >         }
>> >         return $this;
>> >     }
>> >
>> > That _should_ correct the issue for you.
>> >
>> > The problem you're running into is that the form object has the view
>> > object, but it's not injecting into its elements, display groups, etc.
>> > The above should take care of that.
>> >
>> >
>> >> TestForm.php
>> >>
>> >> require_once 'Zend/Form.php';
>> >> require_once 'Zend/Form/Element/Select.php';
>> >>
>> >> class TestForm extends Zend_Form
>> >> {
>> >>         public function init()
>> >>         {
>> >>             $this->setAction("/test_action");
>> >>             $this->setMethod("post");
>> >>
>> >>             $statuses = array(
>> >>                 'Booked'=>'Booked',
>> >>                 'Cancelled'=>'Cancelled',
>> >>                 'Pending'=>'Pending',
>> >>                 );
>> >>
>> >>             $types = array(
>> >>                 'Public'=>'Public',
>> >>                 'Private'=>'Private'
>> >>             );
>> >>
>> >>             $this->addElement(new Zend_Form_Element_Select('status'));
>> >>             $this->addElement(new
>> >> Zend_Form_Element_Select('event_type'));
>> >>
>> >>             $this->getElement('status')->addMultiOptions($statuses);
>> >>             $this->getElement('event_type')->addMultiOptions($types);
>> >>
>> >>             $this->setDecorators(array(
>> >>                 array('ViewScript',
>> >> array('viewScript'=>'test_form.phtml'))
>> >>             ));
>> >>
>> >>         }
>> >> }
>> >>
>> >> test_form.phtml
>> >>
>> >> <form action="<?= $this->escape($this->element->getAction()) ?>"
>> >> method="<?=
>> >> $this->escape($this->element->getMethod()) ?>">
>> >>
>> >>             <fieldset>
>> >>             <legend>Test Form</legend>
>> >>                     <table id='edit_test' style='width: 650px;'>
>> >>                     <tr>
>> >>                         <td class='edit_mem'>Event Status:<br /><?=
>> >> $this->
>> >> element->status ?></td>
>> >>                         <td class='edit_mem'>Event Type:<br /><?=
>> $this->
>> >> element->event_type ?></td>
>> >>                     </tr>
>> >>                     </table>
>> >>                     </fieldset>
>> >>                 </fieldset>
>> >> </form>
>> >>
>> >>
>> >> Any help would be greatly appreciated.  Thanks in advance.
>> >
>> > --
>> > Matthew Weier O'Phinney
>> > Software Architect       | [hidden email]
>> > Zend - The PHP Company   | http://www.zend.com/
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p22987792.html
>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>
>
> --
> Matthew Weier O'Phinney
> Software Architect      | [hidden email]
> Zend Framework          | http://framework.zend.com/
>
>

--
View this message in context: http://www.nabble.com/Zend_Form-without-MVC-problem-tp17375245p23027118.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

pablofmorales
In reply to this post by Paul Cook-4
So easy...


$form = new Forms_YouForm();
$view = new Zend_View();
$form->setView( $view );
echo $form;

http://blog.pablo-morales.com Zend PHP 5.3 Certified
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Form without MVC problem

settermjd
In reply to this post by weierophinney
Hey Matt,

I appreciate it's been a while since you posted this response - but it worked great. Thanks for the guidance.

Matt