How to render some html in Zend form? - zend-framework

In my form i need this:
<li class ="af_title"><div>Contact form</div></li>
I understand that if I need for example a text field - then I'd just create Zend_Form_Element_Text and wrap HtmlTag decorator around it - right?
questions:
1) how would I generate div form element so I can wrap decorator around it?
2) mmm. - how would I set content of said element to be "Contact form"?
Thanks:)

To wrap your Contact form around div which is inside <li class ="af_title"> you can do as follows:
$yourForm = new YourForm();
$yourForm->addDecorator(array('div' => 'HtmlTag'), array('tag' => 'div'));
$yourForm->addDecorator(array('li' => 'HtmlTag'), array('tag' => 'li', 'class' => 'af_title'));
EDIT:
I see that you speak of div form element. Such element does not exist, but you could easily create it. For this you need two things: new form element (i.e. div element) and a form view helper that would take care of generation of the html. I allowed myself to prepare simple examples of these two elements to show what I mean:
Div.php in APPLICATION_PATH . /forms/Element:
class My_Form_Element_Div extends Zend_Form_Element {
/**
* Default form view helper to use for rendering
* #var string
*/
public $helper = 'formDiv';
public function __construct($spec, $options = null) {
parent::__construct($spec, $options);
$this->removeDecorator('label');
$this->removeDecorator('htmlTag');
}
}
FormDiv.php in APPLICATION_PATH . /views/helpers:
class My_View_Helper_FormDiv extends Zend_View_Helper_FormElement {
public function formDiv($name, $value = null, $attribs = null) {
$class = '';
if (isset($attribs['class'])) {
$class = 'class = "'. $attribs['class'] .'"';
}
return "<li $class><div>$value</div></li>";
}
}
Having this, you can just add the div form element to any form element in their init() method as follows:
$div = new My_Form_Element_Div('mydiv');
$div->setValue('Contact form')->setAttrib('class', 'af_title');
$this->addElement($div);

Related

How to pass default data when creating a form holding a CollectionType?

I am trying to create multi import data, so I need many form collection this same class in one place. I can do this using collection type.
I created pictures Class with have one field: pictures and it is arrayCollection type like this:
class Pictures {
protected $pictures;
public function __construct()
{
$this->pictures = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addPicture(Picture $picture)
{
$this->pictures[] = $picture;
return $this;
}
public function removePicture(Picture $picture)
{
$this->pictures->removeElement($picture);
}
public function getPictures()
{
return $this->pictures;
}
}
Next I created pictures form with collection to pictureType.
$builder
->add('pictures', 'collection', array(
'prototype' => false,
'by_reference' => true,
'entry_type' => PictureType::class,
'options' => array('label' => false))
)
;
Next I have picture Entity with fields for example name, width, height, image (nevermind).
So my PictureType is a simple form fith fields that same like Entity (standard).
Now I try to render this form and add some picture in controller (not from database). Then I create Picture Object and next I add it to pictures->addPicture($picture);
$pictures = new Pictures();
foreach($data as $d){
$picture = new Picture();
$picture->setName($d['title']);
$picture->setImage($d['thumbnail_400_url']);
$picture->setWidth($d['width']);
$picture->setHeight($d['height']);
$pictures->addPicture($picture);
}
$form = $this->createForm('PicturesType', $pictures, array(
'action' => $this->generateUrl('dashboard_fotolia_save'),
'method' => 'POST',
));
After it if rendered form is ok. I can see and change values.
But after submit form data disapear. I hope data will be stored in response but not:(
I want to use it only for create new object. Could you help me?
Form errors: This form should not contain extra fields
Actually it seems that your PicturesType holds a pictures field since you add it with :
$builder->add('pictures', 'collection', ...
So you got a form type with holds an array of data with a key 'pictures' for the corresponding fields.
You may want to set it by :
$form = $this->createForm('PicturesType', array('pictures' => $pictures), ...

SilverStripe - Multilingual Custom Form Template

I am building a page with two languages (English and German). For this I am using the translatable module offered on the SilverStripe page (http://addons.silverstripe.org/add-ons/silverstripe/translatable). On regular pages it works just like a charm.
The contact form gets created in a controller. By $form->setTemplate I load a custom template for the form, which is located in the template's include subdirectory. In the class file I defined the different TextFields (model part), which I want to use in the form template.
Here is the controller:
class MietenPage extends Page {
private static $db = array (
//Form
'ChoosPakage' => 'Varchar',
'FormEventHeadline' => 'Varchar',
'FormNoteHeadline' => 'Varchar',
'FormContactHeadline' => 'Varchar',
'FormAdditionalHeadline' => 'Varchar',
'FormCheckOverlay' => 'Varchar',
'FormCheckCaseDesign' => 'Varchar',
'FormCheckAGB' => 'Varchar',
'FormSubmit' => 'Varchar',
'AGBAlert' => 'Varchar'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
//...
return $fields;
}
}
class MietenPage_Controller extends Page_Controller {
private static $allowed_actions = array('ContactForm');
public function ContactForm() {
$fields = new FieldList(
//...
);
$actions = new FieldList(
new FormAction('submit', 'Anfrage Senden!')
);
$form = new Form($this, 'ContactForm', $fields, $actions);
$form->setTemplate('ContactFormTemplate');
return $form;
}
public function submit($data, $ContactForm) {
$email = new Email();
//...
}
}
In my template I am calling the form using $ContactForm. This works totally fine, including all functionalities.
My Problem is, that I can not access the variables (like headline or submit button text), which hold the text in my custom template - just emptyness is returned. I think it has to do something with scope, but I can not solve this problem.
From your example I can't see where your variables live, and how you planned to use them, but to create a multlingual form, you'd best use the _t() functionality: https://docs.silverstripe.org/en/3.1/developer_guides/i18n/#translating-text
You'd use that for any 'hardcoded' text, in php as well as in your templates.

Zend_Form renders all fields as text

I have a Zend_Form form, with some custom decorators, like this:
$decorators = array();
$decorators[] = new Zend_Form_Decorator_ViewHelper(array());
$decorators[] = new Zend_Form_Decorator_Errors;
$decorators[] = new Zend_Form_Decorator_HtmlTag(array('tag' => 'div', 'class' => 'form-item'));
$decorators[] = new Zend_Form_Decorator_Label(array('class' => 'form-label'));
$decorators[] = new Zend_Form_Decorator_Callback(array(
'callback' => function($content, $element, $options) {
return sprintf('<div class="form-row">%s</div>', $content);
},
'placement' => false
));
$this->setElementDecorators($decorators);
The problem is, that all of the fields are rendered as text inputs. Why does it happen?
EDIT: I discovered, that it doesn't render all the inputs necessarily as text inputs, but renders them with type of the first input in form. Here is example of a form that i use(the decorators are set int parent's init):
<?php
class Form_Users_Add extends Form_Base {
protected $pbxs = array(1 => 'Element 1', 2 => 'Element 2');
public function init() {
$monitors = new Zend_Form_Element_Checkbox('prefered_screen_count');
$monitors->setCheckedValue(2);
$monitors->setUncheckedValue(1);
$monitors->setLabel('two_monitors');
$this->addElement($monitors);
$pbx = new Zend_Form_Element_Select('asterisk_id');
$pbx->setMultiOptions($this->pbxs);
$pbx->setLabel('users_asterisk_id');
$this->addElement($pbx);
parent::init();
}
}
Yay! I have solved the issue! The cause was that I used INSTANCES of classes, not the names. This way every element was using the same instance of the decorator.

ZEND form elements in a table containing also data from the database

Hi there:) i've got a problem with decorators and form which would be in table and in this table want to have also data from database... I dont have any idea how to do this to have a structure like something below, lets say
<table>
<tr>
<td><?php echo array[0]['name']?>
//and here input from zend form
<td>
<select name='foo' id='bar'>
<option value='something'>Foo</option>
<option value='something2'>Foo2</option>
</select>
</td>
</tr>
</table>
Ofcourse tr will be more and generated with foreach or some loop.
I have something like this:
<?php
class EditArticles_Form_EditArticles extends Zend_Form
{
protected $uid;
public function render()
{
/* Form Elements & Other Definitions Here ... */
$this->setName('editarticles');
$data = new EditArticles_Model_DbTable_EditArticlesModel();
$datadata = $data->GetArticlesToEdit($this->getUid()); //here is my data from db
for ($i=0;$i<count($datadata);$i++)
{
$do = new Zend_Form_Element_Select(''.$i);
$do->addMultiOption('0', 'Aktywny');
$do->addMultiOption('1', 'Nieaktywny');
$this->addElements(array($do));
}
$submit = new Zend_Form_Element_Submit('updateart');
$this->addElement($submit);
//and here are decorators for array, and i would like to have in this table also data from array containing data from database
$this->addDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'table', 'id' => 'aaaa', 'style' => 'width:500px;')), 'Form',
));
$this->setElementDecorators(array(
'ViewHelper',
array( array('data' => 'HtmlTag'), array('tag' => 'td', 'style' => 'width:200px;')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
),
//wykluczenie submita z overrida stulu
array('submit'), false);
return parent::render();
}
//setting user id for get content from db
public function setUid($uid) {
$this->uid = $uid;
return $this;
}
public function getUid() {
return $this->uid;
}
}
?>
output of code above is something like this: (in red marked where i would like to have that selects from form. In this image the table with data is an other table generated in phtml, but i would like to generate that table by form od just insert only the form elements to that table generated in phtml view).
http://img14.imageshack.us/img14/9973/clipboard01pw.png
Something found here:
Zend_Form: Database records in HTML table with checkboxes
but i dont know how to start with that...
Several comments:
Typically, adding elements to the form is done in init(), rather than render().
If a consumer object (this is this case, the form) needs a dependency (in this case, the article model) to do its work, it is often helpful to explicitly provide the dependency to the consumer, either in the consumer's constructor or via setter method (ex: $form->setArticleModel($model)). This makes it easier to mock the model when testing the form and clearly illustrates the form's dependence on the model.
Re: rendering other content in the form via decorators: Maybe, take a look at the AnyMarkup decorator. It looks like (sorry, can't fully understand the Polish) you want a select box on each row you output. So, you get your rows using the model, loop through the rows, creating your select box on each row. When you assign decorators to the select element - ViewHelper, Errors, probably an HtmlTag decorator to wrap it in a <td> - you also add the AnyMarkup decorator to prepend the a bunch of <td>'s containing your row data, finally wrapping the whole row in <tr>.
Perhaps something like this (not fully tested, just to give the idea):
class EditArticles_Form_EditArticles extends Zend_Form
{
protected $model;
public function __construct($model)
{
$this->model = $model;
parent::__construct();
}
public function init()
{
$rows = $this->model->GetArticlesToEdit($this->getUid());
$numRows = count($rows);
for ($i = 0; $i < $numRows; $i++) {
$do = new Zend_Form_Element_Select('myselect' . $i);
$do->addMultiOption('0', 'Aktywny');
$do->addMultiOption('1', 'Nieaktywny');
$do->setDecorators(array(
'ViewHelper',
array(array('cell' => 'HtmlTag'), array(
'tag' => 'td'
)),
array('AnyMarkup', array(
'markup' => $this->_getMarkupForRow($i, $row),
'placement' => 'PREPEND',
)),
array(array('row' => 'HtmlTag'), array(
'tag' => 'tr'
)),
));
$this->addElement($do);
}
}
protected function _getMarkupForRow($i, $row)
{
return '<td>' . $i . '</td>' .
'<td>' . $row['nazwa'] . '</td>' .
'<td>' . $row['typ'] . '</td>' .
'<td>' . $row['rozmiar'] . '</td>';
}
}
A final note: Remember to register an element decorator prefix path as follows (in the form, probably in init()):
$this->addElementPrefixPath('My_Decorator', 'My/Decorator', self::DECORATOR);
This allows the element to resolve the short name AnyMarkup into a full classname My_Decorator_AnyMarkup.

Extending Zend_View_Helper_FormElement

I have created this file at My/View/Helper/FormElement.php
<?php
abstract class My_View_Helper_FormElement extends Zend_View_Helper_FormElement
{
protected function _getInfo($name, $value = null, $attribs = null,
$options = null, $listsep = null
) {
$info = parent::_getInfo($name, $value, $attribs, $options, $listsep);
$info['id'] = 'My new ID';
return $info;
}
}
How can i get the normal form elements to use this instead?
Why i want this?
Say that i use the same form multiple times on a page, the 'id='-tag of the formelements will apear multiple times, this is not w3c-valid. So initially i want to prefix the id with the id of the form.
Any better ideas or ways to do this is much apreciated.
Update: Just realized it's the same problem with the decorators :( Don't think this is the right path i've taken.
Create new form class extending Zend_Form and in the init() method use variable $ns to add prefix/suffix to all elements. You can set $ns variable through form constructor.
class Form_Test extends Zend_Form
{
protected $ns;
public function init()
{
$this->setAttrib('id', $this->ns . 'testForm');
$name = new Zend_Form_Element_Text('name');
$name->setAttrib('id', $this->ns . 'name');
$name->setLabel('Name: *')->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', $this->ns . 'submitbutton');
$submit->setLabel('Add')->setIgnore(true);
$this->addElements(array($name, $submit));
}
public function setNs($data)
{
$this->ns = $data;
}
}
In the controller or wherever you are calling this forms specify each form instance:
$form1 = new Form_Test(array('ns' => 'prefix1'));
$this->view->form1 = $form1;
$form2 = new Form_Test(array('ns' => 'prefix2'));
$this->view->form2 = $form2;
// Validation if calling from the controller
if ($form1->isValid()) ...
Using multiple instances of same forms on a page can be validated if used as subform.
SubForms prefix all id's with the name/identifier of the subform.