garry27 Posted September 18, 2011 Share Posted September 18, 2011 I've been learning ZF from a book and working through the excercises. I've got the master layout working and controller and views which "serve" two different pages, working fine. The next excercise is for a contact form and doesn't work. Please see below code, taken from the authour's source code: APP_DIR>library>Square>Contact.php : <?php class Square_Form_Contact extends Zend_Form { public function init() { // initialize form $this->setAction('/contact/index') ->setMethod('post'); // create text input for name $name = new Zend_Form_Element_Text('name'); $name->setLabel('Name:') ->setOptions(array('size' => '35')) ->setRequired(true) ->addValidator('NotEmpty', true) ->addValidator('Alpha', true) ->addFilter('HTMLEntities') ->addFilter('StringTrim'); // create text input for email address $email = new Zend_Form_Element_Text('email'); $email->setLabel('Email address:'); $email->setOptions(array('size' => '50')) ->setRequired(true) ->addValidator('NotEmpty', true) ->addValidator('EmailAddress', true) ->addFilter('HTMLEntities') ->addFilter('StringToLower') ->addFilter('StringTrim'); // create text input for message body /* $message = new Zend_Form_Element_TextArea('message'); $message->setLabel('Message:') ->setOptions(array('rows' => '8','cols' => '40')) ->setRequired(true) ->addValidator('NotEmpty', true) ->addFilter('HTMLEntities') ->addFilter('StringTrim');*/ // create captcha $captcha = new Zend_Form_Element_Captcha('captcha', array( 'captcha' => array( 'captcha' => 'Image', 'wordLen' => 6, 'timeout' => 300, 'width' => 300, 'height' => 100, 'imgUrl' => '/captcha', 'imgDir' => APPLICATION_PATH . '/../public/captcha', 'font' => APPLICATION_PATH . '/../public/fonts/LiberationSansRegular.ttf', ) )); $captcha->setLabel('Verification code:'); // create submit button $submit = new Zend_Form_Element_Submit('submit'); $submit->setLabel('Send Message') ->setOptions(array('class' => 'submit')); // attach elements to form $this->addElement($name) ->addElement($email) // ->addElement($message) ->addElement($captcha) ->addElement($submit); } } APP_DIR>modules/default/controllers/ContactController.php : <?php class ContactController extends Zend_Controller_Action { public function init() { $this->view->doctype('XHTML1_STRICT'); } public function indexAction() { $form = new Square_Form_Contact(); $this->view->form = $form; if ($this->getRequest()->isPost()) { if ($form->isValid($this->getRequest()->getPost())) { $values = $form->getValues(); $mail = new Zend_Mail(); $mail->setBodyText($values['message']); $mail->setFrom($values['email'], $values['name']); $mail->addTo('info@square.example.com'); $mail->setSubject('Contact form submission'); $mail->send(); $this->_helper->getHelper('FlashMessenger')->addMessage('Thank you. Your message was successfully sent.'); $this->_redirect('/contact/success'); } } } public function successAction() { if ($this->_helper->getHelper('FlashMessenger')->getMessages()) { $this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages(); } else { $this->_redirect('/'); } } } APP_DIR>modules/default/configs/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" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" resources.modules = "" resources.layout.layoutPath = APPLICATION_PATH "/layouts" resources.layout.layout = master autoloaderNamespaces[] = "Square_" # routes # /home route resources.router.routes.home.route = /home resources.router.routes.home.defaults.module = default resources.router.routes.home.defaults.controller = index resources.router.routes.home.defaults.action = index # /content/* route resources.router.routes.static-content.route = /content/:page resources.router.routes.static-content.defaults.module = default resources.router.routes.static-content.defaults.controller = static-content resources.router.routes.static-content.defaults.action = display # /contact route resources.router.routes.contact.route = /contact resources.router.routes.contact.defaults.module = default resources.router.routes.contact.defaults.controller = contact resources.router.routes.contact.defaults.action = index [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 Everything is the same as the author's code except for the code for the text area in the form class which I have to comment out to prevent 0 code being outputted. I've checked to see if the code parses the form and controller scripts from start to end and it does. However, no form code is outputted, just the master layout and a "Contact" header. I'm using Zend Framework 1.11.10 Full. Could this be a conflict with a newer version of ZF? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/ Share on other sites More sharing options...
thehippy Posted September 19, 2011 Share Posted September 19, 2011 Given the class name "Square_Form_Contact," where is the autoloader going to be looking for that class? Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1270553 Share on other sites More sharing options...
garry27 Posted September 19, 2011 Author Share Posted September 19, 2011 Given the class name "Square_Form_Contact," where is the autoloader going to be looking for that class? Sorry, I'm not sure what you mean. I'm not a very good programmer unfortunately. The expected URL for the page is .../square/public/contact so it loads the Contact_Controller and default action from th default module. Then the include path is in the php.ini on the root of the public_html folder: include_path = .:/zend_training/sites/square/library Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1270599 Share on other sites More sharing options...
garry27 Posted September 20, 2011 Author Share Posted September 20, 2011 Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1270940 Share on other sites More sharing options...
thehippy Posted September 20, 2011 Share Posted September 20, 2011 This has nothing to do with URLs and everything to do with Scope and Files. If you wanted to read an INI file with ZF you would create a config object. $config = new Zend_Config_Ini('/path/to/config.ini', 'staging', $options); Where does ZF look for the Zend_Config_Ini class? in what file? Go find it! Now if you look at your config, you've configured ZF to look in "APPLICATION_PATH "/../library" for all of your code. I'll ask again, given the class name "Square_Form_Contact," where is the autoloader going to be looking for that class? Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1270943 Share on other sites More sharing options...
gristoi Posted September 20, 2011 Share Posted September 20, 2011 what thehippy is trying to say is that Zend framework uses namespacing autoloading for files. so a class called MY_NEW_CONTACTS_CONTACTFORM would tell zend to look in the following folder structure: My |--NEW |--CONTACTS |--CONTACTFORM.php look at how your forms class is named. I normally add this into my bootstrap.php for most projects: <?php protected function _initAutoLoad(){ $resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath'=>APPLICATION_PATH,'namespace'=>'')); $resourceLoader->addResourceType('model', 'models/','Model_'); $resourceLoader->addResourceType('form', 'forms/','Form_'); } ?> this tells the framework where to look for specific files. So anything that begins with FORM_ will be in the APPLICATION_ROOT/forms folder. So a test form : <?php class Form_MyForm extends Zend_Form{ } means that the form is in forms APPLICATION_ROOT/MyForm.php Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1270945 Share on other sites More sharing options...
garry27 Posted September 20, 2011 Author Share Posted September 20, 2011 I tried what you guys suggested and still the contact form doesn't load. I'm not even sure if I needed to add the code at bootstrap as the Square_Form_Contact class executes at runtime from start to end. I added exit("test") to the last line and it executed. I added this extra line ot the .ini script. The Square_Form_Contact class is located at /zend_training/sites/square/library/Square/Form/Contact.php: includePaths.library = APPLICATION_PATH "/../library/Square" and this to the bootstrap class: protected function _initAutoLoad() { $resourceLoader = new Zend_Loader_Autoloader_Resource(array( 'basePath' => '/zend_training/sites/square/library/Square', 'namespace' => 'Square')); $resourceLoader->addResourceType('form', 'Form/', 'Form'); } Thank you for your help! It's greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271182 Share on other sites More sharing options...
garry27 Posted September 21, 2011 Author Share Posted September 21, 2011 okays, I've var_dumped the php for the view script of the contact form: <h2>Contact</h2> <?php echo $this->form; var_dump($this->form); ?> ...and it outputs this: object(Square_Form_Contact)#51 (27) { ["_attribs:protected"]=> array(2) { ["action"]=> string(14) "/contact/index" ["method"]=> string(4) "post" } ["_decorators:protected"]=> array(3) { ["Zend_Form_Decorator_FormElements"]=> object(Zend_Form_Decorator_FormElements)#64 (4) { ["_placement:protected"]=> string(6) "APPEND" ["_element:protected"]=> object(Square_Form_Contact)#51 (27) { ["_attribs:protected"]=> array(2) { ["action"]=> string(14) "/contact/index" ["method"]=> string(4) "post" } ["_decorators:protected"]=> array(3) { ["Zend_Form_Decorator_FormElements"]=> object(Zend_Form_Decorator_FormElements)#64 (4) { ["_placement:protected"]=> string(6) "APPEND" ["_element:protected"]=> *RECURSION* ["_options:protected"]=> array(0) { } ["_separator:protected"]=> string(1) " " } ["Zend_Form_Decorator_HtmlTag"]=> object(Zend_Form_Decorator_HtmlTag)#65 (7) { ["_encoding:protected"]=> NULL ["_placement:protected"]=> NULL ["_tag:protected"]=> NULL ["_tagFilter:protected"]=> NULL ["_element:protected"]=> NULL ["_options:protected"]=> array(2) { ["tag"]=> string(2) "dl" ["class"]=> string(9) "zend_form" } ["_separator:protected"]=> string(1) " " } ["Zend_Form_Decorator_Form"]=> object(Zend_Form_Decorator_Form)#66 (5) { ["_helper:protected"]=> string(4) "form" ["_placement:protected"]=> string(6) "APPEND" ["_element:protected"]=> NULL ["_options:protected"]=> array(0) { } ["_separator:protected"]=> string(1) " " } } ["_defaultDisplayGroupClass:protected"]=> string(22) "Zend_Form_DisplayGroup" ["_description:protected"]=> NULL ["_disableLoadDefaultDecorators:protected"]=> bool(false) ["_displayGroupPrefixPaths:protected"]=> array(0) { } ["_displayGroups:protected"]=> array(0) { } ["_elementDecorators:protected"]=> NULL ["_elementPrefixPaths:protected"]=> array(0) { } ["_elements:protected"]=> array(4) { ["name"]=> object(Zend_Form_Element_Text)#50 (30) { ["helper"]=> string( "formText" ["_allowEmpty:protected"]=> bool(true) ["_autoInsertNotEmptyValidator:protected"]=> bool(true) ["_belongsTo:protected"]=> NULL ["_decorators:protected"]=> array(5) { ["Zend_Form_Decorator_ViewHelper"]=> object(Zend_Form_Decorator_ViewHelper)#68 (6) { ["_buttonTypes:protected"]=> array(3) { [0]=> string(24) "Zend_Form_Element_Button" [1]=> string(23) "Zend_Form_Element_Reset" [2]=> string(24) "Zend_Form_Element_Submit" } ["_helper:protected"]=> string( "formText" ["_placement:protected"]=> string(6) "APPEND" ["_element:protected"]=> object(Zend_Form_Element_Text)#50 (30) { ["helper"]=> string( "formText" ["_allowEmpty:protected"]=> bool(true) ["_autoInsertNotEmptyValidator:protected"]=> bool(true) ["_belongsTo:protected"]=> NULL ["_decorators:protected"]=> array(5) { ["Zend_Form_Decorator_ViewHelper"]=> object(Zend_Form_Decorator_ViewHelper)#68 (6) { ["_buttonTypes:protected"]=> array(3) { [0]=> string(24) "Zend_Form_Element_Button" [1]=> string(23) "Zend_Form_Element_Reset" [2]=> string(24) "Zend_Form_Element_Submit" } ["_helper:protected"]=> string( "formText" ["_placement:protected"]=> string(6) " ...... Not sure what this means but might mean something to someone? I was under the impression that it should output html :-\ Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271209 Share on other sites More sharing options...
gristoi Posted September 21, 2011 Share Posted September 21, 2011 Can u post your form class? Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271262 Share on other sites More sharing options...
garry27 Posted September 21, 2011 Author Share Posted September 21, 2011 Can u post your form class? Hi gristoi, yep, here it is: <?php class Square_Form_Contact extends Zend_Form { public function init() { // initialize form $this->setAction('/contact/index') ->setMethod('post'); // create text input for name $name = new Zend_Form_Element_Text('name'); $name->setLabel('Name:') ->setOptions(array('size' => '35')) ->setRequired(true) ->addValidator('NotEmpty', true) ->addValidator('Alpha', true) ->addFilter('HTMLEntities') ->addFilter('StringTrim'); // create text input for email address $email = new Zend_Form_Element_Text('email'); $email->setLabel('Email address:'); $email->setOptions(array('size' => '50')) ->setRequired(true) ->addValidator('NotEmpty', true) ->addValidator('EmailAddress', true) ->addFilter('HTMLEntities') ->addFilter('StringToLower') ->addFilter('StringTrim'); // create text input for message body /* $message = new Zend_Form_Element_TextArea('message'); $message->setLabel('Message:') ->setOptions(array('rows' => '8','cols' => '40')) ->setRequired(true) ->addValidator('NotEmpty', true) ->addFilter('HTMLEntities') ->addFilter('StringTrim');*/ // create captcha $captcha = new Zend_Form_Element_Captcha('captcha', array( 'captcha' => array( 'captcha' => 'Image', 'wordLen' => 6, 'timeout' => 300, 'width' => 300, 'height' => 100, 'imgUrl' => '/captcha', 'imgDir' => APPLICATION_PATH . '/../public/captcha', 'font' => APPLICATION_PATH . '/../public/fonts/LiberationSansRegular.ttf', ) )); $captcha->setLabel('Verification code:'); // create submit button $submit = new Zend_Form_Element_Submit('submit'); $submit->setLabel('Send Message') ->setOptions(array('class' => 'submit')); // attach elements to form $this->addElement($name) ->addElement($email) // ->addElement($message) ->addElement($captcha) ->addElement($submit); } } Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271365 Share on other sites More sharing options...
gristoi Posted September 21, 2011 Share Posted September 21, 2011 replace the init part of your form with this. I know this particular form works as i have tested it myself: <?php public function init(){ $title = $this->createElement('text', 'title'); $title->setLabel('Page Title'); $title->setRequired(true); $title->addErrorMessage('You must add a title'); $title->addValidator('stringLength', true, array(0, 250)) ->addFilter('StripTags'); $urlName = $this->createElement('text', 'url'); $urlName->setLabel('Url Safe name'); $urlName->setRequired(true); $urlName->addErrorMessage('You must add a url name'); $urlName->addValidator('stringLength', true, array(0, 250)) ->addFilter('StripTags'); $menuTitle = $this->createElement('text', 'menu_name'); $menuTitle->setLabel('Menu Name'); $menuTitle->setAllowEmpty(true); $menuTitle->addValidator('stringLength', true, array(0, 250)) ->addValidator('regex', false, array('/^[a-z_]\S/i')) ->addFilter('StripTags') ->addFilter('StringtoLower'); $body = $this->createElement('textarea', 'body'); $body->setLabel('Page Body'); $body->setRequired(true); $this->addElements(array($title,$urlName,$menuTitle,$body)); $this->addElement('submit', 'submit', array('label' => 'Submit')); $this->setMethod('POST'); } and tell me if it displays Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271373 Share on other sites More sharing options...
garry27 Posted September 21, 2011 Author Share Posted September 21, 2011 replace the init part of your form with this. I know this particular form works as i have tested it myself: <?php public function init(){ $title = $this->createElement('text', 'title'); $title->setLabel('Page Title'); $title->setRequired(true); $title->addErrorMessage('You must add a title'); $title->addValidator('stringLength', true, array(0, 250)) ->addFilter('StripTags'); $urlName = $this->createElement('text', 'url'); $urlName->setLabel('Url Safe name'); $urlName->setRequired(true); $urlName->addErrorMessage('You must add a url name'); $urlName->addValidator('stringLength', true, array(0, 250)) ->addFilter('StripTags'); $menuTitle = $this->createElement('text', 'menu_name'); $menuTitle->setLabel('Menu Name'); $menuTitle->setAllowEmpty(true); $menuTitle->addValidator('stringLength', true, array(0, 250)) ->addValidator('regex', false, array('/^[a-z_]\S/i')) ->addFilter('StripTags') ->addFilter('StringtoLower'); $body = $this->createElement('textarea', 'body'); $body->setLabel('Page Body'); $body->setRequired(true); $this->addElements(array($title,$urlName,$menuTitle,$body)); $this->addElement('submit', 'submit', array('label' => 'Submit')); $this->setMethod('POST'); } and tell me if it displays That form does display, gristoi Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271481 Share on other sites More sharing options...
gristoi Posted September 21, 2011 Share Posted September 21, 2011 All I can advise is either build the forms the way I have done or comment out each of ur elements to see which is causing an issue. And I forgot that if u have your framework set to production, u can set turn startup errors and Display errors from 0 to 1 in your application.ini Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271500 Share on other sites More sharing options...
garry27 Posted September 21, 2011 Author Share Posted September 21, 2011 Phew! Sorted now, thanks guys! Once I got the error reporting it pointed out I googled it and turns out the element entity should be spelled "HtmlEntities" not "HTMLEntities". The error reporting also pointed that there was no TextArea.php file in the library I downloaded which explains why I got a blank screen before. Oh well you live and learn Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271602 Share on other sites More sharing options...
garry27 Posted September 21, 2011 Author Share Posted September 21, 2011 Actually, "TextArea" in the class name for Contact form should be "Textarea". My mistake, was looking at wrong directory for the class file in case anyone's following this thread Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1271603 Share on other sites More sharing options...
sydadder Posted July 6, 2012 Share Posted July 6, 2012 I'm still having problem with the code from the book... When I try to instantiate the Default_Form_Contact (I saved it in default)... it doesn't work.... Any help please??? Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1359772 Share on other sites More sharing options...
gristoi Posted July 7, 2012 Share Posted July 7, 2012 You should start a new post not add to an old one. Also ' my form doesn't work' doesn't exactly give us a lot to go on. Can u tell us exactly what is happening Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1359827 Share on other sites More sharing options...
sydadder Posted July 7, 2012 Share Posted July 7, 2012 Oh Thanks for replying. I'll keep that in mind. I got it figured out now. The class wasn't being called properly... (Fatal Error)... Default_Form_Contact... .. I removed 'Default_' as I read somewhere that classes should not be named default.... so it worked.... But just joined this PHP freaks forum... and hope to find great help all the way... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/247396-problem-with-using-zend-form/#findComment-1359848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.