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