Html coders + zend form - zend-framework

So I use a zend framework, but for all commercial projects I am not using zend form component.
HTML+Js coders are working on frontend and I don't want to have nothing with it, I just connect the final view in my zf based application.
So for example I have in view a html form which looks like this:
http://pastie.org/1668143
So form design, elements and classes can be changed by frontend coders, and I want to know is there easy way for me, to use zend form with this concept (in each project form code looks different of course )?
My main problem here is code duplication in my controllers, which in this case looks something like this:
http://pastie.org/1668023 (please don't take exceptions and loggedMember seriously, they are just used for this snippet, to demonstrate a problem, which I have)
So, what would be the best solution for problem which I have, what do u think :) ?

If you have absolutely no control over the form's html structure, but still want to maximize the use of Zend_Form's features, use Zend_Form_Decorator_ViewScript.
http://framework.zend.com/manual/en/zend.form.standardDecorators.html (last section)
$element->setDecorators(array(array('ViewScript', array(
'viewScript' => '_element.phtml',
'class' => 'form element'
))));
I would do it like this:
create a form class that has all elements, validators and filters
create an instance of the form in your action and set the view script(s) (this way you can change them per controller and still have very little duplicated definition code.

Splendid, I don't understand why you would have a problem with code duplication, in your 2nd link, you are performing your checks, then check if the page is a post request, then performing the checks again, yes its duplicated, but I don't understand what you are trying to explain by doing this?
As for the form, its up to you how you use it, you could create the form object, then instead of ever out putting the form, simply pass it the data from your designers form, and use it to validate things.
Or you could use custom templates for the form, OK it means you don't give the designers quite as much freedom of them designing a form and you sorting the results, but they can still do their best at it.
This is the setup I use, after all I am in charge of the functionality as the programmer, the designers just make it look good what the user see's.
So for example, if I want to create an input element I can:
$arrival_time = $this->createElement('text', 'arrival_time', array(
'required' => true,
'label' => 'Arrival Time:',
));
$arrival_time->removeDecorator('HtmlTag');
$this->addElement($arrival_time);
Notice I have removed the HtmlTag decorators here - I don't need them for the markup as the designers will be arranging things for me.
Next thing to do is tell the form to use the layout the designers have made:
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'NewArrivalsForm.phtml'))));
Here my template is within the view's, script's directory.
Now the designers have a few options. They could do:
<table border="0" width="100%">
<tr>
<td>
<?php echo $this->element->arrival_time; ?>
</td>
This will give you the following output:
<td>
<dt id="arrival_time"><label for="arrival_time" class="required">Arrival Time:</label></dt>
<input type="text" name="arrival_time" id="arrival_time" value="" />
</td>
If there we're an error, that would be presented as well. You could remove the decorators 'Label', 'Description' & 'Errors' as well, to make it simply an input box:
<td>
<input type="text" name="arrival_time" id="arrival_time" value="" />
</td>
Even once you have removed the decorators, the designers could still use for example:
<tr>
<td>
<?php echo $this->element->time_on_site->getLabel(); ?>
</td>
</tr>
<tr>
<td>
<?php echo $this->element->time_on_site ?>
</td>
This will allow them to lay the form out exactly as they want to. But it will still allow you to use the full power of Zend_Form for your final validation checks. Take a look inside the Zend/form/element.php for all the methods you and your designers can use on the form element.

Related

Zend Frameword 2: How to set a html link (img) in a legend of the fieldset?

I used zf2 to design a website.
And the form is something like this:
$this->add(array
'options'=>array(
'label'=> 'title1'))
And finally it shows like this:
<form>
<fieldset>
<legend>title1</legend>
<label>****</label>
</fielset>
</form>
Now, I wanna add a link or an image after the title1, for example:
<form>
<fieldset>
<legend>title1<a href=''>link</a></legend>
<label>****</label>
</fielset>
</form>
How can I do this?
You can't. Well, at least not without overwriting the specific ViewHelper (probably formCollection()). In ZF2 all Labels are run through the Zend\View\Helper\EscapeHtml ViewHelper. Therefore using any sort of HTML inside Labels is not supported in any way.
While going by specification it may be allowed to use inline-elements inside the <legend> Element, semantically it looks a little different. The <legend> shall do nothing but to describe the contents of the <fieldset>.
Anyways, opinions aside, as i've mentioned, you'll have to overwrite the ViewHelper and then skip the use of the EscapeHtml ViewHelper, as it's done in this line of the formCollection() Code

Trouble pinpointing child elements while using Mojo::DOM

I'm trying to extract text from an old vBulletin forum using WWW::Mechanize and Mojo::DOM.
vBulletin doesn't use HTML and CSS for semantic markup, and I'm having trouble using Mojo::DOM->children to get at certain elements.
These vBulletin posts are structured differently depending on their content.
Single message:
<div id="postid_12345">The quick brown fox jumps over the lazy dog.<div>
Single message quoting another user:
<div id="postid_12345">
<div>
<table>
<tr>
<td>
<div>Quote originally posted by Bob</div>
<div>Everyone knows the sky is blue.</div>
</td>
</tr>
</table>
</div>
I disagree with you, Bob. It's obviously green.
</div>
Single message with spoilers:
<div id="postid_12345">
<div class="spoiler">Yoda is Luke's father!</div>
</div>
Single message quoting another user, with spoilers:
<div id="postid_12345">
<div>
<table>
<tr>
<td>
<div>Quote originally posted by Fred</div>
<div class="spoiler">Yoda is Luke's father!</div>
</td>
</tr>
</table>
</div>
<div class="spoiler">No waaaaay!</div>
</div>
Assuming the above HTML and an array packed with the necessary post IDs:
for (#post_ids) {
$mech->get($full_url_of_specific_forum_post);
my $dom = Mojo::DOM->new($mech->content);
my $div_id = 'postid_' . $_;
say $dom->at($div_id)->children('div')->first;
say $dom->at($div_id)->text;
}
Using $dom->at($div_id)->all_text gives me everything in an unbroken line, which makes it difficult to tell what's quoted and what's original in the post.
Using $dom->at($div_id)->text skips all of the child elements, so quoted text and spoilers are not picked up.
I've tried variations of $dom->at($div_id)->children('div')->first, but this gives me everything, including the HTML.
Ideally, I'd like to be able to pick up all the text in each post, with each child element on its own line, e.g.
POSTID12345:
+ Quote originally posted by Bob
+ Everyone knows the sky is blue.
I disagree with you, Bob. It's obviously green.
I'm new to Mojo and rusty with Perl. I wanted to solve this on my own, but after looking over the documentation and fiddling with it for a few hours, my brain is mush and I'm at a loss. I'm just not getting how Mojo::DOM and Mojo::Collections work.
Any help will be greatly appreciated.
Looking at the source of Mojo::DOM, basically the all_text method recursively walks the DOM and extracts all text. Use that source to write your own walking the DOM function. Its recursive function depends on returning a single string, in yours you might have it return an array with whatever context you need.
EDIT:
After some discussion on IRC, the web scraping example has been updated, it might help you guide you. http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Web_scraping
There is a module to flattern HTML tree, HTML::Linear.
The explanation of purpose for flatterning HTML tree is a bit long and boring, so here's a picture showing the output of the xpathify tool, bound with that module:
As you see, HTML tree nodes become single key/value list, where the key is the XPath for that node, and the value is the node's text attribute.
In a few keystrokes, this is how you use HTML::Linear:
#!/usr/bin/env perl
use strict;
use utf8;
use warnings;
use Data::Printer;
use HTML::Linear;
my $hl = HTML::Linear->new;
$hl->parse_file(q(vboard.html));
for my $el ($hl->as_list) {
my $hash = $el->as_hash;
next unless keys %{$hash};
p $hash;
}

Zend Framework: what is the difference between partials and placeholders

In Zend Framework, Can anybody explain the difference between partial and placeholder?
From my understanding, one can render a particular template / container by using both placeholders and partials.
In which condition should one use a partial and what scenario is optimal use for placeholders?
It's pretty simple, the placholder is used to persist data between views and layouts and partials are used in specific views to do specific tasks.
See these excerpts from the reference.
77.4.1.6. Placeholder Helper: The Placeholder view helper is used to persist content between view scripts and view instances. It also
offers some useful features such as aggregating content, capturing
view script content for later use, and adding pre- and post-text to
content (and custom separators for aggregated content).
77.4.1.5. Partial Helper: The Partial view helper is used to render a specified template within its own variable scope. The primary use is
for reusable template fragments with which you do not need to worry
about variable name clashes. Additionally, they allow you to specify
partial view scripts from specific modules.
Here is a simple example of a placeholder, that sounds like what you want. (to persist data)
<?php
//this is placed in my layout above the html and uses an action helper
//so that a specific action is called, you could also use view helpers or partials
$this->layout()->nav = $this->action('render', 'menu', null,
array('menu' => $this->mainMenuId))
?>
<div id="nav">
//Here the placeholder is called in the layout
<?php echo $this->layout()->nav ?>
</div>
in this case the menu id's are setup in the bootstrap, however this is not requiered it just simple.
protected function _initMenus() {
$view = $this->getResource('view');
$view->mainMenuId = 4;
$view->adminMenuId = 5;
}
[EDIT]
I think a better placeholder example might be in order. This place holder is a small search form I use in several actions in several controllers in different configurations.
In this configuration this form is setup to search just for music artists, the controllers that use this placeholder will have different paths for setAction(), different Labels and sometimes different placeholder text. I use this same form to search music and video databases.
I you always use the same setup or have more interest in doing it then I do, this can be setup as a plugin.
//in the controller
public function preDispatch() {
//add form
$searchForm = new Application_Form_Search();
$searchForm->setAction('/admin/music/update');
$searchForm->query->setAttribs(array('placeholder' => 'Search for Artist',
'size' => 27,
));
$searchForm->search->setLabel('Find an Artist\'s work.');
$searchForm->setDecorators(array(
array('ViewScript', array(
'viewScript' => '_searchForm.phtml'
))
));
//assign form to placeholder
$this->_helper->layout()->search = $searchForm;
}
I use the placeholder in my layout (can also be used in any view script). The search form is rendered when the placeholder has a value and not rendered when the placeholder has no value.
//in the layout.phtml
<?php echo $this->layout()->search ?>
and just to be complete, here is the partial that the form uses as a viewscript decorator.
<article class="search">
<form action="<?php echo $this->element->getAction() ?>"
method="<?php echo $this->element->getMethod() ?>">
<table>
<tr>
<th><?php echo $this->element->query->renderLabel() ?></th>
</tr>
<tr>
<td><?php echo $this->element->query->renderViewHelper() ?></td>
</tr>
<tr>
<td><?php echo $this->element->search ?></td>
</tr>
</table>
</form>
</article>
This example should really illustrate the difference between partial and placeholder.
I think what might be more helpful for you here is either a custom view helper, possibly expanding on the existing Zend_View_Helper_FormSelect class or create a custom Zend Form element that suits your needs. Alternatively, a helper script that's in a general location may be the best bet.

customizing size of form fields created using field.custom.widget

I used the following code to generate a form in attached image.
Is it possible to change the size of the fields in the form.
I want to decrease size of input field of Estimated time and the dropbox field to the right of it
{{=form.custom.begin}}
<table>
<table><tr>
<td><b>Type :</b></td><td><div>{{=form.custom.widget.type}}</div></td>
</tr><tr>
<td><b>Title :</b></td><td><div>{{=form.custom.widget.title}}</div></td>
</tr><tr>
<td><b>Description :</b></td><td><div>{{=form.custom.widget.description}}</div></td>
</tr><tr>
<td><b>Estimated Time :</b></td><div'><td>{{=form.custom.widget.estimated_time}}{{=form.custom.widget.estimated_time_unit}}</td> </div>
</tr>
<tr>
<td></td><td><div align='center'>{{=form.custom.submit}}</div></td>
</tr>
</table>
{{=form.custom.end}}
Yes. You can and there are many ways.
The recommended way is to look at the generates JS. You will find it follows a naming convention described in the book. You can use CSS to change the look-and feel of every widget.
input[name=estimate_time] { width:50px }
Similarly you can use JS/jQuery (I would recommend you do this in the view).
jQuery(document).ready(function(){ jQuery('input[name=estimate_time]').css('width','50px');});
You can also use jQuery-like syntax in python in the controller:
form.element('input[name=estimate_time]')['_style']='width:50px'

porting template to zend framework

I got few issues proting a pear based form to zend form.
I have few elements I need :
Basic Elements
Groups
Group Elements
Sections
I previously used templates to render the forms on Pear. I obviously cannot use pre-existing zend decorators, since I need to specify css classes for each of the components of my base elements.
To see the issue I need to render this, which is the template for a basic element :
<li class = "{position_in_the_form} {error}">
<label class="{label_class}"> {label}
[<span class="required_class"> * </span>]
</label>
<div> {element_content} </div>
[<p class = "{error_class}"> {error_message} </p>]
</li>
So as you can see I have many dynamic things I would like to be able to specify : position in the form, class for the label, class for the required section, the class for the error.
I would also like to be able to specify this from an ini file. I manage to set up the basic meta from the ini but not custom fields.
One of the reason I cannot use basic decorators is that I need to have "error" in the "li" class when there is an error in the element or the sub_form.I'm not sure this is possible with the error decorator... (correct me if I'm wrong)
Also, for the group I need something handling the errors, and since the core groups don't handle errors I need to subclass the sub_form. But how can I create a subform in an ini file and I don't know how to provide parameters to the sub form fromn the ini.
The main idea here is to be able to have visual and logic groups of elements in a form. For example I need a 'name' group with fullname, middle name, etc. This also implies a global validator for this "name" group.
An other thing is that I want to be able to position these groups : left half, right half, full
I got the css ready for this and working with pear.
So what I need is a simple solution, with few code and ini configurations. Unfortunately I think I got stuck in something too complicated, so if someone has any idea about a simple architecture it would be amazing!
Thanks in advance for your help,
Best, Boris
In your complex decoration need, you might want to use the ViewScript Zend_Form_Element_Decorator
$element->setDecorators(array(
array('ViewScript', array('viewScript' => 'path/to/your/views/element.phtml')),
));
and then in path/to/your/views/element.phtml, more or less something like
<li class="<?php echo $this->element->getAttrib('position_in_the_form') ?> <?php echo $this->element->hasErrors() ? 'error' : '' ?>">
<label class="<?php echo $this->element->getAttrib('label_class') ?>">
<?php echo $this->formLabel($this->element->getName(),
$this->element->getLabel()) ?>
<? if ( $this->element->isRequired() ) { ?>
[<span class="required_class"> * </span>]
<? } ?>
</label>
<div> <?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?> </div>
<? if ( $this->element->hasErrors() ) { ?>
[<p class="<?php echo $this->element->getAttrib('error_class') ?>"> <?php echo $this->formErrors($this->element->getMessages()) ?> </p>]
<? } ?>
</li>
This is only a drafty snippet of code, but should lead you in the direction you aim.
Regards