I want to prefix the form title within the form tags, but above the form elements.
<form>
<h3>Login Form</h3>
<!-- form elements -->
</form>
I do not want to use a view script.
I thought of using the Description decorator, but that appears to be only available to the elements.
How do I set an arbitrary description for a form, and position it (either append / prepend) within the form tags? I have a feeling I need to play with the HtmlTag decorator but I've played with it and can't get the right results.
Typical. Ask a question, work out the solution.
$form->setDecorators(array(
array(
'Description',
array(
'tag' => 'div',
'class' =>'title'
)
),
'FormElements',
'Form'
))
->setDescription('Enter Login Credentials:');
Depending where you place the description decorator will determine where in the form tags it displays, i.e. place above 'FormElements' to display above the elements; place below 'FormElements' to display the description below the elements; place below the 'Form' decorator to display after the form tags.
Related
I have a simple form to filter a list in function of themes :
$builder
->add('themes', EntityType::class, array(
'class' => 'XXBundle:Theme',
'choice_label' => 'image',
...
))
->add('save', SubmitType::class)
;
And in Twig I try to access all themes to do a specific render, in fact I would like to display Image (related to each theme : theme.image) instead of the theme.
I followed other solutions : Symfony2 + CreateFormBuilder how to render an image in a form
But it doesn't work :
form.vars.value.themes / form.vars.data.themes (or form.themes.vars.value / form.themes.vars.data this is the same)
exists but are always empty. Because this is a new form. If I submit the form, it works form.vars are not empty anymore.
How can I get themes when I display the form for the first time ? I followed the doc (http://symfony.com/doc/current/reference/forms/twig_reference.html#form-variables-reference) but I can't find what I want.
TY
Basically the EntityType is a List of choices - form.themes.vars.choices. Its an array of ChoiceView, to get entity simply access public data property.
So use this property to access to the entities:
form.themes.vars.choices
More accessible attribute are listed in the doc here.
Hope this help
I struggle with getting a form to work in the way I want it to behave.
I have a Regions and Properties model, one region can have many properties and so on...
I created a form to select the Region and then a Property in that region!
The form is having both lists but I struggle to have the second list [Property] updating automagic with only the properties in the region you have selected from the first list [Regions]
When you select a different region in the list, it should update automagic the property list, so you only see the properties for that region! Sorry for my bad explaining, but not sure how to explain this any better.
This is the code in my controller:
// Retrieve the region list
$this->set('regions', $this->Region->find('list', array(
'fields' => array('Region.id', 'Region.regionname'),
'order' => 'regionname',
)));
// Retrieve Property list for the regions.
$this->set('properties', $this->Region->Property->find('list', array(
'conditions' => array('Property.live' => true ),
'fields' => array('Property.id','Property.description'),
'order' => 'id',
)));
This is part of my form.
<?php echo $this->Form->create('Upload', array('action' => 'add', 'type' => 'file')); ?>
<?php echo $this->Form->input('region_id', array('label' => 'Select Region:')); ?>
<?php echo $this->Form->input('property_id', array('label' => 'Select Property:')); ?>
<?php echo $this->Form->file('file'); ?>
I have spend a lot of time looking around here and on youtube, but can't find it :-(
There's no automagic way to do what you're asking. Since all the data for dropdowns have already beed displayed on load, the only way to change the second dropdown depending on the first select is via javascript.
If you search for "dropdown on select" or something similar for cake, you'll find solutions to do it with ajax or plain js. I leave you one reference here. That one is done with ajax and a new action. But you could also do it with just js, doing a find for Regions and Properties and setting them in a json variable in js to be manipulated.
I am aware that form errors can be set to render at the top of the form using decorator code such as:
$form->setDecorators(array(
array('FormElements'),
array('FormErrors'),
However, I have subforms within my (parent) form and I need to render the subforms errors - aggregated and rendered at the top of the parent form. How can this be achieved please? Thanks.
I am a bit new to Zend but I have a similar implementation that acheives this. Although I also have extra code in here that makes the errors display in a custom manner and also puts the subforms into seperate divs.
The important parts to notice are the facts that the errors are turned off on both forms by not specifying the decorator FormErrors and then creating a new errors decorator on the parent form.
On my main form I've added a FormErrors decorator such as this:
$form->setDecorators(array(
new Zend_Form_Decorator_FormErrors(array(
'ignoreSubForms'=>false,
'markupElementLabelStart'=> '<p>',
'markupElementLabelEnd'=> '</p>',
'markupListStart'=>'<div class="formErrors">',
'markupListEnd' => '</div>',
'markupListItemStart'=>'<div>',
'markupListItemEnd'=>'</div>'
)),
'FormElements'
));
Then on the subform you turn off the form errors
$subForm->setDecorators(array(
'FormElements',
array(
array('data' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'formRow')
)
));
Is there some parameter or workaround I can use to have the form helper render a field as if it were required by the model even if the model does not actually require it?
(I'm looking for the cakephp required rendering type, not the actual 'required' form property making this a different question than Cakephp Form Helper)
All the "required" style is is a specific CSS class you can set yourself:
$this->Form->input('foo', array('div' => array('class' => 'input text required')));
I have a search module. It has one form within it that produces the search field, I am using a viewScript that is stored within views/scripts/forms to render the form.
I want this form to appear on ever screen so I have written a ViewHelper that creates the from and returns it. This works fine when I am within the search module but when I am in any other module I am getting an error.
Any ideas why?
This is what is used to create the viewScript in the form
$this->setDecorators(array(
array( 'ViewScript', array('viewScript' => 'forms/Search.phtml'))
));
Thanks,
Martin
In my code I have to put a / in front of the path so for you it would mean:
$this->setDecorators(array(
array( 'ViewScript', array('viewScript' => '/forms/Search.phtml'))
));