Nic Raboy
Nic Raboy

Reputation: 3153

ZendFramework 2 Form File Validators

I have been following the ZendFramework documentation for file uploading found here: http://framework.zend.com/manual/2.1/en/modules/zend.form.file-upload.html

My problem is, when I try to submit the form when it is not valid I get the following error message:

Array provided to Escape helper, but flags do not allow recursion

Here is the code the the particular action in my controller:

public function addAction()
{
    $form = new TeamForm();
    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $team = new Team();
        $form->setInputFilter($team->getInputFilter());
        $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
        $form->setData($post);

        if ($form->isValid()) {
            $files = $request->getFiles();
            $filter = new RenameUpload(array("target" => "./public/uploads/", "use_upload_extension"  => true, "randomize" => true));
            $fileinfo = $filter->filter($files['image']);
            $team->exchangeArray($form->getData());
            $team->image = basename($fileinfo["tmp_name"]);

            $this->getTeamTable()->saveTeam($team);

            return $this->redirect()->toRoute('team');
        }
    }
    return array('form' => $form);
}

I narrowed the error down to the following line:

$form->setData($post);

When I do a variable dump of $post, everything looks correct. After searching around the internet I haven't been able to find any answers as to why this happens.

I'd be happy to supply more information if necessary regarding this.

Thanks,

EDIT

Here is the view code

<?php
$form->setAttribute('action', $this->url('team', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formInput($form->get('image'));
echo $this->formInput($form->get('name')
    ->setAttribute('class', 'large m-wrap')
    ->setAttribute('autocomplete', 'off')
    ->setAttribute('placeholder', 'Name'));
echo $this->formElementErrors($form->get('name'));
echo $this->formInput($form->get('title')
    ->setAttribute('class', 'large m-wrap')
    ->setAttribute('autocomplete', 'off')
    ->setAttribute('placeholder', 'Title'));
echo $this->formElementErrors($form->get('title'));
echo $this->formSubmit($form->get('submit')
    ->setAttribute('class', 'btn green'));
echo $this->form()->closeTag();
?>

Upvotes: 3

Views: 1683

Answers (3)

Hitendra Singh Rajput
Hitendra Singh Rajput

Reputation: 21

Problem is in your view file please use

<?php echo $this->formFile($form->get('image')); ?>

instead of

echo $this->formInput($form->get('image'));

for file type it should be $this->formFile()

Upvotes: 2

Carlos
Carlos

Reputation: 5072

The problem is not in the controller but in the view. You are passing an array to the view helper escape() instead of a string.

Upvotes: 0

Chidge
Chidge

Reputation: 184

I think the problem is the line above, do an array_merge rather than a recursive merge.

Upvotes: -1

Related Questions