views:

116

answers:

2

Hello!

It's true I've seen a lot of examples on stackoverflow and many from Google search but apparently nobody showed the big picture of how things falls under one another, even from the manual itself.

I've just picked Zend Framework(1.10.8) and when creating forms I've finally found that the ViewScript for now is much easier for me to configure but wasn't the case.

I have amodule booking, with UserController and createAction in it.Under /application/modules/booking/views/scripts/user I have create.phtml and custormerForm.phtml.

From my understanding, at the end of everything my form with it's rendering will be shown in my create.phtml as in my form will use the customerForm.phtml for it's visual and injected in the create view.

So I went ahead and created a simple form

function init(){
  $this->setMethod("post");

  $name = New Zend_Form_Element_Text("name");
  $name->setLabel("Name: ")
  ->setOptions(array("size"=>"35"))
  ->setRequired(true)
  ->addValidator("NotEmpty", true);

  $surname = New Zend_Form_Element_Text("surname");
  $surname->setLabel("Surname: ")
  ->setOptions(array("size"=>"35"))
  ->setRequired(true)
  ->addValidator("Alpha", true);

  $this->addElement($name)
  ->addElement($surname)
  ->addElement($submit);
}

Now here is the createAction in UserController

public function createAction(){
  $this->view->show = "Please Enter your Details";

  $form = new Hotel_Form_Entity();
  $form->setAction("/booking/user/create");
                //and here set the for to be displayed at described in customerForm view
  $form->setDecorators(array(array('ViewScript',array('viewScript'=>'customerForm.phtml'))));
                //so here i set the form to form variable accessible in create view
  $this->view->form = $form;
  if($this->getRequest()->isPost()){
    if($form->isValid($this->getRequest()->getPost())){
      $values = $form->getValues();
      $this->_helper->flashMessenger("Thank you.Form processed");

      $this->_forward("success","user","booking",$values);
    }
  }
}

Now these are the create.phtml and customerForm.phtml

<!-- create.phtml -->
<h4><?php echo $this->show; ?></h4><br/><!--  -->
<p><?php echo $this->form; ?></p><br/> 

 <!-- customerForm.phtml --> 
<div style="padding: 10 0 0 15; border: solid 1.5px #999">
 <form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
  <table>
     <tr>
       <td><?php echo $this->element->name; ?></td>
       <td></td>
     </tr>
     <tr>
       <td></td>
       <td><?php echo $this->element->surname; ?></td>
     </tr>
     <tr>
       <td colspan="2"><?php echo $this->element->submit; ?> </td>
     </tr>
   </table>
  </form> 
 </div>

so when I hit my page as in http://localhost/project/booking/user/create it just displays the layout with the content of the create view with no form. Nothing in page source, no errors.

Did I get the wrong idea on how to use this or I'm just doing something wrong in the code?And since i'm using Zend framework 1.10.8 there seems not to be any tutorial covering the whole thing on ViewScript decorator.

Can anyone please give me a hand and share his valuable experience here? Thank you very much for reading this. Maybe I'll make this a tutorial who knows :D

A: 

Hi i'd like to rewrite your action and lets forget about customerForm.phtml for a while

public function createAction(){
  $this->view->show = "Please Enter your Details";

  $form = new Hotel_Form_Entity();
  $form->setAction("/booking/user/create");
  $form->setElementDecorators(array('viewHelper', 'formElements')));
  $this->view->form = $form;
  if($this->getRequest()->isPost()){
    if($form->isValid($this->getRequest()->getPost())){
      $values = $form->getValues();
      $this->_helper->flashMessenger("Thank you.Form processed");

      $this->_forward("success","user","booking",$values);
    }}}

what formElements decorators do

Zend_Form_Decorator_FormElements Forms, display groups, and sub forms are collections of elements. In order to render these elements, they utilize the FormElements decorator, which iterates through all items, calling render() on each and joining them with the registered separator. It can either append or prepend content passed to it.

http://framework.zend.com/manual/en/zend.form.standardDecorators.html

I hope i did it right , FYI i haven't test the action yet :)

tawfekov
thanks for the effort to explain how i could use the standard decorators.i will look at it but really what i need now is to understand if i'm on the right path with viewscript which looks much much simpler to me.Can you please look at and tell me what i'm doing wrong.i just need something to get going. thank you ;)
black sensei
A: 

OK i've figured it out.Everything in coding side was correct.
i just had to put the customerForm.phtml into /modules/booking/views/scripts/ and that's it.thanks for those who tried to help me.I can now learn how to customize the standard decorators as Tawfekov tried to explain.

black sensei