This screencast ran a little long, and unfortunately I’ll have to post the rest next week. We start with creating a DTO (Data Transfer Object) that will house our data as we pass it from the Controller, to the form and then back to the view. Zend_Form is used for validation and rendering.
Zend_Form Introduction, part 1
Description
This screencast ran a little long, and unfortunately I’ll have to post the rest next week. We start with creating a DTO (Data Transfer Object) that will house our data as we pass it from the Controller, to the form and then back to the view. Zend_Form is used for validation and rendering. enjoy!
Tags
dto, Forms, validation, zend_controller

Great tutorials u have here! keep it up.
hi, in PHP/Zend Framework are variables passed and set byref or byval?
eg. when u do a
$this->view->item = $this->session->item
I think we’re passing by value here (making a copy), so watch out! Generally, I prefer this approach unless memory is an issue, so that the “commit” to session is handled in one location.
i am wondering why now we have public functions. we used to have just
function someName() {
…
}
in say Controller classes. is public required? if we dont specify public/private/protected it will be public right?
also i noticed we used $this->name without declaring it say protected $name 1st. this is ok?
PHP is a language that has a lot of old non-OO baggage lying around. If you don’t declare something as “public or private”, it will default to a public function (which I think is a bad move since it inhibits encapsulation). I might have been a little sloppy in the tutorial as I was trying to run through the material.
Same thing with $this->name, you don’t need to declare your variables in PHP before you use them, even if they’re class variables. In production, this is obviously a bad practice! Thanks for mentioning it!
Fatal error: Class ‘App_ItemDto’ not found.
Maybe it’s need to set Loader namespace?
Hey,
I am trying to do what you did,I copied the code and how it works for you?You didn’t require the itemDto.php file..
Fatal error: Class ‘App_itemDto’ not found in D:\Applications\wamp\www\zendFrameworkLearning\application\controllers\ItemController.php on line 17
This the error the i am getting
You’ll have to register the App_ namespace in the Bootstrap.php file for it to work
protected function _initAutoloaders()
{
$this->getApplication()->setAutoloaderNamespaces(array(‘App_’));
return $this;
and in application.ini
includePaths.library = “../library/”
If you use Doctrine and get the strange error:
Fatal error: spl_autoload() [function.spl-autoload]: Class Doctrine_Event could not be loaded in …..
you’ll have to add Zend_Session::writeClose(true);
at the end of your main index.php file
ref:
http://stackoverflow.com/questions/1364750/opcode-apc-xcache-zend-doctri
I modified my Bootstrap file and i no longer have the Fatal error.
But i have no data being displayed. If i comment if($item instanceof App_ItemDto) then i get the notice.
Notice: Trying to get property of non-object in C:\wamp\www\quickstart\application\views\scripts\item\index.phtml on line 14
Hello! At first, thanks a lot for your work!!! Great tuts on the web.
At second – I’ve got a question:
why we are calling $this->addElements in the ItemEditor, and at the same time writing $this->name (or other: description, id).
By writing $this->name we’ve already added this element to the form, or not?
hi Jon, I have a question, i have created a directory in application folder “forms” and in that file I have created a file “Elements.php”, which is a class file.
code goes like this:
<?php
class Forms_Elements extends Zend_Form
{
//all the code
}
————-
now when I call this file, in my action scripts,
code goes like this:
function xx(){
$form = new Zend_Form();
$loudbiteElements = new Forms_Elements();
—- it throws an error Forms_Elements not found in controller.
when I modify it to
function xx(){
$form = new Zend_Form();
require"..application/forms/Elements.php"
$loudbiteElements = new Forms_Elements();
It works normally.
I have initialized zend autoloader in public/index.php
What should I do to make the class load directly, without using [require], in awesome Zend way??
Fatal error: Class ‘App_ItemDto’ not found in C:\wamp\www\endofline\application\controllers\ItemController.php on line 21
why i get this error? i do everything like in video!
for those of you getting the error: Fatal error: Class ‘App_ItemDto’ not found in …
you are getting this error because you have not registered the ‘App’ namespace so zf does not know to look in the library/App folder. the easiest way to solve this is to modify the application.ini file so that it is available for all your php files.
add this line in the production part:
autoloaderNamespaces[] = “App_”
Hope that helps!
form ?> , not work tag
” form ?> ” , not work tag “”
Firstly thank you very much for your tuts. These are excellent resources for a beginner like me. I am wondering if there are any download files for the tuts in each videos.
Cheers.
Ok, I found the sorce codes in the next zendcast. Thanks.
In the App_forms_ItemEditor I noticed that you didn’t add $this->submit to the array parameter for the addElements method function and yet the form still rendered the submit button.
In fact, on my version I didn’t even need the addElements call at all and yet doing $this->name = new Zend_Form_Element_Text(‘name’) call instantly made it appear within the view.
Does anyway understand why this is and can explain?
How do you register a namespace into the bootstrap?
I followed the suggestion here for editing the application.ini but still getting an error “Page not found”. I am on version 1.11.3
Jon Thanks so much, I can can’t begin to explain how much this helped!
I very much doubt I will be writing HTML Forms ever again!
@MG7282
The Following code is used within the ‘_initAutoload() method’ if your bootstrap.php is a class, older Zend versions differ:
Zend_Loader_Autoloader::getInstance()->registerNamespace(‘App_’);
You can of course replace ‘App_’ with what you like but make sure you add the ‘_’ and follow the Zend naming convention to ensure your classes are found.
@Andy
In this case $this->varname is implying that varname is a property of the class. Normally you’d define the properties outside of the constructor. Like protected varname = null; or something like that.
But to be honest, since you are instantiating the properties inside the constructor you could also just not define them as properties. Remove the $this. Then pass them all to the addElements stack.
The reason you see the submit button is because by default PHP makes properties public. Define submit as protected / private and you will no longer see it.
Great tutorial –
Thanks.
One thing: If we all use a name convention, then, we are not using a name convention.
You have no idea how happy I am to find this website!
I love your screencasts. Thank you!! @_@