How to add name as show[xyz], show[abc], etc for multiple checkboxes in zend form - zend-framework

Hii... I want multiple checkboxes to be displayed as and so on...name="show[adgroups]", how can i do this using zend form code?? Please see below example how I wanted my output to be viewed:
Instructions
<div class="fieldgrp">
<label for="show_adgroups">Campaign/Ad-groups</label>
<div class="field"><input type="checkbox" name="show[adgroups]" id="show_adgroups" class="" value="adgroups" checked="checked" /></div>
</div>
<div class="fieldgrp">
<label for="show_keywords">Keywords</label>
<div class="field"><input type="checkbox" name="show[keywords]" id="show_keywords" class="" value="keywords" checked="checked" /></div>
</div>

Ignoring the "why", you can achieve what you want using a subform.
Simply name the subform "show" and add your "adgroups" and "keywords" checkboxes to it.
$form = new Zend_Form;
$show = new Zend_Form_SubForm();
$show->addElement('checkbox', 'adgroups', array(
'label' => 'Campaign/Ad-groups',
'checked' => true
));
$show->addElement('checkbox', 'keywords', array(
'label' => 'Keywords',
'checked' => true
));
$form->addSubForm($show, 'show');

Related

Yii2 form checkbox template

I set the form parameters:
<?
$form = ActiveForm::begin([
'id' => 'activeForm',
'action' => 'javascript://',
]);
$checkboxTemplate = '<div class="checkbox">{labelTitle}{beginLabel}{input}<span class="slider round"></span>{endLabel}{error}{hint}</div>';
echo $form->field($aclForm, tbl_RbacActions::IS_DEVELOPMENT)
->checkbox([
'labelOptions' => ['class' => 'switch'],
'template' => $checkboxTemplate
]);
?>
As a result, it still turns a standard form with standart classes:
<form id="Index-form" class="row col-12 no-gutters" action="javascript://" method="post">
<input type="hidden" name="_csrf-frontend" value="Lo7lVHTJ9wcN5rdfjK-b7AgW8L4OHEaqI9IsVofZPOl3yKkFBJqAQz-i-y7H3-mdMUmY-H1RcMRBhkU01-F2oA==">
<div class="form-group field-aclform-is_development required">
<input type="hidden" name="AclForm[is_development]" value="0">
<label class="switch">
<input type="checkbox" id="aclform-is_development" name="AclForm[is_development]" value="1" template="{input}{beginLabel}{labelTitle}{endLabel}{error}"> Is Development</label>
<div class="help-block"></div>
</div>
</form>
Why is it adding the template as the input attribute?
You haven't provided the HTML template that you are trying to follow or integrate here in the checkbox, but regarding the template being added as the attribute of the input field is quite logical as you are passing it as the option of the checkbox rather than the field option.
You need to provide the template as an array to the third parameter option of the field see below
<?php
$form = ActiveForm::begin(
[
'id' => 'activeForm',
'action' => 'javascript://'
]
);
$checkboxTemplate = '<div class="checkbox">{labelTitle}{beginLabel}{input}<span class="slider round"></span>{endLabel}{error}{hint}</div>';
echo $form->field(
$aclForm,
tbl_RbacActions::IS_DEVELOPMENT,
[
'template' => $checkboxTemplate
]
)->checkbox(
[
'labelOptions' => ['class' => 'switch']
]
);

Laravel 4 Preview Form Submit

Any idea how to solve the following approach.
I have a form and want to display the entered data in a specific formtemplate before store it in the DB. If the entered data looks properly, the user can save the form.So I am searching for a way to display the entered data as a preview in a new window/tab first. With my code below I am not able to preview the form without saving the data in the databse. Also display the preview in a new window or tab is not possible. I guess there is no way to achieve this with php / laravel. I tried some onlick events, but no luck. As it seems like the route is prefered.
Any idea how to solve this?
My form looks like:
{{ Form::open(array('url' => 'backend/menubuilder/'.$id, 'method' => 'PUT'))}}
<section>
<div class="container">
<div class="row">
<div class="inputBox">
<div class="col-xs-12 col-md-6">
<h3>Montag</h3>
<div class="form-group">
{{Form::label('gericht_1_mo','Gericht 1')}}
{{Form::textarea('gericht_1_mo', Auth::user()->gericht_1_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_1_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_1_mo', Auth::user()->preis_1_mo, array('class' => 'col-md-6'))}}
</div>
<div class="form-group mrgT55">
{{Form::label('gericht_2_mo','Gericht 2')}}
{{Form::textarea('gericht_2_mo', Auth::user()->gericht_2_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_2_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_2_mo', Auth::user()->preis_2_mo, array('class' => 'col-md-6'))}}
</div>
</div>
</div>
</div>
</div>
</div>
{{Form::submit('update')}}
{{-- <input type="submit" name="preview" value="preview"> --}}
{{Form::close()}}
{{ Form::open(array('url' => 'backend/menubuilder/templatesview/'.$id, 'method' => 'POST'))}}
{{-- {{Form::submit('Preview',array('onClick' => 'target_blank'))}} --}}
<input onclick="newTab()" type="submit" name="preview" value="preview" >
{{Form::close()}}
My routes:
Route::get('backend/menubuilder/templates/{id}', 'MenuBuilderController#template');
Route::post('backend/menubuilder/templatesview/{id}', 'MenuBuilderController#preview');
My Controller:
public function preview($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templatesview/tempone')->withUser($user)->withOwner($owner);
}
public function template($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templates/tempone')->withUser($user)->withOwner($owner);
}

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.