Place an image for photo form button in Zend form - forms

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

Related

wp shortcode breaks get_post_thumbnail

I'm having some trouble with short code's breaking a featured image I have set on my home page in Wordpress (4.0). For some reason, when I paste in my shortcode in the wp-admin > page > edit > content for the home page, suddenly my featured image stops working. It's working until I paste in the shortcode and update the page, but then the image disappears and the_post_thumbnail() returns false. I've also tried get_the_post_thumbnail() without success.
Here's my code excerpt from "front-page.php":
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php if (has_post_thumbnail()) {the_post_thumbnail();} ?>
</div>
And here's the shortcode function from "functions.php":
// [random_testimonial]
function short_code_get_random_testimonial(){
$args = array( 'post_type' => 'testimonial', 'posts_per_page' => 1, 'orderby' => 'rand' );
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$random_testimonial = get_the_content();
$author = get_the_title();
$rt = '<p class="quote">' . $random_testimonial;
$rt .= '</p><p class="right">– ' . $author . '</p>';
endwhile;
return $rt;
}
// Register the shortcodes.
add_shortcode( 'random_testimonial', 'short_code_get_random_testimonial' );
// Allow text widgets to contain shortcodes.
add_filter('widget_text', 'do_shortcode');
add_filter('the_content', 'do_shortcode');
Any ideas would be greatly appreciated.
I think I may have solved this one. It seems to have something to do with the fact that I was calling a custom post type with the shortcode, so by the time the_post_thumbnail() was called, the query had changed to refer to the custom post type included in the shortcode ("testimonial") rather than the parent post (in this case the home page). I revised my code as follows to catch the image before the shortcode is called, and all is working again:
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<?php
// go ahead and get the post image in case [random_testimonial]
// shortcode is used in page content which will break the query.
if (has_post_thumbnail()) {$image = get_the_post_thumbnail();} ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php
// Echo post image from above.
if(isset($image)){echo $image;}
?>
</div>

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(); ?>

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;?>

Zend_Form_Element_File (no decorator found error)

I am creating a form with an upload element. I have created the file element with the code below
$image = new Zend_Form_Element_File('image');
$image->setAttrib('title','Listing Image:')
->addDecorator('File')
->addValidator('Size', false, 204800)
->addValidator('Extension', false, 'jpg,png,gif');
$this->addElement($image);
Which as far as I can tell works fine. It tries to add a file element to the page. I get an error which says " Warning: No file decorator found... unable to render file element in..."
As you can see I have set the decorator for the element to be the File decorator so I am unsure why I am getting this error?
It is worth noting I have removed all default decorators except for viewHelper and PrepareElements and I am using a view partial as the viewHelper to display the output. Just incase this is required I have also included that below.
<form method="<?php echo $this->element->getMethod(); ?>" action="<?php echo $this->element->getAction(); ?>">
<div class="boxmiddle-Holder">
<div class="boxmiddle-content">
<table border="0" cellspacing="2" cellpadding="3" class="dataTable">
<?php foreach($this->element->getElements() as $element): ?>
<!-- if not a button then display label -->
<tr>
<td><label for="<?php echo $element->getName(); ?>"><?php echo $element->getAttrib('title'); ?></label></td>
<td class="normal"><?php echo $element; ?></td>
<?php if($element->getMessages()) {
foreach($element->getMessages() as $message) { ?>
<td> <?php echo "<p class='elementError'>".$message."</p>"; ?></td>
<?php }
}
?>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
Any ideas?
I'd double check to make sure the File decorator isn't being cleared or reset later, that error indicates that your form's file element does not have a Zend_Form_Decorator_File decorator attached to it.
Look in Zend/Form/Element/File.php around line 859 for the function that throws this error. I suspect somehow the File decorator is being removed from the element. It could be that it has no decorators or is just lacking the File decorator.
Hope that helps.

Zend Framework Custom Forms with viewScript

I am having some problems working out how to use custom forms in Zend Framework.
I have followed various guides but none seem to work. Nothing at all gets rendered.
Here is the bits of code that I am trying to use (All code below is in the default module). I have simplified the code to a single input for the test.
applications/forms/One/Nametest.php
class Application_Form_One_Nametest extends Zend_Form {
public function init() {
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Box Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit Message');
$submit->setAttrib('id', 'submitbutton');
$submit->setAttrib('class', 'bluebutton');
$this->addElements(array($name, $submit));
}
}
application/views/scripts/one/formlayout.phtml
<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">
<p>
Please provide us the following information so we can know more about
you.
</p>
<? echo $this->element->name ?>
<? echo $this->element->submit ?>
</form>
application/controllers/IndexController.php
public function formtestAction() {
$form = new Application_Form_One_Nametest();
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));
$this->view->form = $form;
}
application/views/scripts/index/formtest.phtml
<h1>Formtest</h1>
<?
echo $this->form;
?>
The above code does not throw any errors or render any part of formlayout.phtml including the form tags or text between the p tags.
Can anybody tell me what I might be doing wrong?
I think the problem is your form element's decorator. You should set the decorator to ViewHelper and Error only. It works for me at least.
Here is the code I used and it should work
applications/forms/Form.php
class Application_Form_Form extends Zend_Form {
public function loadDefaultDecorators() {
$this->setDecorators(
array(
array(
'ViewScript',
array(
'viewScript' => 'index/formlayout.phtml',
)
)
)
);
}
public function init() {
$this->setAction('/action');
$this->setMethod('post');
$this->addElement('text', 'name', array(
'decorators' => array('ViewHelper', 'Errors')
));
}
}
application/views/scripts/index/formlayout.phtml
<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<div>
<label for="name">Box Name</label>
<?php echo $this->element->name; ?>
</div>
<input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>
application/views/scripts/index/index.phtml
<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>
application/controllers/IndexController.php
public function indexAction() {
$form = new Application_Form_Form();
$this -> view -> form = $form;
}
Here is a very simple example to get you going adapted from this article.
The form:-
class Application_Form_Test extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
$this->setAction('/');
$text = new Zend_Form_Element_Text('testText');
$submit = new Zend_Form_Element_Submit('submit');
$this->setDecorators(
array(
array('ViewScript', array('viewScript' => '_form_test.phtml'))
)
);
$this->addElements(array($text, $submit));
$this->setElementDecorators(array('ViewHelper'));
}
}
The order in which setDecorators(), addElements() and setElementDecorators() are called is very important here.
The view script _form_test.phtml can be called anything you like, but it needs to be in /views/scripts so that it can be found by the renderer.
/views/scripts/_form_test.phtml would look something like this:-
<form id="contact" action="<?php echo $this->element->getAction(); ?>"
method="<?php echo $this->element->getMethod(); ?>">
<p>
Text<br />
<?php echo $this->element->testText; ?>
</p>
<p>
<?php echo $this->element->submit ?>
</p>
</form>
You instantiate the form, pass it to the view and render it as usual. The output from this example looks like this:-
<form id='contact' action='/' method='post'>
<p>
Text<br />
<input type="text" name="testText" id="testText" value=""></p>
<p>
<input type="submit" name="submit" id="submit" value="submit"></p>
</form>
That should be enough to get you started creating your own forms.
Usually, if you don't see anything on the screen it means that some sort of error happened.
Maybe you have errors turned off or something, maybe not. I'm just trying to give you ideas.
The only things I could spot where the following.
In the below code, you have to still specify the form when trying to print out the elements.
<form>
action="<?php $this->escape($this->element->getAction()) ?>"
method="<?php $this->escape($this->element->getMethod()) ?>" >
<p>
Please provide us the following information so we can know more about
you.
</p>
<?php echo $this->element->getElement( 'name' ); ?>
<?php echo $this->element->getElement( 'submit' ) ?>
</form>
As vascowhite's code shows, once you are inside the viewscript, the variable with the form is called element. The viewscript decorator uses a partial to do the rendering and thus it creates its own scope within the viewscript with different variable names.
So, although in your original view it was called $form, in the viewscript you'll have to call it element.
Also, maybe it was copy/paste haste, but you used <? ?> tags instead of <?= ?> or <?php ?> tags. Maybe that caused some error that is beyond parsing and that's why you got no output.