Not getting the form data loaded into [log:Zend_View_Abstract:private] - zend-framework

I assigned
$this->view->form = $form (form instance) and now trying to display the content in layout.phtml using
echo $this->form;
but not displaying anything. I tried to check contents using
print_r($this);
and I see no data loaded into [log:Zend_View_Abstract:private].
Could anyone please help me on how to get data loaded in phtml file.
AuthForm.php
class forms_AuthForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('login');
$username = new Zend_Form_Element_Text('username','username');
$username->setLabel('Username:')
->setRequired(true)
->setOptions(array('class'=>'longfield'));
$password = new Zend_Form_Element_Text('password','password');
$password->setLabel('Password: *')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit','submit');
$submit->setLabel('Login');
$this->addElements(array($username, $password,$submit));
}
}
And AuthController.php
$form = new forms_AuthForm();
$this->view->form = $form;
Regrds
kiran

Iam not sure, but i think you have to assign the form to the layout not the view.
$layout = Zend_Layout::getMvcInstance();
$form = new forms_AuthForm();
$layout->form = $form;
and in your layout.phtml
echo $this->layout()->form;

Related

Zend Framework addaction with success or failure message

public function addAction()
{
$form = new ApplicationForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$name = $form->getvalue('name');
$class = $form->getvalue('class');
$file = new Application_Model_DbTable_Records();
$file->addRecord($name,$class);
$this->_helper->redirector('index');
}
}
}
Above addAction controller part, here when i am clicking AddAction my form is waiting for user inputs when i click submit my inputs recorded in database.
Now my question is i want add some message after the submit form data whether it success or failure.
Could you please help me on this ?
Many Thanks,
viswa
The docs for the action-helper describe an example. But standard usage goes something like this:
After you add the record, before you redirect, set the desired message in your controller:
public function addAction()
{
$form = new ApplicationForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$name = $form->getValue('name');
$class = $form->getValue('class');
$file = new Application_Model_DbTable_Records();
$file->addRecord($name,$class);
// Add the message here
$this->_helper->getHelper('FlashMessenger')->addMessage('Record added');
$this->_helper->redirector('index');
}
}
}
Then in your indexAction - the controller to which you are redirecting after successful record addition - get the messages and add them to your view:
public function indexAction()
{
// All your existing processing
// Blah, blah..
// Get the messages from the FlashMessenger
$messenger = $this->_helper->getHelper('FlashMessenger');
$messages = $messenger->hasMessages() ? $messenger->getMessages() : [];
// Add the messages into the view
$this->view->messages = $messages;
}
Finally, somewhere in the index view-script where you want the messages to appear, check for the messages and render, something like:
<?php if ($this->messages): ?>
<div id="refresh-messages">
<ul>
<?php foreach ($this->messages as $message): ?>
<li><?= $message ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
The wrapping div is just to assist with styling by providing a DOM element id to which you can target your CSS.
Disclaimer: Not tested directly, just coding from memory.

Zend_Form_Element requires each element to have a name

I'm trying to create a form in ZF 1.Here's my form class
class Application_Form_Album extends Zend_Form
{
public function init()
{
$this->setName('album');
#artist
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')->setRequired(true)->addValidator('NotEmpty');
#title
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')->setRequired(true)->addValidator('NotEmpty');
#submit
$submit = new Zend_Form_Element_Submit();
$submit->setAttribute('id','submitbutton');
$this->addElements(array($artist,$title,$submit));
}
}
and my controller action
public function addAction()
{
$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;
}
and my add.phtml
<?php echo $this->form;?>
But I'm getting this error.
Message: Zend_Form_Element requires each element to have a name
Not sure what I missed.Could anyone help me?
You should give a name for each form element. Missing name of submit. Zend Form generate id and name html tags from given name
For example:
$submit = new Zend_Form_Element_Submit('submitbutton');
And remove
$submit->setAttribute('id','submitbutton');
line.

Zend Form Registration with

I am new in Zend.
I have tried to create registration form in Zend.
I have get an array of form but it return false.
It returns me every time bye .
I don't know why???
It's Simple:
First, create your registration form under application/forms or use zend tool
zf enable form
zf create form registration
this will create a file under application/forms entitled Registration.php
class Application_Form_Registration extends Zend_Form
{
public function init()
{
$firstname = $this->createElement('text','firstname');
$firstname->setLabel('First Name:')
->setRequired(false);
$lastname = $this->createElement('text','lastname');
$lastname->setLabel('Last Name:')
->setRequired(false);
$email = $this->createElement('text','email');
$email->setLabel('Email: *')
->setRequired(false);
$username = $this->createElement('text','username');
$username->setLabel('Username: *')
->setRequired(true);
$password = $this->createElement('password','password');
$password->setLabel('Password: *')
->setRequired(true);
$confirmPassword = $this->createElement('password','confirmPassword');
$confirmPassword->setLabel('Confirm Password: *')
->setRequired(true);
$register = $this->createElement('submit','register');
$register->setLabel('Sign up')
->setIgnore(true);
$this->addElements(array(
$firstname,
$lastname,
$email,
$username,
$password,
$confirmPassword,
$register
));
}
}
This is a simple form with limited validation (only validation for fields that are required!)
Then you have to render the form in a view using the corresponding action:
as example in your UserController add action called
public function registerAction() {
//send the form to the view (register)
$userForm = new Application_Form_Registration();
$this->view->form = $userForm;
//check if the user entered data and submitted it
if ($this->getRequest()->isPost()) {
//check the form validation if not valid error message will appear under every required field
if ($userForm->isValid($this->getRequest()->getParams())) {
//send data to the model to store it
$userModel = new Application_Model_User();
$userId = $userModel->addUser($userForm->getValues());
}
}
}
The last two, is to render the form in the view called register
using
<?= $this->form ?>
and add method in your model called addUser() to handle the insertion of data into the database

Zend Framework: How to pass data from bootstrap to layout?

I have some configuration values set in application.ini and i want to pass those values to the layout on application load. How can i do this from bootstrap ? For trial i tried doing this
In my Bootstrap initialization function:
$this->bootstrap('view');
$view = $this->getResource('view');
$view->layout()->whatever = "Some Value";
In layout:
<?php echo $this->layout()->whatever; ?>
But m not able to get the value to display in the layout.
The following should work:
$this->bootstrap('view');
$view = $this->getResource('view');
$view->whatever = 'Some value';
Then, in layout:
<?php echo $this->whatever ?>
You have to grab the Layout and from there the view object:
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->text = 'Welcome';

File not found after calling receive on form file element and file transfer adapter

As the title says, I tried calling the receive() function either of Form Element and the Adapter Object(not one after another of course). I printed the returned value - was 1 in both cases - which means receive() returned true.
The file was not found on the server though. I tried setting encrypt type of zend form to multipart/form-data - didn't help.
I'm totally clueless so any info is welcomed.
Calling receive() on transfer adapter: file location and upload name are constants.
$this->uploadName = $uploadName;
$this->upload = new Zend_File_Transfer_Adapter_Http();
$this->upload->setDestination($this->fileLocation);
...
$val = $this->upload->receive();
$quoteName = $this->upload->getFileName($this->uploadName);
$size = $this->upload->getFileSize($this->uploadName);
calling receive on form element:
//form creation - my form extends zend form
$staticForm = Srm_Form::getForm(my form,null,null,
my config);
$staticForm->setEnctype('multipart/form-data');
$staticForm->getElement(my file element name)->setDestination(my dest);
//calling receive
$form = Srm_Form::getForm(my form,null,null,my config);
$form->setEnctype('multipart/form-data');
if(!$form->isValid($_POST)){
print_r($form->getMessages());
}
// echo $form->getElement(my file element)->getValue();
$val = $form->getElement(my file element)->receive();
echo "bbbbbb".$val;
I should add that this code works when it is called after the file element is added to the form manually and not through use of a config file.
Okay, the problem was found -
The destination was not set for the file element (it was set manually for the transfer adapter in other place)when handling the submitted form.
I define a simple form with the Zend_Form_Element_File element
<?php
class Form_UploadForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setMethod('post');
$this->setAttrib('enctype', 'multipart/form-data');
$decors = array(
array('ViewHelper'),
array('HtmlTag'),//array('tag'=>'table')),
array('Label', array('separator' => ' ')), // those unpredictable newlines
array('Errors', array('separator' => ' ')), // in the render output
);
$file = new Zend_Form_Element_File('file');
$file->setDestination('/a/b/c/upload');
$file->setLabel('Document File Path')
->setRequired(true)
->addValidator('NotEmpty');
$this->addElement($file);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Upload File');
$this->addElement($submit);
}
}
?>
My action method in the Controller is
function uploadAction()
{
$this->view->pageTitle = "Zend_Form File Upload Example";
$this->view->bodyCopy = "<p>Please fill out this form.</p>";
$form = new Form_UploadForm();
if ($this->_request->isPost())
{
$formData = $this->_request->getPost();
if ($form->isValid($formData))
{
try
{
$form->file->receive();
}
catch (Zend_File_Transfer_Exception $e)
{
throw new Exception('unable to recieve : '.$e->getMessage());
}
$uploadedData = $form->getValues();
//Zend_Debug::dump($form->file->getFileName(), 'tmp_file');
$this->processFile($form->file->getFileName());
}
else
{
$form->populate($formData);
}
}
$this->view->form = $form;
}
Note - i don't call the Zend_File_Transfer_Adapter_Http directly
The final piece is the view
<?php echo $this->form; ?>