How to make own frontend-Forms with Magento 1.6.2 - forms

I have make a little Extension for Magento 1.6.2. I managed to write code in the backend-system to create a EAV Model to the database and I can write/read items from it like this tutorial: http://www.pierrefay.com/magento-admin-gridview-85
How can I use the following Forms in the frontendsystem, .. I have see there are dont classes like Mage_Adminhtml_Block_Widget_Form. I dont want to use own HTML constructions, want to get I want Magento look and feel. Have anyone a idea how to make own forms in magento frontend with magento classes?
class Extension_Name_Adminhtml_Printcatalog_Edit_General_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('printcatalog_form',
array( 'legend' => __('Allgemeine Informationen')));
$fieldset->addField('catalog_name', 'text',
array(
'label' => __('Katalogname'),
'class' => 'required-entry',
'required' => true,
'name' => 'catalog_name',
));
$fieldset->addField('release_date', 'text',
array(
'label' => __('Erscheinungsdatum'),
'class' => 'required-entry',
'required' => true,
'name' => 'release_date',
// 'image' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'/adminhtml/default/default/images/grid-cal.gif',
// 'format' =>
Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
));
if (Mage::registry('printcatalog_data'))
{
$form->setValues(Mage::registry('printcatalog_data')->getData());
}
return parent::_prepareForm();
}
}
?>

Not possible out of the box, all magento front-end forms are hard coded. As you can see in the class you provided it is part of the Adminhtml module (Mage_Adminhtml_Block_Widget_Form), which is for the administration dashboard within magento.
The amount of development to integrate a form class like in your code is not worth the time or flexibility of a hardcoded front-end form ... in most cases. If the majority of your continued development revolved around forms, then I'd reconsider building out abstract form classes to help in the creation of your forms via the controller.
On a higher note, Magento does provide a fairly decent javascript validation system for your front-end.

You should look into Zend_Form, which came around after Magento/Varien's original form implementation.

Related

Symfony 2.3 add form collections of entities

I'm triying to make a dynamic form adding collections inside an entity.
I have followed the code example in the Symfony's documentation, and it works, but what I want to do is add a new form (the form of the entity collections).
So, if I have an entity A that contains a collection of entities B, I want to add new entities B dynamically in the form, but I don't know how to do it.
The entity A form should be something like:
$builder->add('entityB', 'collection', array(
'type' => 'HOW TO PUT THE FORM OF THE ENTITY B???',
'options' => array(
'required' => false,
),
'allow_add'=>true,
));
Taken from the Cookbook:
$builder->add('entityB', 'collection', array(
'type' => new EntityBType(),
'options' => array(
'required' => false
),
'allow_add' => true
));
This is assuming that you have created a Form Type Class for EntityB (not manually creating it when needed in your controller). The linked cookbook entry gives a lot of good examples based on per-case situations.

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 to use the ChoiceListInterface in Symfony 2?

I'd like to display a dynamic list of checkboxes in a form.
So far, I built a form embedding a static list of checkboxes, and I created a Tag entity for different values in different languages and populated the database. I'd like to replace the static checkboxes by a dynamic list based on the Tag entity.
The documentation says I should use the ChoiceListInterface. But it is really poorly documented. Would you have an example or a global logic explanation to help me ?
You can extend LazyChoiceList abstract class and implement loadChoiceList() method, create a service of it, inject it to the form and set it as choice_list option.
Finally, I used an entity field type :
->add('tags', 'entity', array(
'class' => 'bndMyBundle:Tag',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('t')
->orderBy('t.en', 'ASC');
},
'expanded' => true,
'multiple' => true,
'property' => 'en',
))
Then, I just need to replace the 'en' value by the user's current locale to choose the right language.

Example of Zend Form with Collection Element using Forms not Fieldsets in Zend Framework2

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();

Translate select options in Symfony2 class forms

I'm using a class form in Symfony2 Beta3 as follows:
namespace Partners\FrontendBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ConfigForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('no_containers', 'choice', array('choices' => array(1 => 'yes', 0 => 'no')));
...
I want to translate the 'yes' and 'no' options, but I don't know how to use the translator here.
You can use the translation resources as usual. This worked for me:
$builder->add('sex', 'choice', array(
'choices' => array(
1 => 'profile.show.sex.male',
2 => 'profile.show.sex.female',
),
'required' => false,
'label' => 'profile.show.sex.label',
'translation_domain' => 'AcmeUserBundle'
));
And then add your translations to the Resources->translations directory of your Bundle.
Update from #CptSadface:
In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:
'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',
Without specifying the domain, options are not translated.
I searched a while to find an answer, but finally I found out how Symfony translates form content. The easiest way in your case seems to be to just add a translation for "yes" and "no" by adding a YAML or XLIFF translation file to your application (e.g. app/Resources/translations/messages.de.yml) or your bundle. This is described here:
http://symfony.com/doc/current/book/translation.html
The problem - in my opinion - is that you don't seem to be able to use custom translation keys. The guys from FOSUserBundle solve this (or a similar) problem with "Form Themes" (http://symfony.com/doc/2.0/cookbook/form/form_customization.html). Here are two significant lines of code to achieve the usage of the form element id as translation key:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig#L1 / https://github.com/FriendsOfSymfony/FOSUserBundle/blob/50ab4d8fdfd324c1e722cb982e685abdc111be0b/Resources/views/form.html.twig#L4
By adding a Form Theme you're able to modify pretty much everything of the forms in the templates - this seems to be the right way of doing this.
(Sorry, I had to split two of the links b/c I don't have enough reputation to post more than two links. Sad.)
In symfony 2.7, using the choice_label argument, you can specify the translation domain like this:
'choice_label' => 'typeName',
'choice_translation_domain' => 'messages',
Without specifying the domain, options are not translated.
CptSadface's answer was what helped me with translating my entity choices.
$builder
->add(
'authorizationRoles',
null,
[
'label' => 'app.user.fields.authorization_roles',
'multiple' => true,
'choice_label' => 'name', // entity field storing your translation key
'choice_translation_domain' => 'messages',
]
);