gilmar bec
gilmar bec

Reputation: 3

Symfony Form don't send file to controller

Hello im new in symfony and i have to make a CRUD of products with him. I have to send this informations to database:

Problem:

Obs:

My Entity:

/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(type="text", length=4000, nullable=true)
     */
    private $description;

    /**
     * @ORM\Column(type="blob")
     */
    private $image;

    /**
     * @ORM\Column(type="integer")
     */
    private $stock;

My Form is generate by:

$product = new Product();

$form = $this->createFormBuilder($product)
    ->add('title',TextType::class, [
        'required' => true,
        'label' => 'Titulo: ',
        'attr' => ['minlength' => 6, 'id' => 'title_product'],
    ])
    ->add('description', TextareaType::class,[
        'required' => false,
        'label' => 'Descrição: ',
        'attr' => ['maxlength' => 4000, 'id' => 'description_product'],
    ])
    ->add('image', FileType::class, [
        'required' => true,
        'attr' => ['accept' => 'image/jpeg, image/png, image/gif'],
        'label' => 'Imagem do produto(JPG, PNG ou GIF): ',
        'help' => 'A imagem deve ter um peso maximo de 5 MBs.',
    ])
    ->add('stock', IntegerType::class, [
        'required' => true,
        'label' => 'Quantidade em estoque: ',
    ])
    ->add('save', SubmitType::class, [
        'label' => 'Criar Produto',
        'attr' => ['class' => 'btn btn-success']
    ])
    ->getForm();

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    $product = $form->getData();

    if ($this->validateFormProduct($product)){
        $this->createProduct($product);
        return $this->redirectToRoute('index_products');
    }
}
return $this->render('product/new.html.twig', [
    'form' => $form->createView()
]);

Functions of validation:

private function invalidImageType($img) {
    $permitedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
    $detectedType= exif_imagetype($img);
    return !in_array($detectedType, $permitedTypes);
}

private function invalidImageSize($img) {
    return filesize($img) > 5000000;
}

private function validateFormProduct($form){
    if ($this->invalidImageType($form->getImage())){
        $this->addFlash(
            'warning',
            'Tipo de imagem invalido!'
        );
        return false;
    }

    if ($this->invalidImageSize($form->getImage())){
        $this->addFlash(
            'warning',
            'Este arquivo excede o tamanho maximo de 5mb!'
        );
        return false;
    }
    return true;
}

Function encript:

private function encriptImage($img) {
    $normalizer = new DataUriNormalizer();
    return $normalizer->normalize(new \SplFileObject($img));
}

Upvotes: 0

Views: 317

Answers (2)

Houssein Zouari
Houssein Zouari

Reputation: 722

You need to update the values of upload_max_filesize and post_max_size in your php.ini: (configuration file)

; Maximum allowed size for uploaded files.
upload_max_filesize = 1M

; Must be greater than or equal to upload_max_filesize
post_max_size = 1M

See the Description php.ini.

Upvotes: 0

Khaldoun Nd
Khaldoun Nd

Reputation: 1466

You need to increase the max upload size in your php.ini configuration file.
Both upload_max_filesize and post_max_size

Make sure that the second one is either greater than or equal the first.

Upvotes: 0

Related Questions