Form fields and label position in Zend framework 2 - forms

I have a Zend form created using Zend\Form\Annotation. I am using following code to generate the HTML:
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
The problem is labels are placed just before the input text fields like this:
Label: Input text Box
I want to do like this:
Label:
Input Text Box
How can I format my labels to get my desired position?

What I am going to do is use individual elements print instead of using formCollection method like below:
<dl class="zend_form">
<?php echo $this->formElementErrors($form->get('identity')) ?>
<dt><?php echo $this->formLabel($form->get('identity')) ?></dt>
<dd><?php echo $this->formInput($form->get('identity')) ?></dd>
<dt><?php echo $this->formLabel($form->get('credential')) ?></dt>
<dd><?php echo $this->formInput($form->get('credential')) ?></dd>
<?php if ($this->redirect): ?>
<input type="hidden" name="redirect" value="<?php echo $this->redirect ?>" />
<?php endif ?>
<dd><?php echo $this->formButton($form->get('submit')) ?></dd>
</dl>

You just could use css:
label {
display: block;
}

just give the elements an id in your annotation like #Annotation\Attributes({"id" : "id"})

Related

bootstrap codeigniter form input and submit button

I am trying to go along with bootstrap and Codeigniter, but I am having a little problem, I cant seem to figure out how to align 3 simple elements into one row, so they are aligned perfectly next to each other.
I want my H3 tag, input field and my submit button from my form to be next to each other. I have already tried the span4 on the 3 objects within divs, then also display:inline, but nothing works correctly. I dont know why, but If I want them to be perfectly next to each other, I just have to put there margin-top: -7px and other margins on the other objects but this looks like a not a good approach on this matter at all. Could you please help me ? !
My code:
<div class="well">
<h3>My superb h3 tag!</h3>
<?php echo form_open('search/quick_search'); ?>
<?php echo form_error('search_query'); ?>
<?php echo form_input('search_query','','placeholder="Search query"'); ?>
<?php echo form_submit('submit_quick_search','Search','class="btn"'); ?>
<?php echo form_close(); ?>
How does it look
This is how I've been using then together (Horizontal form)
<?php echo form_open('search/quick_search', 'class="form-horizontal');?>
<div class="control-group <?php echo form_error('search_query')? 'error' : ''; ?>">
<label class="control-label" for="search_query">Search Term:</label>
<div class="controls">
<?php echo form_input('search_query','','placeholder="Search query"'); ?>
<span class="help-inline"><?php echo form_error('search_query'); ?></span>
<span class="help-block"></span>
</div>
</div>
<div class="form-actions">
<?php echo form_submit('submit_quick_search', 'Search', 'class="btn"'); ?>
</div>
<?php echo form_close(); ?>

"Available Product Listing Sort By" will not remove options from drop-down

I want to remove/hide some attribute sort options in my categories.
For this I unchecked "use all attributes" and selected the attributes I want to display in the sort select.
After this I cleared cache and reindexed categories and products data.
But I still have all attributes showing in the sort by select. Can somone help me please?
I'm using a modified toolbar.phtml to hide the "position" sort option, but I think this has nothing to do with the problem:
<div class="sort-by">
<label><?php echo $this->__('Sort By') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<?php if ($_order != $this->__('Position')) : // Remove "Position" from the sort option list ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<?php if($this->getCurrentDirection() == 'desc'): ?>
<img src="<?php echo $this->getSkinUrl('images/i_desc_arrow.gif') ?>" alt="<?php echo $this->__('Set Ascending Direction') ?>" class="v-middle" />
<?php else: ?>
<img src="<?php echo $this->getSkinUrl('images/i_asc_arrow.gif') ?>" alt="<?php echo $this->__('Set Descending Direction') ?>" class="v-middle" />
<?php endif; ?>
</div>
EDIT:
Here's an image from my display settings inside the category:
But inside my storeview all options are displayed
The problem for me was exactly what the error was saying, it just was hard to understand. You can't pick a default sort option that isn't enabled
Okay, fixed the problem:
I changed the settings in all store views, but it didn't changed in all sotre views. When I tried to change the setting Use Default Value in my "languagesviews". I get the error "Default Product Listing Sort by not exists on Available Product Listing Sort By".
So I "just" need to change all categorysettings of each language.
EDIT:
After editing some categories and checking/unchecking "use default values" the error is gone and I set all "use default values" to yes.

Zend Custom ViewScript for Form not rendering file element

I created a custom viewscript for a zend form. In the form, there is a file element. When I run the code echo $this->element->elementname->renderViewHelper() it works fine on all elements except the file element. When I run the following code on my file element within my custom viewscript, i get the following error: Uncaught exception 'Zend_Form_Element_Exception' with message 'Decorator by name ViewHelper does not exist'
<?php if($this->element->avatarimage->hasErrors()):?>
<div class="control-group error">
<?php else: ?>
<div class="control-group">
<?php endif;?>
<label for="avatarimage" class="control-label">Avatar Image</label>
<div class="controls">
<?php echo $this->element->avatarimage->renderViewHelper();?>
<?php if($this->element->avatarimage->hasErrors()):
$messages = $this->element->avatarimage->getMessages();
$messages = array_values($messages);
$message = $messages[0];
echo '<span class="help-inline">'.$message.'</span>';
endif;
?>
</div>
</div>
How can I incorporate my file element into my custom form viewscript?
This should definitely work.
<? echo $this->element->avatarimage->renderFile(); ?>
Try like this:
<?php echo $this->element->avatarimage;?>

Place an image for photo form button in Zend form

I have created a form by Zend form that uploads pictures in jpg and png format. however, the form generates a default button and text that reads "no file chosen". How can I place my own designed button image and remove the default text for this form?
$photo = new Zend_Form_Element_File('photo');
$photo->addValidator('Count', false, 1)
->addValidator('Size', false, 200*1024)
->addValidator('Extension', false, 'jpg,png');
It looks like you need to use Zend_Form_Element_Buttom if you want to use an image.
Just make sure you pass in type=submit and you can use the style="" attribute to set your image.
You can pass in these using:
setAttribs(array('type'=>'submit', 'style'=> array('image' => 'image.png')));
I think this syntax is correct, but I'm sure I have the incorrect stlye attributes.
MDN Button Reference
$photo->setDecorators(array(
array('ViewScript', array('viewScript' => 'my_upload_button.phtml', 'abc' => $some_parameter)
);
then inside
my_upload_button.phtml do your own style, here is mine
<?php
$image = $this->element->getAttrib('class');//prendi il nome del file
$this->element->setAttrib('class', '');//cancella la class
?>
<td class="nome_campo">
<label for="<?php echo $this->element->getName(); ?>"><?php echo $this->element->getLabel(); ?></label></td>
<td class="valore_campo" colspan="1">
<?php echo $this->content; ?>
<?php if ($this->element->hasErrors()): ?>
<span class="error">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</span>
<?php endif; ?>
<p class="hint"><?php echo $this->element->getDescription() ?></p>
<b>
<?php

Rendering only the <form> tag

Is there any way that i can render ONLY the start <form> tag of a Zend_Form object?
print $this->registerForm->renderForm();
renders <form></form>, and i only need <form>
Edit:
After Asleys possible solution i wrote this for My_Form class
public function renderFormOpen() {
return str_replace('</form>', '', $this->renderForm());
}
public function renderFormClose() {
return '</form>';
}
Still looking for at ZF way of doing thins, even though i don't think there is any - after going through the code in the ZF library.
You could write an custom form-decorator that uses a custom view-helper that only renders the open form tag. But I think this would be overkill.
Just "hardcode" the form-tags and fill the attributes with the data provided by the form-variable in your view.
<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
enctype="<?php echo $this->form->getEnctype() ?>"
method="<?php echo $this->form->getMethod() ?>"
id="<?php echo $this->form->getId() ?>"
class="<?php echo $this->form->getAttrib('class') ?>" >
<!--in case your products are represented as elements -->
<?php foreach ($this->form->getElements() as $element): ?>
<?php echo $element ?>
<?php endforeach; ?>
<!--in case your products are represented as displayGroups -->
<?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
<?php echo $displayGroup ?>
<?php endforeach; ?>
<!--in case your products are represented as subforms -->
<?php foreach ($this->form->getSubforms() as $subform): ?>
<?php echo $subform ?>
<?php endforeach; ?>
<!--in case your products are rendered by a view helper -->
<?php foreach ($this->products as $product): ?>
<?php echo $this->renderProduct($product) ?>
<?php endforeach; ?>
</form>
Just for fun the overkill way
// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');
// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
// replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
// replace method "form" with onlyOpenTagForm"
// replace
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
// with:
if (false !== $content) {
$xhtml .= $content;
}
Done! - The Java-Guys will love it ;)
You can render just the open form tag by passing false to the form decorator like so:
<?php echo $this->form->renderForm(false) ?>
Which will output something like:
<form id="post" enctype="multipart/form-data" method="post" action="/post">
Additonally you can pass a string to the form decorator to be enclosed by the form tags like so:
<?php echo $this->form->renderForm('Some Text') ?>
Which outputs something like:
<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>
Hope this helps...
You could do something like this:
echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);