Zend Form instantiation failing silently in white screen of death - zend-framework

Zend newbie trying to configure and use Zend_Form.
Just for the record I'm on Zend Framework Version: 1.11.1 on Win XP running Apache 2.something. I'm working on a site which for the most part works just fine. (Somebody else started it. I have to extend it).
I am having trouble in the area of forms and am trying to introduce Zend_Form in the hope that this will somehow simplify matters. But trying to use Zend_Form is presenting problems of it's own.
When I try to instantiate the first test form, I'm getting the white screen of death -- without even an error message.
Data as follows:
Dir Structure:
MYAPPNAME
....controllers
....forms
....models
....services
....views
Bootstrap.php contains:
protected function _initAutoLoading()
{
$loader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'MYAPPNAME',
'basePath' => APPLICATION_PATH . '/modules/MYAPPNAME',
));
$loader->addResourceTypes(array(
'model' => array(
'path' => 'models',
'namespace' => 'Model'),
'form' => array(
'path' => 'forms',
'namespace' => 'Form'),
'service' => array(
'path' => 'services',
'namespace' => 'Service')));
}
This works fine for models with names like:
class MYAPPNAME_Model_DataRecordName extends Doctrine_Record
{
etc...
But it seems to be failing miserably for forms ... although mind you, this is my first pass at using Zend_Form.
My form is defined in file MYAPPNAME/forms/Formtest.php:
<?php
class MYAPPNAME_Form_Formtest extends Zend_Form
{
public function init($action){
$this->setAction($action)
->setMethod('post')
->setAttrib('id', 'formtestForm');
$email = $this->addElement( 'text', 'email',
array('label', => 'EMail'));
)
$submit = $this->addElement('submit', 'Submit and Be Free!');
}// End init
} // End class def
The form is being displayed in a view defined as:
<div class=""testForm">
<p style="margin-top:20px; margin-bottom:10px"">Explanatory Text</p>
<h2>This is a Form Test</h2>
<?php echo $this->formResponse; ?>
<?php echo $this->form; ?>
<hr>
<p>FORM ABOVE THIS BAR</p>
</div>
The view works just fine.
It is being managed by an action (in a working controller) defined as:
public function formtestAction(){
echo "formtestAction: ENTERED";
$form = new MYAPPNAME_Form_Formtest('ThisController/formtest2');
//$form = "<p>GARBAGE DATA</p>";
if(!empty($form)){$this->view->form = $form;}
else{
$form = "<p>THE FORM VAR IS EMPTY</p>";
$this->view->form = $form;
$formResponse = "<p>INSTANTIATION FAILED</p>";
$this->view->formResponse = $formResponse;
}
}
public function formtest2Action(){
echo "formtest2Action: ENTERED";
}
If I comment out both the form instantiation and the garbage data lines, I get valid output in the view. If I set $form to "GARBAGE DATA" I also get valid predictable output.
However when I try to instantiate the form object I get the white screen of death containing only "formtestAction: ENTERED" (from the echo statement at the top.)
I am going slowly mad.
I can't figure out if this is an autoloader problem, a routing problem, an object instantiation problem, or what.
I'd be very much obliged for any advice.
Thanks for reading.

With Zends, I've run into that several times, and it usually is something annoying as a superflous comma. In
... 'basePath' => APPLICATION_PATH . '/modules/MYAPPNAME',));
it looks just like on of those. Only a quick look, but you might check it anyway.
HTH,
Marcus

mtoepper: Very close. Good catch!
It was indeed an extra comma, only it was in the Form class definition -- preventing successful object instantiation.
These silent failures are VERY annoying.

Related

Zend 2 - Form on every page

I am new to Zend Framework 2
I want to add a form to every page (a login box for example) that functions as it does when in its own module ie. so it validates and there is no need to redirect back from the module after the action.
I have looked at various things such as view helpers and action helpers, I have not posted any code as it may just add confusion
I am looking for a guide on how to achieve this as I currently am confused as to how this would be best implemented
This is certainly a use case for a view helper. This provides a piece of logic reusable in multiple views across different modules.
Take for example the login form,. you might want to return a Zend\Form\Form instance when you call the helper. So first, create the helper:
namespace MyLogin\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\Form\Form;
class LoginForm extends AbstractHelper
{
public function __invoke()
{
$form = new Form;
$url = $this->getView()->url('user/login');
$form->setAttribute('action', $url);
$form->add([
'name' => 'username',
]);
$form->add([
'type' => 'password',
'name' => 'password',
]);
return $form;
}
}
Then you register this view helper under the name "loginForm" in your config:
'view_helpers' => [
'invokables' => [
'loginForm' => 'MyLogin\View\Helper\LoginForm',
],
],
Now you can use the helper in your views:
<?php $form = $this->loginForm() ?>
<?= $this->form()->openTag($form)?>
<?= $this->formRow($form->get('username'))?>
<?= $this->formRow($form->get('password'))?>
<button type="submit" value="Login">
<?= $this->form()->closeTag()?>
Of course you can apply any login in your form, whatever you need to be reusable:
Return a form instance so your view can render the form
Return a rendered view already in the helper so your view does not need to render
Set all kind of options to the form
Etc

Zend\Form: Call to a member function insert() on a non-object in Zend/Form/Fieldset.php

I am learning how to use Zend Framework 2 (2.1.4) forms and running into this error.
Call to a member function insert() on a non-object in ... /Zend/Form/Fieldset.php on line 178
I don't want use the form to automatically connect to a database, in fact I only want to use the form to help validate and will pull from and populate it with an array of values. How do I turn off the database connectivity in the form objects?
I am used to dealing with the ZF1 forms so this new form system is confusing. Once I thought about it though, the way we can use the form elements in our view scripts for formatting is going to be nice. Those old decorators were a pain. Anyway, for me, it would be nice to use the forms without dealing with bound database objects. Is this possible? It just seems so overly complicated to need a model class using InputFilterAwareInterface classes in addition to a simple form. One step at a time though, I can't even get the form to display.
I appreciate any help.
Below are my controller, form, and view scripts:
Form class:
namespace FBWeb\Form;
use Zend\Form\Form;
use Zend\Form\Element;
class ClientForm extends Form
{
public function __construct()
{
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'client',
'type' => 'Zend\Form\Element\Text',
'options' => array(
'label' => 'Client Name',
),
'attributes' => array(
'type' => 'text',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Add'
),
));
}
}
Controller class:
namespace FBWeb\Controller;
use Zend\Debug\Debug;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Http\Request;
use FBWeb\Form\ClientForm;
class ClientController extends AbstractActionController
{
public function indexAction()
{
$clientform = new ClientForm();
return array('form' => $clientform);
}
}
index.phtml view script:
<div id="clientformtable">
<?php
$form = $this->form;
$form->setAttribute('action','/app/client/add');
$form->prepare();
echo $this->form()->openTag($form);
$client = $form->get('client');
echo $this->formRow($client);
echo $this->form()->closeTag();
?>
</div>
This, and similar error messages, happen due to the fact that the form isn't properly set up. As you can see within the code above the __construct() function doesn't call the parents constructor. Therefore the internal "bootstrapping" doesn't happen and the error occurs.
You have to make sure to always call the parents constructor when dealing with Zend\Form\Form and/or Zend\Form\Fieldset.
parent::__construct('client-form');

How mvc works in Zend framework

Thanks for previous replies..
I am trying to print Hello_world using zend framework. I wrote php file in model folder and return string value as a "Hello_world". In controller i access the value of PHP like this
$value = new TextReturner();
$this->view->setValue = $value->hello_world(); . i dont know how to access the value from controller to the view php file. I am new to zend framework. I already go through the outline structure of zend framework, i dont know how to access through codings. If anyone have idea of how to print hello_world through MVC pls guide me.
You are trying to use class $value = new TextReturner(); but your controller doesn't see that class.
Set this in your Bootstrap file that will help:
protected function _initAutoLoad() {
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
),
),
)
);
return $resourceLoader;
}
This will be autoload all of your model class.
in view you can access your variable like this:
<?php echo $this->setValue;?>

Write hyperlink inside the Zend Form?

I am using Zend-Framework in my project. I made a login form using the Zend Form that contains the User Id and Passwords fields with a submit button. Everything is working fine in the login form.
How do I add two hyperlinks inside the login form that is one for the Sign-Up and other for the Forget Password?
I've faced the same problem before, and solved it by creating a custom Zend_Form_Element_Html, as follows:
class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml {
/**
* Default form view helper to use for rendering
* #var string
*/
public $helper = 'formNote';
public function isValid($value, $context = null) {
return true;
}
}
So, in your form you just have to do the following:
$tag = new Zend_Form_Element_Html('forgetPassword');
$tag->setValue('Forgotten your password?');
$this->addElement($tag);
Hope this helps!
In your viewscript file where you print the form, e.g. login.phtml
echo $this->form;
you can specify any other html markup, e.g. links
echo "<p><a href='".$this->url ( array ('controller' => 'authentication',
'action' => 'lostPW' ) )."'>
Lost pw </a></p>";
So you actually do not write it in the form itself but in the view script where you echo the form.
Try this:
$passwordElement->setDescription('Forgot password?');
$passwordElement->getDecorator('Description')->setOption('escape', false);
Description decorator will add this text beside your field.
You can use Zend_Form_Decorator_ViewScript
Or, create a custom Zend_Form_Element to render HTML elements or ViewScript.
For only decorators use directly in the form try:
$this->addElement(new Zend_Form_Element_Note(array(
'name' => 'forgotten',
'value' => __('Forgot your password?'),
'decorators' => array(
array('ViewHelper'),
array('HtmlTag', array(
'tag' => 'a',
'href' => $this->getView()->url(array(
'remind'
))
)),
)
)), 'forgotten');

Render view from Zend_form

I try it via my Zend_form:
$output .= $this->_view->render('admin/form.phtml'
, array('id' => $this->getName()
, 'action' => $this->getAction()
, 'method' => $this->getMethod()
, 'enctype' => $this->getEnctype()
, 'data' => array('code' => $code
, 'name' => $name
, 'description' => $description)));
but when i <?php echo $this->enctype; ?> in admin/form.phtml i got nothing.
admin/form.phtml is rendered correctly
Choosing render just displays the output, Zend does not pass your data to the view. But Using view partials, you could achieve that.
From the documentation,
The Partial view helper is used to render a specified template within
its own variable scope. The primary use is for reusable template
fragments with which you do not need to worry about variable name
clashes. Additionally, they allow you to specify partial view scripts
from specific modules.