CakePHP 2.0 Determine which submit button has been clicked - forms

In CakePHP 1.3 you can create a form with multiple submit buttons:
echo $this->Form->submit('Submit 1', array('name'=>'submit');
echo $this->Form->submit('Submit 2', array('name'=>'submit');
and detect which submit button was pressed in the controller with:
if (isset($this->params['form']['submit']) && $this->params['form']['submit'] == "Submit 1") {
// first button clicked
}
In CakePHP, $this->params['form'] isn't set and the clicked button value doesn't appear anywhere in $this->request, $this->request->data, $this->params, $this->data or $_POST.
How do I determine which button has been clicked in CakePHP 2.0?
Thanks in advance.
Edit:
As requested, here's the code for the form:
<?php echo $this->Form->create('History', array('action'=>'add')); ?>
<div class='submit'>
<?php
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit'));
?>
</div>
<?php echo $this->Form->end()?>
And the output of the form:
<form action="/projects/kings_recruit/trunk/www/histories/add" id="HistoryAddForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input name="_method" value="POST" type="hidden">
</div>
<div class="submit">
<input name="submit" value="Yes" type="submit">
<input name="submit" value="No" type="submit">
</div>
</form>

Generally it is a bad practise to use the same name for both submit buttons.
There should be a "submit" key in the $_POST and $this->request->data
I tested this in CakePHP 2.1.1 as shown below:
The view code:
<?php echo $this->Form->create('Message', array('action'=>'test'));
// Extra test input field
echo $this->Form->input('test');
?>
<div class='submit'>
<?php
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit'));
?>
</div>
<?php echo $this->Form->end()?>
The in the controller in $this->request->data:
array(
'submit' => 'Yes',
'Message' => array(
'test' => 'TestFieldTest'
)
)
And in $_POST:
array(
'_method' => 'POST',
'data' => array(
'Message' => array(
'test' => 'TestFieldTest'
)
),
'submit' => 'Yes'
)
You can also give the two submits different names:
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submitY'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submitN'));
This way you can differ them in the $_POST or $this->request->data, because the keys will be the submits' names:
array(
'submitY' => 'Yes',
'Message' => array(
'test' => 'foo'
)
)
array(
'_method' => 'POST',
'data' => array(
'Message' => array(
'test' => 'Bar'
)
),
'submitY' => 'Yes'
)
Then to determine which button is pressed you can use a simple isset($_POST['']) or over $this->request->data ?

Don't use the same name for both submit buttons. Consider this example:
<?php echo $this->Form->create(false); ?>
<?php echo $this->Form->text('input'); ?>
<?php echo $this->Form->submit('Yes', array('name' => 'submit1')); ?>
<?php echo $this->Form->submit('No', array('name' => 'submit2')); ?>
<?php echo $this->Form->end(); ?>
debug($this->request->data) will produce the following when the "Yes" button is clicked:
array(
'submit1' => 'Yes',
'input' => 'test'
)
And here it is when the "No" button is clicked:
array(
'submit2' => 'No',
'input' => 'test'
)
To check which button was clicked:
if (isset($this->request->data['submit1'])) {
// yes button was clicked
} else if (isset($this->request->data['submit2'])) {
// no button was clicked
}

in 2.0 there is no $this->params['form'] anymore
all form helper posted fields end up in $this->data (which makes more sense anyway)
so
if (!empty($this->data['submit']) && $this->data['submit'] == "Submit 1") {}
note that !empty() is better here as well.
PS: you can use my enhanced upgrade shell to replace it in your code: https://github.com/dereuromark/upgrade
its the command
cake Upgrade.Upgrade request
(https://github.com/dereuromark/upgrade/blob/master/Console/Command/UpgradeShell.php#L833)

if (!empty($this->request->data['submit']) && $this->request->data['submit'] == "Yes") {
// do your stuff
}

Related

CodeIgniter Omnipay Przelewy24: Message: The email parameter is required

I try enable payment gateway Przelewy24 with Omnipay library for Codeigniter 3.
For this I install via composer both libraries.
Library:
https://github.com/mysiar-org/omnipay-przelewy24v1
and
https://github.com/thephpleague/omnipay
Then I create controller
Cart_contoller.php I add function
use Omnipay\Omnipay;
/**
* Payment with Przelewy24
*/
public function przelewy24_payment_post()
{
$przelewy24 = get_payment_gateway('przelewy24');
if (empty($przelewy24)) {
$this->session->set_flashdata('error', "Payment method not found!");
echo json_encode([
'result' => 0
]);
exit();
}
/** #var \Omnipay\Przelewy24\Gateway $gateway */
$gateway = Omnipay::create('Przelewy24');
$gateway->initialize([
'merchantId' => 'xxxx',
'posId' => 'xxxx',
'crc' => 'xxxxxxxxxxxx',
'testMode' => true,
]);
$params = array(
'sessionId' => 2327398739,
'amount' => 12.34,
'currency' => 'PLN',
'description' => 'Payment test',
'returnUrl' => 'www.xxxxxx',
'notifyUrl' => 'www.xxxxxxxxxxx',
'card' => array(
'email' => 'info#example.com',
'name' => 'My name',
'country' => 'PL',
),
);
$response = $gateway->purchase($params)->send();
if ($response->isSuccessful()) {
$response->redirect();
} else {
echo 'Failed';
}
}
in view page I add:
<?php echo form_open('cart_controller/przelewy24_payment_post'); ?>
<div class="payment-icons-container">
<label class="payment-icons">
<?php $logos = #explode(',', $payment_gateway->logos);
if (!empty($logos) && item_count($logos) > 0):
foreach ($logos as $logo): ?>
<img src="<?php echo base_url(); ?>assets/img/payment/<?= html_escape(trim($logo)); ?>.svg" alt="<?= html_escape(trim($logo)); ?>">
<?php endforeach;
endif; ?>
</label>
</div>
<button id="submit" class="btn btn-primary" id="card-button">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<?= trans("pay"); ?> <?= price_formatted($total_amount, $currency); ?>
</button>
<?php echo form_close(); ?><!-- form end -->
On button submit I get:
Type: Omnipay\Common\Exception\InvalidRequestException
Message: The email parameter is required
What I do wrong? Can anyone help me with this code how to correct integrate this gateway?

cakephp3- unable to post form data to controller function

I have a contact us form and user enter data and submits it. This data needs to accessed in controller function so I can send a mail to admin informing about the user request recently received.
But when I press submit button, nothing happens. Form just reloads and the contactus form (view page) is shown to the users. I don't know why the data is not getting passed to the controller function.
I'mm new to CakePHP framework and I'm using CakePHP3 for development purposes.
Here is my form:
<?php echo $this->Form->create(null, ['url' => ['controller' => 'Pages', 'action' => 'contactus']]); ?>
<div class="col-md-6">
<?php echo $this->Form->input('fname', ['placeholder' => 'Your name.' , 'id' => 'fname', 'required' => 'required']); ?>
</div>
<div class="col-md-6">
<?php echo $this->Form->input('mail', ['placeholder' => 'Your email.' , 'id' => 'mail', 'required' => 'required']); ?>
</div>
<div class="col-md-6">
<?php echo $this->Form->input('subject', ['placeholder' => 'Write something.', 'id' => 'subject']); ?>
</div>
<div class="col-md-9">
<?php echo $this->Form->button(__('Submit')); ?>
</div>
<?php echo $this->Form->end(); ?>
And my controller function is:
public function contactus()
{
$pages ='';
if ($this->request->is('post'))
{
$pages = $this->request->data('Contact');
}
$this->set(compact('pages'));
$this->set('_serialize', ['pages']);
}
Can anyone tell me the mistakes I made?
I think your form is submitting but not through the post method. So I would like to say you that, please make the bellow changes before submitting the form.
$pages ='';
if ($this->request->is('post'))
{
echo "Request is post";die;
$pages = $this->request->data('Contact');
}else{
echo "request is not post";die;
}
$this->set(compact('pages'));
$this->set('_serialize', ['pages']);
Now check, which is printing in the display. Then I can help you further.
Remember: - fill the form, then after change the controller method, then press submit method.

ZF2 Elements "select" and "multicheckbox" not render correctly

Why elements "select" and "multicheckbox" not render correctly (in html)?
Element "text" and "checkbox" render correctly!
Example:
1) Code in form:
class Profile extends Form {
public function __construct($name = null) {
parent::__construct('page');
$this->setAttribute('action', 'info');
$this->setAttribute('method', 'post');
$this->setInputFilter($this->getFilters());`
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'usernames',
'attributes' => array(
'id' => 'usernames'
),
'options' => array(
'label' => 'User Name',
'options' => array(
'test' => 'Hi, Im a test!',
'Foo' => 'Bar',
),
),
));
$this->add(array(
'type' => 'select',
'name' => 'language',
'options' => array(
'label' => 'Which is your mother tongue?',
'value' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
}
2) Code in view:
<h1><?php echo $this->translate('Profile') ?></h1>
<?php $form = $this->form; $form->prepare();?>
<?php echo $this->form()->openTag($form) ?>
<dl class="zend_form">
<?php foreach ($form as $element): ?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->translate($this->formLabel($element)); ?></dt>
<?php endif ?>
<?php if ($element instanceof Zend\Form\Element\Button): ?>
<dd><?php echo $this->formButton($element) ?></dd>
<?php elseif ($element instanceof Zend\Form\Element\Captcha): ?>
<dd><?php echo $this->formCaptcha($element) . $this->translate($this->formElementErrors($element)); ?></dd>
<?php else: ?>
<dd><?php echo $this->formInput($element) . $this->translate($this->formElementErrors($element)); ?></dd>
<?php endif ?>
<?php endforeach ?>
</dl>
<?php if ($this->redirect): ?>
<input type="hidden" name="redirect" value="<?php echo $this->redirect ?>" />
<?php endif ?>
<input type="submit" value="<?php echo $this->translate('Submit'); ?>" />
<?php echo $this->form()->closeTag() ?>
3) Code in html, after render:
...
<dt><label for="usernames">User Name</label></dt>
<dd><input type="select" name="usernames" id="usernames" value=""></dd>
<dt><label for="language">Which is your mother tongue?</label></dt>
<dd><input type="select" name="language" value=""></dd>
...
it is not correctly!?
...
<select>
<option>value 1</option>
<option>value 2</option>
</select>
... - that's correct)
The formInput() helper is specifically for outputting <input> elements, so ZF is doing exactly what you've told it to. You probably want something more like this:
<?php foreach ($form as $element): ?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->translate($this->formLabel($element)); ?></dt>
<?php endif ?>
<dd><?php echo $this->formElement($element) . $this->translate($this->formElementErrors($element)) ?></dd>
<?php endforeach ?>
The formElement() helper will call whatever helper is appropriate.

How to handle multiple forms in the same view?

I have a default form for my specific view.
Through an element I (dinamically) include another view (using view extension) in order to provide an upload form.
My problem is that the second form seems to submit the first one.
Default form
<div class="content-box-content">
<?php
echo $this->Form->create("WebSubject", array(
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'input-notification error png_bg'
)
)
)
));
?>
<?=$this->Form->input('id', array('type' => 'hidden'))?>
<?=$this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire'))?>
<?=$this->Form->input('description', array('type' => 'textarea', 'label' => 'Descriere', 'id' => 'description'))?>
<?=$this->Form->input('description_long', array('type' => 'textarea', 'label' => 'Continut', 'id' => 'description_long'))?>
<?=$this->Form->submit('Salveaza', array('class' => "button"))?>
</div>
This way I include the element
<div class="tab-content default-tab" id="fotoUploadTab">
<?php
echo $this->element('file_upload_form', array(
'view' => 'upload_admin',
'webFileType' => 'image',
'redirect' => $SHT['here']
)
);
?>
<div class="tab-content default-tab">
Lista imagini
</div>
</div>
Element code
<?php
$view = (isset($view)) ? $view : "upload_admin";
$webFileType = (isset($webFileType)) ? $webFileType : "image";
$redirect = (isset($redirect)) ? $redirect : "/";
?>
<?php
$this->extend("/WebFiles/".$view);
?>
Extended View code
<div class="tab-content default-tab">
<?php echo $this->Form->create("WebFile", array('action' => '/', 'type' => 'file')); ?>
<input type="hidden" name="redirect" value="" />
<?php echo $this->Form->input('entity_id', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('entity_table_name', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('type', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire')); ?>
<?php echo $this->Form->input('description', array('class' => "text-input small-input", 'label' => 'Descriere')); ?>
<?php echo $this->Form->submit('Upload', array('class' => 'button')); ?>
</div>
As seen in the last snippet, I tried to force the last form by providing an action url, but on submiting it, it sends data as the first one does.
How should I handle this ?
Thank you!
If you just want to have both forms, the one from parent and the other from the child view/element make sure you call $this->Form->end() in both templates and that you are not nesting a form inside the other. Probably, in your case, just by adding end() to both forms will solve your issue.
As a side note, you cannot have a parent view opening a Form with $this->Form->create() and inject fields into it using the child view, basically because you need create() to be called before any input is rendered and parent views are rendered after the child is executed.

How do I use ViewScripts on Zend_Form File Elements?

I am using this ViewScript for my standard form elements:
<div class="field" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())) : ?>
<?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value"><?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?></span>
<?php if (0 < $this->element->getMessages()->length) : ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())) : ?>
<span class="hint"><?php echo $this->element->getDescription(); ?></span>
<?php endif; ?>
</div>
Trying to use that ViewScript alone results in an error:
Exception caught by form: No file
decorator found... unable to render
file element
Looking at this FAQ revealed part of my problem, and I updated my form element decorators like this:
'decorators' => array(
array('File'),
array('ViewScript', array('viewScript' => 'form/field.phtml'))
)
Now it's rendering the file element twice, once within my view script, and extra elements with the file element outside my view script:
<input type="hidden" name="MAX_FILE_SIZE" value="8388608" id="MAX_FILE_SIZE" />
<input type="hidden" name="UPLOAD_IDENTIFIER" value="4b5f7335a55ee" id="progress_key" />
<input type="file" name="upload_file" id="upload_file" />
<div class="field" id="field_upload_file">
<label for="upload_file">Upload File</label>
<span class="value"><input type="file" name="upload_file" id="upload_file" /></span>
</div>
Any ideas on how to handle this properly with a ViewScript?
UPDATE: Based on Shaun's solution, here's my final code:
Form Element:
$this->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array('File', array('ViewScript', array(
'viewScript' => '_form/file.phtml',
'placement' => false,
))),
'label' => 'Upload',
'required' => false,
'filters' => array(),
'validators' => array(array('Count', false, 1),),
));
View Script:
<?php
$class .= 'field ' . strtolower(end(explode('_',$this->element->getType())));
if ($this->element->isRequired()) {
$class .= ' required';
}
if ($this->element->hasErrors()) {
$class .= ' errors';
}
?>
<div class="<?php echo $class; ?>" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())): ?>
<?php echo $this->formLabel($this->element->getFullyQualifiedName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value"><?php echo $this->content; ?></span>
<?php if ($this->element->hasErrors()): ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())): ?>
<p class="hint"><?php echo $this->element->getDescription(); ?></p>
<?php endif; ?>
</div>
The answer is relatively simple as it happens. All you need do is specify the File decorator first, create a specific view script for the file input and specify false for the placement in the viewScript decorator arguments, this will effectively inject the output from the File decorator into the viewScript decorator e.g.
$element->setDecorators(array('File', array('ViewScript', array('viewScript' => 'decorators/file.phtml', 'placement' => false))));
Then in the new file element view script you simply echo $this->content in the script where you'd like the file input markup to be placed. Here's an example from a recent project, so ignore the markup if it looks a little odd, hopefully it will illustrate the point.
<label for="<?php echo $this->element->getName(); ?>" class="element <?php if ($this->element->hasErrors()): ?> error<?php endif; ?>" id="label_<?php echo $this->element->getName(); ?>">
<span><?php echo $this->element->getLabel(); ?></span>
<?php echo $this->content; ?>
<?php if ($this->element->hasErrors()): ?>
<span class="error">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</span>
<?php endif; ?>
</label>
When rendered you will see this html for the element
<label for="photo" class="element" id="label_photo">
<span>Photo</span>
<input type="hidden" name="MAX_FILE_SIZE" value="6291456" id="MAX_FILE_SIZE">
<input type="file" name="photo" id="photo">
</label>
This is not a simple or ideal solution because it requires an extension of the File decorator... but it's rather frustrating that they didn't make the effort to separate the hidden element generation logic from the file input generation logic. I'm not sure if the file view helper handles the issue of an element being an array (that seems to be the reason they did it this way.)
Extension of File Decorator:
(the commented out part is what causes the extra input to be generated.)
<?php
class Sys_Form_Decorator_File extends Zend_Form_Decorator_File {
public function render($content) {
$element = $this->getElement();
if (!$element instanceof Zend_Form_Element) {return $content;}
$view = $element->getView();
if (!$view instanceof Zend_View_Interface) {return $content;}
$name = $element->getName();
$attribs = $this->getAttribs();
if (!array_key_exists('id', $attribs)) {$attribs['id'] = $name;}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$markup = array();
$size = $element->getMaxFileSize();
if ($size > 0) {
$element->setMaxFileSize(0);
$markup[] = $view->formHidden('MAX_FILE_SIZE', $size);
}
if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) {
$apcAttribs = array('id' => 'progress_key');
$markup[] = $view->formHidden('APC_UPLOAD_PROGRESS', uniqid(), $apcAttribs);
}
else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) {
$uploadIdAttribs = array('id' => 'progress_key');
$markup[] = $view->formHidden('UPLOAD_IDENTIFIER', uniqid(), $uploadIdAttribs);
}
/*
if ($element->isArray()) {
$name .= "[]";
$count = $element->getMultiFile();
for ($i = 0; $i < $count; ++$i) {
$htmlAttribs = $attribs;
$htmlAttribs['id'] .= '-' . $i;
$markup[] = $view->formFile($name, $htmlAttribs);
}
}
else {$markup[] = $view->formFile($name, $attribs);}
*/
$markup = implode($separator, $markup);
switch ($placement) {
case self::PREPEND: return $markup . $separator . $content;
case self::APPEND:
default: return $content . $separator . $markup;
}
}
}
?>
Form setup in controller action:
$form = new Zend_Form();
$form->addElement(new Zend_Form_Element_File('file'));
$form->file->setLabel('File');
$form->file->setDescription('Description goes here.');
$decorators = array();
$decorators[] = array('File' => new Sys_Form_Decorator_File());
$decorators[] = array('ViewScript', array('viewScript' => '_formElementFile.phtml'));
$form->file->setDecorators($decorators);
$this->view->form = $form;
In action view:
<?php echo $this->form; ?>
In element script:
<div class="field" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())) : ?>
<?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value">
<?php
echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
);
?>
</span>
<?php if (0 < $this->element->getMessages()->length) : ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())) : ?>
<span class="hint"><?php echo $this->element->getDescription(); ?></span>
<?php endif; ?>
</div>
Output should be:
<form enctype="multipart/form-data" action="" method="post">
<dl class="zend_form">
<input type="hidden" name="MAX_FILE_SIZE" value="134217728" id="MAX_FILE_SIZE" />
<div class="field" id="field_file">
<label for="file">File</label>
<span class="value"><input type="file" name="file" id="file" /></span>
<span class="hint">Description goes here.</span>
</div>
</dl>
</form>
A problem with this solution is that the hidden elements don't render within the viewscript; this might be a problem if you're using the div as a selector in a client-side script...
What I've realized is, a custom decorator class will handle most fields except File fields.
Make sure your class implements the interface like so:
class CC_Form_Decorator_Pattern
extends Zend_Form_Decorator_Abstract
implements Zend_Form_Decorator_Marker_File_Interface
This worked for me.
This helped me fix my problem. I adjusted the code to wrap the file element inside a table. To make it work, simply remove the label from the viewdecorator and add the file element as follows:
$form->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array(
'Label',
array(array('labelTd' => 'HtmlTag'), array('tag' => 'td', 'class' => 'labelElement')),
array(array('elemTdOpen' => 'HtmlTag'), array('tag' => 'td', 'class' => 'dataElement','openOnly' => true, 'placement' => 'append')),
'File',
array('ViewScript', array(
'viewScript' => 'decorators/file.phtml',
'placement' => false,
)),
array(array('elemTdClose' => 'HtmlTag'), array('tag' => 'td', 'closeOnly' => true, 'placement' => 'append')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
),
'label' => 'Upload',
'required' => false,
'filters' => array(),
'validators' => array(array('Count', false, 1), ),
));
I've found a work-around that avoids the ViewScript altogether.
First, the element definition:
$this->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array(
'File',
array(array('Value'=>'HtmlTag'), array('tag'=>'span','class'=>'value')),
'Errors',
'Description',
'Label',
array(array('Field'=>'HtmlTag'), array('tag'=>'div','class'=>'field file')),
),
'label' => 'Upload File',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(),
));
Second, after the form class has been instantiated, I mimic the behavior of my ViewScript:
$field = $form->getElement('upload_file');
$decorator = $field->getDecorator('Field');
$options = $decorator->getOptions();
$options['id'] = 'field_' . $field->getId();
if ($field->hasErrors()) {
$options['class'] .= ' errors';
}
$decorator->setOptions($options);
I guess that I should look into class-based decorators. Maybe there's more flexibility there?
The easiest thing to do is to add no markup at all to the output in your custom File Decorator:
class Custom_Form_Decorator_File extends Zend_Form_Decorator_File {
public function render($content) {
return $content;
}
}
now you can do whatever you want in your viewscript for this file element (output the file input field and all hidden fields you need on your own).
Just in case you have followed #Shaun's answer and you are still getting the error: make sure that you've disabled default decorators for the element in question (take look at line 2):
$this->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array('File', array('ViewScript', array(
'viewScript' => '_form/file.phtml',
'placement' => false,
))),
'label' => 'Upload',
'required' => false,
'filters' => array(),
'validators' => array(array('Count', false, 1),),
));