为什么我的验证规则不起作用symfony2

时间:2014-11-04 13:08:20

标签: php symfony

当我提交表单时,我收到了消息:'无法找到该文件。',但这不是我的验证消息。怎么解决这个?

我的实体User.php在src / Acme / UserBundle / Entity / User.php中:

<?php

namespace Acme\UserBundle\Entity;


use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    //...
    /**
     *  @ORM\Column(type="string", length=255)
     */

    protected $path;

    /**
     * @Assert\File(
     *  maxSizeMessage = "Файл должен быть не больше 5Mb",
     *  maxSize = "5000k",
     *  mimeTypes = {"image/jpg", "image/jpeg", "image/gif", "image/png"},
     *  mimeTypesMessage = "Используйте PNG. JPG или GIF формат",
     *  uploadErrorMessage = "Файл не может быть загружен"
     * )
     */
    private $file;

    public function setFile($file){

        $this->file = $file;

    }
    public function getFile(){
        return $this->file;
    }

    /**
     * Set path
     *
     * @param string $path
     * @return User
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
        return $this->path;
    }
}

我的控制器在Acme / UserBundle / Controller中:

namespace Acme\UserBundle\Controller;


use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Controller\ProfileController as BaseController;
use Acme\UserBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
 * 
 * @Route("/profile")
 * 
*/

class ProfileController extends BaseController
{


    public function showAction(){
        $user = new User();
        $form = $this->createFormBuilder($user)
                        ->add('file', 'file')
                        ->getForm();
        $user = $this->getUser();
        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        return $this->render('FOSUserBundle:Profile:show.html.twig', array(
            'user' => $user,
            'form' => $form->createView()
        ));
    }

    /**
     * @Route("/upload", name="user_upload")
     * @Method({"POST"})
     */
    public function uploadAction(Request $request)
    {
        $user = $this->getUser(); 
        $userForm = new User();
        $form = $this->createFormBuilder($userForm)
                       ->add('file', 'file')
                            ->getForm();
        $form->handleRequest($request);
        if($form->isValid()){
            $user->setPath('test');

            $this->get('fos_user.user_manager')->updateUser($user, false);
            $this->getDoctrine()->getEntityManager()->flush();

        }

        return $this->render('FOSUserBundle:Profile:show.html.twig', array(
            'user' => $user,
            'form' => $form->createView()
        ));

    }




}

0 个答案:

没有答案