Zend Form Decorators for theme - php

I got a new theme for which i need to edit my decorators.
Most of it i got working except for the checkbox and radio.
<!-- Previously -->
<div class="form-group ">
<label for="mobileTheme" class="col-lg-2 control-label optional">Mobiel thema</label>
<div class="col-lg-5">
<div class="checkbox">
<input type="hidden" name="mobileTheme" value="0" />
<input type="checkbox" name="mobileTheme" id="mobileTheme" value="1" class=" " />
</div>
<span class="help-block">Redirect mobiele gebruikers naar een eigen domein met eigen thema</span>
</div>
</div>
<!-- After -->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" value="">
<i class="input-helper"></i>
Remember me
</label>
</div>
</div>
</div>
My decorator looks like this now:
'checkbox' => array(
'decorators' => array(
'ViewHelper',
array(array('input' => 'HtmlTag'), array('tag' => 'div', 'class' => 'checkbox')),
array('Errors', array('class' => 'help-inline')),
array('Description', array('tag' => 'span', 'class' => 'help-block')),
array('Label', array('class' => 'col-lg-2 control-label')),
array('HtmlTag', array('tag' => 'div', 'class' => 'col-lg-5')),
'ElementWrapper'
),
'options' => array(
'class' => '',
),
),
The issue i'm struggling with is that i don't know how to order the decorators right so that label is now within the col-*-* class

I got it working,
'checkbox' => array(
'decorators' => array(
'ViewHelper',
array('AdditionalElement', array('placement' => 'APPEND', 'tag' => 'i', 'class' => 'input-helper')),
array(array('input' => 'HtmlTag'), array('tag' => 'div', 'class' => 'checkbox')),
array('Errors', array('class' => 'help-inline')),
array('Description', array('tag' => 'span', 'class' => 'help-block')),
array('HtmlTag', array('tag' => 'div', 'class' => 'col-lg-5')),
array('Label', array('class' => 'col-lg-2 control-label')),
'ElementWrapper'
),
'options' => array(
'class' => '',
),
),
AdditionalElement is my own custom class for implementing any piece of html.

Related

Difficult decorators for file input

I need this markup for file input:
<label class="col-sm-12">File upload</label>
<div class="col-sm-12">
<div class="fileinput fileinput-new input-group" data-provides="fileinput">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i>
<span class="fileinput-filename"></span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new">Select file</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="...">
</span>
<a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a
</div>
<ul class="errors">
<li>Some error</li>
</ul>
</div>
I tried it like that:
<label class="col-sm-12">Attachment</label>
<div class="col-sm-12">
<?php echo $this->form->attachment; ?>
</div>
With very difficult decorator:
$this->fileDecorator = array(
array(
array('divOpen' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-control', 'data-trigger' => 'fileinput', 'openOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
),
array(
array('i' => 'HtmlTag'), array('tag' => 'i', 'class' => 'glyphicon glyphicon-file fileinput-exists', 'placement' => Zend_Form_Decorator_Abstract::APPEND)
),
array(
array('span' => 'HtmlTag'), array('tag' => 'span', 'class' => 'fileinput-filename', 'placement' => Zend_Form_Decorator_Abstract::APPEND)
),
array(
array('divClose' => 'HtmlTag'), array('tag' => 'div', 'closeOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
),
array(
array('spanOpen' => 'HtmlTag'), array('tag' => 'span', 'class' => 'input-group-addon btn btn-default btn-file', 'openOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
),
array(
'Callback',
array('callback' =>
function($content, $element, $options) {
return "<span class=\"{$options['class']}\">{$options['text']}</span><span class=\"{$options['class2']}\">{$options['text2']}</span>";
},
'class' => 'fileinput-new',
'text' => $this->translator->_('_selectFile'),
'class2' => 'fileinput-exists',
'text2' => $this->translator->_('_change')
)
),
'File',
array(
array('spanClose' => 'HtmlTag'), array('tag' => 'span', 'closeOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
),
array(
'Callback',
array('callback' =>
function($content, $element, $options) {
return "{$options['text']}";
},
'class' => 'input-group-addon btn btn-default fileinput-exists',
'text' => $this->translator->_('_remove'),
'data-dismiss' => 'fileinput'
)
),
array(
array('div' => 'HtmlTag'), array('tag' => 'div', 'class' => 'fileinput fileinput-new input-group', 'data-provides' => 'fileinput')
),
'Errors'
);
But problem is, that Callback can be only once in decorator (not like HtmlTag). And tag with content is not possible add without callback. It can be solved with more callbacks or differently?
Edit:
I have idea. Is possible to add File decoratror to Callback decorator?
It is simple like that:
<div class="form-group<?php echo count($this->form->attachment->getErrors()) ? ' has-error has-feedback' : null; ?>">
<label class="col-sm-12"><?php echo $this->form->attachment->renderLabel(); ?></label>
<div class="col-sm-12">
<div class="fileinput fileinput-new input-group" data-provides="fileinput">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i>
<span class="fileinput-filename"></span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new">Select file</span>
<span class="fileinput-exists">Change</span>
<?php echo $this->form->attachment->renderFile(); ?>
</span>
Remove
</div>
<?php echo $this->formErrors($this->form->attachment->getMessages()); ?>
</div>
</div>

Add extra element using zend form

Here is my code
private $elementDecorators = array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
);
public function init()
{
$username = new Zend_Form_Element_Text('username',array(
'decorators' =>$this->elementDecorators,
'label' =>'Username',
'required' =>true,
'span' =>array('class'=>'validation','id'=>'unameInfo'),
));
}
$this->addElements(array(
$username
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag',
array('tag'=>'table', 'width' => '100%')
),
'Form'
));
Form created for above code is as below
<tr>
<td id="username-label"><label for="username" class="blue-color required">Username</label></td>
<td><input type="text" name="username" id="username" value="" span="Array"></td>
</tr>
I want following html
<tr>
<td id="username-label"><label for="username" class="blue-color required">Username</label></td>
<td>
<input type="text" name="username" id="username" value="" span="Array">
<span class="validation" id="userinfo"></span>
</td>
</tr>
How can i add span tag in my above zend form code?
Thank you in advance
You can use the AnyMarkup decorator.
$username = new Zend_Form_Element_Text('username',array(
'decorators' => array(
'ViewHelper',
array('AnyMarkup', array('markup' => 'your-markup-here', 'placement' => 'append')),
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td','class'=>'blue-color','placement'=>'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
),
'label' => 'Username',
'required' => true,
'span' => array('class'=>'validation','id'=>'unameInfo'),
// actually, this last 'span' entry strikes me as odd
));
To add the decorator to an element using the short-form (as above) rather than creating an instance, you need to register the decorator's path/prefix with the element, something like:
$username->addPrefixPath('My_Decorator_', APPLICATION_PATH . '/../library/My/Decorator', Zend_Form_Element::DECORATOR);
You can add that prefix/path to all (currently defined) elements using the:
$form->addElementPrefixPath($prefix, $path)
method.
Try like below,
...
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Submit Button',
'class' => 'form-submit',
'decorators' => array(
'ViewHelper',
),
));
//$submit->removeDecorator('Label');
$this->addElement($submit);
$reset = new Zend_Form_Element_Reset('reset', array(
'label' => 'Reset Button',
'class' => 'form-reset',
'decorators' => array(
'ViewHelper',
),
));
//$submit->removeDecorator('Label');
$this->addElement($reset);
$this->addDisplayGroup(array('submit', 'reset',), 'submitButtons', array(
'order' => 10,
'decorators' => array(
'FormElements',
array(
array('data' => 'HtmlTag'),
array('tag' => 'td','class'=>'move_td')
),
array(
array('row' => 'HtmlTag', 'class' => 'element'),
array('tag' => 'tr')
)
),
));
...
This will generate code like below,
...
<td class="move_td">
<input type="submit" class="form-submit" value="Submit Button" id="submit" name="submit">
<input type="reset" class="form-reset" value="Reset Button" id="reset" name="reset">
</td>
...
Updated
Use below code to generate span
$this->addElement(
'hidden',
'dummy',
array(
'required' => false,
'ignore' => true,
'autoInsertNotEmptyValidator' => false,
'decorators' => array(
array(
'HtmlTag', array(
'tag' => 'span',
'id' => 'span-id',
'class' => 'span-class'
)
)
)
)
);
$this->dummy->clearValidators();
Now you need to use addDisplayGroup function to group html elements.

Using CakePHP FormHelper with Bootstrap Forms

CakePHP's FormHelper is how you generate forms when making CakePHP applications. As one might assume, this includes generating input elements, like so:
$this->Form->input('abc');
Which will produce HTML something like this:
<div class="input text">
<label for="ModelAbc">Abc</label>
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
Now, sadly, Bootstrap wants something like the following:
<div class="control-group">
<label for="ModelAbc" class="control-label">Abc</label>
<div class="controls">
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
</div>
How do I make CakePHP produce this output?
Inspired by lericson's answer, this is my final solution for CakePHP 2.x:
<?php echo $this->Form->create('ModelName', array(
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
)));?>
<fieldset>
<?php echo $this->Form->input('Fieldname', array(
'label' => array('class' => 'control-label'), // the preset in Form->create() doesn't work for me
)); ?>
</fieldset>
<?php echo $this->Form->end();?>
Which produces:
<form...>
<fieldset>
<div class="control-group required error">
<label for="Fieldname" class="control-label">Fieldname</label>
<div class="controls">
<input name="data[Fieldname]" class="form-error" maxlength="255" type="text" value="" id="Fieldname"/>
<span class="help-inline">Error message</span>
</div>
</div>
</fieldset>
</form>
I basically added the 'format' and 'error' keys, and added the control-label class to the label element.
Here's a solution for Bootstrap 3
<?php echo $this->Form->create('User', array(
'class' => 'form-horizontal',
'role' => 'form',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'form-group'),
'class' => array('form-control'),
'label' => array('class' => 'col-lg-2 control-label'),
'between' => '<div class="col-lg-3">',
'after' => '</div>',
'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
))); ?>
<fieldset>
<legend><?php echo __('Username and password'); ?></legend>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
In case a field needs its own label:
<?php echo $this->Form->input('username', array('label' => array('text' => 'Your username', 'class' => 'col-lg-2 control-label'))); ?>
Here's one way:
<?php echo $this->Form->create(null, array(
'inputDefaults' => array(
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'class' => '')
)); ?>
Your answer is correct, but for the benefit of other users there's some other tweaks you can do to take advantage of the error/help text:
Add form-horizontal to class in the Form->create() for more compact forms (labels on the left of the input, rather than on top)
Here's how to put help text underneath a field (has to be done for each field), not forgetting to close the </div>.
echo $this->Form->input('field_name', array(
'after'=>'<span class="help-block">This text appears
underneath the input.</span></div>'));
and to correctly display errors:
// cake 2.0
echo $this->Form->input('abc', array(
'error' => array('attributes' => array('class' => 'controls help-block'))
));
Outputs:
<div class="control-group required error">
<label for="ModelAbc" class="control-label">Abc</label>
<div class="controls">
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
<!-- error message -->
<div class="controls help-block">This is the error validation message.</div>
<!-- error message -->
</div>
It's extra mark-up and not as neat as bootstrap but it's a quick fix. The alternative is to do each error message individually.
and it lines up nicely. I haven't discovered an easy way to make use of inline messages yet however.
Applying the same principle as above to the form->end function as follows:
<?php echo $this->Form->end(array(
'label' => __('Submit'),
'class' => 'btn',
'div' => array(
'class' => 'control-group',
),
'before' => '<div class="controls">',
'after' => '</div>'
));?>
To produce:
<div class="control-group">
<div class="controls">
<input class="btn" type="submit" value="Submit">
</div>
</div>
small add for another comments:
if you whant add class and change label base text, you can write next
<?php echo $this->Form->input('Fieldname', array(
'label' => array('class' => 'control-label','text'=>'HERE YOU LABEL TEXT')
)); ?>
I had the same problem using slywalker / cakephp-plugin-boost_cake, I open a ticket and he had it fix in a few hours, he updated to 1,03 and told me to use it like this
<?php echo $this->Form->input('email', array(
'label' => array(
'text' => __('Email:'),
),
'beforeInput' => '<div class="input-append">',
'afterInput' => '<span class="add-on"><i class="icon-envelope"></i></span></div>'
)); ?>
I hope it helps some one else too
To get it working with a horizontal form in bootstrap with bootswatch I had to use:
echo $this->Form->create(
'User',
array(
'action' => 'add',
'admin' => 'false',
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array( 'before', 'label', 'between',
'input', 'error', 'after' ),
'class' => 'form-control',
'div' => array( 'class' => 'form-group' ),
'label' => array( 'class' => 'col-lg-2 control-label' ),
'between' => '<div class="col-lg-10">',
'after' => '</div>',
'error' => array( 'attributes' => array( 'wrap' => 'span',
'class' => 'text-danger' ) ),
)
)
);
Then you can just use it as normal:
echo $this->Form->input( 'User.username' );
Luc Franken posted this link in his comment: http://github.com/slywalker/cakephp-plugin-boost_cake
It took me a while to notice it, so for those who are still looking for the simplest solution:
Simply add the CakePHP Bootstrap plugin from GitHub and let the helper do the job for you!

How to set decorators in zend form?

I am using zend. I am trying to set decorators for the following div structure ,
<div class="fm">
<div class="fm_text">
<div class="p_details">Debit Weight:</div>
</div>
<div class="fm_text">
<div class="txtbox">
<input type="text" name="pname" class="txtcolor">
</div>
</div>
<div class="fm_text">
<div class="p_details">Credit Weight:</div>
</div>
<div class="fm_text">
<div class="txtbox">
<input type="text" name="pname" class="txtcolor">
</div>
</div>
</div>
<div class="fm">
......
</div>
It is difficult to set decorator for above div structure.I tried with following code ,
public $requirednewElementDecorators = array(
'ViewHelper',
'Errors',
array('Description', array('escape' => false, 'tag' => 'span', 'class' => '', 'placement' => 'prepend')),
array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'fm_text')),
array('Label', array('tag' => 'div', 'class' => 'p_details')
),
array('HtmlTag', array('tag' => 'div', 'class' => 'fm'))
);
I have to use zend form so that i can validate form effectively. I can use the HTML in template and continue the flow. but i dont want to do that.Kindly help me.
Two input-fields togehter? You can try it with a fieldset for the <div class="fm">...</div>. So, you must use the fieldset decorator. Why do you not want to use the <label class="p_details">DebitWeight</label> instead of <div class="p_details">Debit Weight:</div>?
This article is very helpful, maybe: http://devzone.zend.com/article/3450-Decorators-with-Zend_Form

How to wrap Zend_Form error message in custom html?

I need to wrap zend form error messages in custom html.
<div class="cerror" id="ID-error">
<div class="ui-widget">
<div class="ui-state-error ui-corner-all" id="IDerror-msg">
%ZEND_FORM_ERROR_MESSAGE%
</div>
</div>
</div>
Now I get errors in format:
<ul>
<li>Error message</li>
</ul>
I need:
<div class="cerror" id="EMAIL-error">
<div class="ui-widget">
<div class="ui-state-error ui-corner-all" id="EMAIL-error-msg">
<ul>
<li>Error message</li>
</ul>
</div>
</div>
</div>
Thank you!
I have following code:
$element->clearDecorators();
$element->removeDecorator('DtDdWrapper');
$element->addDecorator('ViewHelper');
$element->addDecorator('Description', array('tag' => 'p', 'class' => 'description'));
$element->addDecorator('Label', array('tag' => null));
$element->addDecorator(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-line'));
How to wrap errors in 3 div tags? Thank!
$element->clearDecorators();
$element->addDecorator('Errors');
$element->addDecorator(array('div1' => 'HtmlTag'), array('tag' => 'div',
'class' => 'cerror', 'id' => 'EMAIL-error'));
$element->addDecorator(array('div2' => 'HtmlTag'), array('tag' => 'div',
'class' => 'ui-widget'));
$element->addDecorator(array('div3' => 'HtmlTag'), array('tag' => 'div',
'class' => 'ui-state-error ui-corner-all',
'id' => 'EMAIL-error-msg'));
$element->addDecorator('ViewHelper');
$element->addDecorator('Description', array('tag' => 'p', 'class' => 'description'));
$element->addDecorator('Label', array('tag' => null));
$element->addDecorator(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-line'));

Categories