Zend form and input button - zend-framework

I'm trying to add button input to the form element. I'd like the rendered code to be <input type="button" ..."> I tried setting attribs field like so:
$this->addElement('submit', 'cancel', array(
'ignore' => true,
'label' => 'Anuluj',
'attribs'=>array('class' => 'class_name', 'type' => 'button') )
);
but i still got <input type="submit" ..."> instead <input type="button" ...">
Setting class attribute works, but setting the type doesn't. Any Idea to get type="button"?

Use button as the first parameter instead, which will give you a <button> HTML element instead (functionally the same as <input type="button" ..>).

Please use the below code for button
$this->addElement('button', 'submitted');
$submitElement = $this->getElement('submitted');
$submitElement->setAttrib('class',"btn-primary btn");
$submitElement->setAttrib('value', 'submitted');
$submitElement->setLabel('SUBMIT');
The Html code is,
<button name="submitted" id="submitted" type="button" class="btn-primary btn">SUBMIT</button>

Related

TYPO3 Indexed Search Url on submit

i use TYPO3 7.6.10
i use indexed_search 7.6.0
when i submit form i go to the target page and i get the results.
The url of target page is:
search.html?tx_indexedsearch_pi2%5Baction%5D=search&tx_indexedsearch_pi2%5Bcontroller%5D=Search
i want to remove action and controller variable form url to get:
search.html
i can do that adding a configuration to real url like this:
'searchConfiguration' => array(
array(
'GETvar' => 'tx_indexedsearch_pi2[action]',
'valueMap' => array(),
'noMatch' => 'bypass'
),
array(
'GETvar' => 'tx_indexedsearch_pi2[controller]',
'valueMap' => array(),
'noMatch' => 'bypass'
)),'135' => 'searchConfiguration'
Now i get a nice url but the submitted data is not sent!
How can i resolve it?
Those parameters are required for the controller to be routed by the request. Without them the sWord will not become processed by the controller and you will not get any results.
Instead of bypassing them you can rewrite them to get something like /search/perform/results/ or you can configure your form to use method="POST" instead of "GET" and add those parameters above into hidden fields of the form and make sure the form attribute does not have the arguments as parameters in the action. Example form in the result:
<form method="POST" class="header-search-form hidden-xs hidden-sm" action="suche.html">
<input type="hidden" name="tx_indexedsearch_pi2[controller]" value="Search">
<input type="hidden" name="tx_indexedsearch_pi2[action]" value="search">
<div class="input-group">
<input type="text" class="search-query form-control" placeholder="Suchen" id="default-search-input" name="tx_indexedsearch_pi2[search][sword]">
<span class="input-group-btn">
<button class="btn" type="button">
<i class="fa fa-search" aria-hidden="true"></i>
</button>
</span>
</div>
</form>

Zend Form Rendering and Decorators (Use correctly with bootstrap)

The Bootstrap Example Code
http://getbootstrap.com/css/#forms
Copying a simple email input element from getbootstrap.com suggests we format the HTML in the following way:
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input id="exampleInputEmail1" class="form-control" type="email" placeholder="Email">
</div>
Above we have a <label> tag that closes straight after it's text content, "Email address".
I would now like to create the same form group using Zend Framework.
module/MyApp/src/MyModule/Form/MyForm.php
namespace MyModule\Form;
use Zend\Form\Form;
class MyModuleForm extends Form {
public function __construct($name = null)
{
$this->add(array(
'name' => 'email_address',
'type' => 'Email',
'options' => array(
'label' => 'Email address',
),
'attributes' => array(
'class' => 'form-control',
'placeholder' => 'Email'
)
));
The Zend Framework generated code
<div class="form-group">
<label>
<span>Email address</span>
<input class="form-control" type="email" placeholder="Email" id="exampleInputEmail1">
</label>
</div>
In the above HTML you can see that Zend has not closed the <label> tag. Instead the <label> ecapsulates its children.
How do I change the way the Zend rendering works?
I assume you are using ZF2 FormRow view helper to render your form element in your view script, e.g. $this->formRow($this->form->get('email_address'));
To render it differently you need to use the following view helpers
FormLabel
FormText
FormElementErrors
If for example you wanted to render as a definition list you would use something like
<dl class="zend_form">
<dt><?php echo $this->formLabel($this->form->get('email_address')); ?></dt>
<dd><?php echo $this->formText($this->form->get('email_address')); ?>
<?php echo $this->formElementErrors($this->form->get('email_address')); ?></dd>
</dl>
I hope this points you in the right direction

can we apply class to checkbox input instedad of container div in cakephp

Here the code for checkbox generation
//code
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox','div'=>'col-md-9',
'class' => 'required'
));
//output
<div class="required" aria-required="true">
<input type="checkbox" id="FormData6783" value="83" name="data[Model][field][]">
<label for="FormData6783">Sr. Secondary</label>
</div>
<div class="required" aria-required="true">
<input type="checkbox" id="FormData6783" value="83" name="data[Model][field][]">
<label for="FormData6783">Secondary</label>
</div>
it applies class to container div instead of input.. Is there any way to apply class to input ?
You will have to extend the FormHelper with your own helper and then overload the select method to change the code that generates the select. Check the BoostCake plugin to get an idea of one way o changing FormHelpers output.
You can then alias the helper to replace it app wide:
public $helpers = ['Form' => ['className' => 'MyForm']];

zend form element add div after input field

my zend form generates following code:
<dt id="register_username-label">
<label for="register_username" class="required">Membername*</label>
</dt>
<dd id="register_username-element">
<input type="text" name="register_username" id="register_username" value="" />
</dd>
but in some cases (handled in a separete decorator) i need to add some more html next to the input field (also in the dd tag). i have a instance of Zend_Form_Element_Text witch i could add some more decorators, but i don't know how to get this done :(
solution should look like this:
<dt id="register_username-label">
<label for="register_username" class="required">Membername*</label>
</dt>
<dd id="register_username-element">
<input type="text" name="register_username" id="register_username" value="" />
<div class="validate"><div class="validate-check"></div></div>
</dd>
You can add decorators to your form element in form file just as below
$form->addElement(
'text',
'register_username',
array(
'required' => false,
'decorators' => array(
array(
'HtmlTag', array(
'tag' => 'div',
'class' => 'validate'
)
)
)
)
);
And more i would like to share with you one interesting Link to understand how zend form is basically works.
Please let me know if i can help you more.

Styling individual radio buttons in drupal form

I have this group of radio buttons in which each of individual button has of its own position set through style attribute. I would like to how can I archive the same by using drupal form api. I found how to style as whole but, not as individual control within group. Here's how my html code look like -
<input type="radio" name="base_location" checked="checked" value="0" style="margin-left:70px;float:left;"/><span style="float:left;">District</span>
<input type="radio" name="base_location" value="1" style="margin-left:50px;float:left;"/><span style="float:left;">MRT</span>
<input type="radio" name="base_location" value="2" style="margin-left:60px;float:left;"/><span style="float:left;">Address</span>
And this is the drupal code I'm stuck at -
$form['base_location'] = array(
'#type' => 'radios',
'#title' => t('base location'),
'#default_value' => variable_get('search_type', 0),
'#options' => array(
'0'=>t('District'),
'1'=>t('MRT'),
'2'=>t('Address')),
'#description' => t('base location'),
I'm aware of #type=>radio existence. However, I do not know how to group all my radio buttons together in this regard. If I use same array key for all of them, they would collide each other. If I don't, they aren't seen as part of the same group. I thank you in advance.
If you're using Drupal 6.x Form API and #type=>radios (http://api.drupal.org/api/function/theme_radios/6) each radio element will have its unique id which you can use to apply proper CSS.
The example you provided
$form['base_location'] = array(
'#type' => 'radios',
'#title' => t('base location'),
'#default_value' => variable_get('search_type', 0),
'#options' => array(
'0'=>t('District'),
'1'=>t('MRT'),
'2'=>t('Address')),
'#description' => t('base location'),
);
should output markup like so:
<div id="base-location-0-wrapper" class="form-item">
<label for="base-location-0" class="option"><input type="radio" class="form-radio" value="0" name="base_location" id="base-location-0"> District</label>
</div>
<div id="base-location-1-wrapper" class="form-item">
<label for="base-location-1" class="option"><input type="radio" class="form-radio" value="1" name="base_location" id="base-location-1"> MRT</label>
</div>
<div id="base-location-2-wrapper" class="form-item">
<label for="base-location-2" class="option"><input type="radio" class="form-radio" value="2" name="base_location" id="base-location-2"> Address</label>
</div>
Apply the following CSS and you should be set.
#base-location-0-wrapper,
#base-location-1-wrapper,
#base-location-2-wrapper {
display:inline;
margin-left:50px;
}
The answer is to use CSS with the html that you already have. It looks like you just want to change the radio buttons' display to 'inline' with 10px margins, which you can do. (And if you did break the radios up into separate elements within a fieldset, they'd no longer interact with each other.)