I followed a tutorial just to get deeper in the form topic.
I built a Form class: application/forms/BugReportForm.php I added to my bootstrap.php (before it was unused)
protected function _InitAutoload()
{
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader= new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_',
)
),
));
//return it ao sthat istcan stored in the bootstrap
return $autoLoader;
}
I built also my forms/BugReportForm.php
My BugController.php looks like this:
<?php
class BugController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function submitAction()
{
$frmBugReport = new Form_BugReport();
$frmBugReport = setAction('/bug/submit');
$frmBugReport = setMethod('post');
$this->view->form = $frmBugReport;
}
my application.ini
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
phpSettings.date.timezone = "Europe/Berlin"
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =
resources.db.params.dbname = TQM
resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view.doctype = "XHTML1_STRICT"
I get this Error : Class 'Form_BugReportForm' not found in ....
What ist wrong, I think the bootstrap doesn´t work or the namespace ist not known?
Folderstructure
and at the moment I don´t use virtual hosts.
errors after changing my class call in $frmBugReport = new Application_Form_BugReportForm();
hier for example another very small form
<?php
// Formular wird angezeigt bei neue Ebene 1 und editieren
class Application_Form_Hierarchie extends Zend_Form
{
public function init()
{
$this->setName('Ebene1');
$this->setAttrib('enctype', 'multipart/form-data'); //Formular wird für Dateiuploads verwendet
$nr = new Zend_Form_Element_Hidden('nr');
$nr->addFilter('Int');
$ebene1 = new Zend_Form_Element_Text('ebene1');
$ebene1->setLabel('Ebene1')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('nr', 'submitbutton');
$this->addElements(array($nr, $ebene1, $submit)); //, $imagepreview
}
}
?>
and hier the action (add) from the Controller:
$form = new Application_Form_Hierarchie();
I tried the other posibility (it is from a book tutorial) to learn how to use own formclasses. I think I should give up with the turorial and go back to the simple way.
The problem probably comes from the wrong class name specified in your application. Be sure to follow these rules while working with zend from and models, and you'll be fine :
Bootstrap.php file:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initAutoload(){
$resourceLoader= new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
$resourceLoader->addResourceType('model', 'models/', 'Model');
$resourceLoader->addResourceType('form', 'forms/', 'Form');
return $resourceLoader;
}
}
Zend_Form directory and file names :
Zend_Form
is Form and it will pointing to folder named as forms.Form_
yourFormName.php
. This will be your Form class file.yourFormName.php
create a class as Form_yourFormName.php
yourFormName.php :
class Form_yourFormName extends Zend_Form
{
public function init()
{
//your form codes goes here like creating form elements
}
}
Then in your controller get a instance of your defined form class :
class YourController extends Zend_Controller_Action
{
public function indexAction()
{
$form = new Form_yourFormName();
$this->view->form = $form;
}
}
EDIT : For your case it should be :
$frmBugReport = new Form_BugReportForm();
Not :
$frmBugReport = new Form_BugReport();