For a form I'm trying to add a selectbox which contains a list of items from my database.
My form is situated in /application/forms/News/Edit.php
In my controller I want to fetch this list which I want to use in my form.
How can I add that list from my controller to my form?
This is how my code in Edit.php looks like:
$this->addElement(
'select',
'view_status',
array(
'label' => 'View status',
'multioptions' => array(
//THIS SHOULD BE FILLED WITH DYNAMIC CONTENT FROM MY CONTROLLER
)
)
);
You can pass the options of your select in the first param of the form constructor. When initialising the form, Zend_Form look for a set method postfixed by the option name:
class App_Form_News_Edit extends Zend_Form
{
public function setViewStatusOptions($options)
{
$this->view_status->setMultioptions($options);
}
}
$form = new App_Form_News_Edit(array('viewStatusOptions' => array(..)));
Just use the constructor or _init function of your forms class to set any custom values you would have.
It would get you something looking like (in your controller):
$myForm = new form_News_Edit($myArrayOfValues);
Then in your forms class :
public function __construct($myArrayOfValue){
....
$this->addElement(
'select',
'view_status',
array(
'label' => 'View status',
'multioptions' => $myArrayOfValue
)
);
}
yvoyer's Solution is good too but takes out some of the business logic for the form out of it.
In your controller's action, you could initialize the options you wants depending on the action. You should set the optoions before sending the form to the view.
$Form = new form_News_Edit();
$Form->getElement('view_status')
->setMultioptions($arrayOptions);
$this->view->assign('Form', $Form);
Related
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');
I create custom element like here: ZF2Docs: Advanced use of Forms
1.Create CustomElement class in Application/Form/Element/CustomElement.php
2.Add to my Module.php function
public function getFormElementConfig()
{
return array(
'invokables' => array(
'custom' => 'Application\Form\Element\CustomElement',
),
);
}
If I use FQCN it works fine:
$form->add(array(
'type' => 'Application\Form\Element\CustomElement',
'name' => 'myCustomElement'
));
But if I use short name:
$form->add(array(
'type' => 'Custom',
'name' => 'myCustomElement'
));
throws Exception:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create
an instance for Custom
Problem
The error is probably due to how you are instantiating the $form object. If you just use the new Zend\Form\Form expression or something similar the form will not be set up with the correct service locator.
$form = new \Zend\Form\Form;
$form->add(array(
'type' => 'custom',
'name' => 'foobar',
));
Solution
The trick here is to use the FormElementManager service locator to instantiate the form.
// inside a controller action
$form = $this->getServiceLocator()->get('FormElementManager')->get('Form');
$form->add(array(
'type' => 'custom',
'name' => 'foobar',
));
Better yet, define a form() method in your controller as a shortcut to do this for you:
class MyController extends AbstractActionController
{
public function form($name, $options = array())
{
$forms = $this->getServiceLocator()->get('FormElementManager');
return $forms->get($name, $options);
}
public function createAction()
{
$form = $this->form('SomeForm');
// ...
}
}
Explanation
Each form object is attached to a form factory which is in turn attached to a service locator. This service locator is in charge of fetching all the classes used to instantiate new form/element/fieldset objects.
If you instantiate a new form object (all by itself), a blank service locator is instantiated and used to fetch later classes within that form. But each subsequent object is then attached to that same service locator.
The problem here is that getFormElementConfig configures a very specific instance of this service locator. This is the FormElementManager service locator. Once it's configured, all forms pulled from this service locator will then be attached to this service locator and will be used to fetch other elements/fieldsets etc.
Hope this solves your issue.
I need a straight forward working example how I can include a collection element in Zend Form, I have seen some examples from Zend Framework 2 site and from previous posts in StackOverflow where most of them pointed to this link. But right now I am not using Fieldsets and staying with Forms, so in case if someone can direct me in the right way, how I can include a simple collection element when the user gets a page where the user can choose multiple choices from the shown collection form. Much better would be populating the collection form from database.
I have searched in the internet for quite a sometime now and thought I would post here, so that Zend profis can give their suggestions.
Just For Information:
Normally one can include a static dropdownbox in Zend Form in this fashion
$this->add(
array(
'name' => "countr",
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => "Countries",
'options' => array(
'country1' => 'Brazil',
'country2' => 'USA',
'country3' => 'Mexico',
'country4' => 'France',
)
)
)
);
So I am expecting a simple example which could give me a basic idea how this can be done.
To be honest, I don't see your problem here. Since form collections extend Fieldset which extends Element, you can just add it to the form as a regular element. The view helpers will take care of the rendering recursively.
Step 1: Create a form collection (create an instance of Zend\Form\Element\Collection). If the elements have to be added dynamically in some way, I'd create a factory class for this purpose.
Step 2: Add it to the form. (For example using $form->add($myCollectionInstance).)
Step 3: Render it. Zend\Form\View\Helper\Collection is a pretty good view helper to render the whole form without any pain.
You can also create a new class extending Zend\Form\Element\Collection and use the constructor to add the fields you need. Thus, you can add it to the form using the array you've pasted in your question. Also, you could directly use it in annotations.
Hope this helps.
If you just want to fill in a select list with option values you can add the array to the select list in a controller:
$form = new MyForm();
$form->get('countr')->setOptions(array('value_options'=>array(
'country1' => 'Brazil',
'country2' => 'USA',
'country3' => 'Mexico',
'country4' => 'France',
));
the array can be fetched from db.
this is a different example for using form collections in the simplest way.
In this example it creates input text elements in a collection and fills them in. The number of elements depends on the array:
class MyForm extends \Zend\Form\Form
{
$this->add(array(
'type' => '\Zend\Form\Element\Collection',
'name' => 'myCollection',
'options' => array(
'label' => 'My collection',
'allow_add' => true,
)
));
}
class IndexController extends AbstractActionController
{
public function indexAction
{
$form = new MyForm();
$this->addElementsFromArray($form, array(
'country1' => 'Brazil',
'country2' => 'USA',
'country3' => 'Mexico',
'country4' => 'France',
));
//the above line can be replaced if fetching the array from a db table:
//$arrayFromDb = getArrayFromDb();
//$this->addElementsFromArray($form, $arrayFromDb);
return array(
'form' => $form
);
}
private function addElementsFromArray($form, $array)
{
foreach ($array as $key=>$value)
{
$form->get('myCollection')->add(array(
//'type' => '\Zend\Form\Element\SomeElement',
'name' => $key,
'options' => array(
'label' => $key,
),
'attributes' => array(
'value' => $value,
)
));
}
}
}
index.phtml:
$form->setAttribute('action', $this->url('home'))
->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form->get('myCollection'));
echo $this->form()->closeTag();
I use my class to alter decoration of my form.
In other words, instead of calling
Application_Form_Login extends Zend_Form
I use:
Application_Form_Login extends My_Form
In my "My_Form" class I define the following:
protected $_disableLoadDefaultDecorators = true;
protected $_elementDecorators = array(
'ViewHelper',
array(
'Errors',
array(
'data-icon'=>"alert",
'class'=>"ui-body ui-body-e errors"
)
),
'Label',
array(
array(
'row' => "HtmlTag"
), array(
'tag'=>"div",
'data-role'=>"fieldcontain"
)
)
);
This works perfect on my regular forms.
But once I use jQuery forms:
$this->addElement(new ZendX_JQuery_Form_Element_AutoComplete(
"ac1",
array('label' => "Your Address:"))
));
It has no effect on them, and they still render with their default decorators.
Any ideas how to globally set decorators for jQuery Form Elements as well?
I have solved the problem. Any default decorators defined this way will also work on any ZendX_JQuery_Form_Element
IF
The element is created inside of addElement function. In other words, instead of creating an element this way:
$this->addElement(new ZendX_JQuery_Form_Element_AutoComplete(
"address",
array(
'label' => "Your Address:"
)
));
You should create it this way:
$this->addElement('AutoComplete', 'address', array(
'label' => "Your Address:"
));
Because when addElement creates the element itself, it will pass the default decorators to the creating function. Otherwise the elements will be created outside of the form context.
There's no AutoComplete element in Zend_Form. So, the class you use to build your forms, that includes all your global settings and decorations (in my case: "My_Form") should extend ZendX_JQuery_Form, and not Zend_Form
ZendX_JQuery_Form_Element_UiWidget requires UiWidgetElement decorator. So we replace the ViewHelper decorator with ZendX_JQuery's: UiWidgetElement.
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');