How to Validate Input in Symfony | Symfony Validation | Symfony 6 for Beginners | Learn Symfony 6

preview_player
Показать описание
This video will show you how you can easily validate input fields through Symfony Forms - Want to learn an incredible open-source PHP framework? Symfony is one of the most popular frameworks when you need to build high-performance & complex web applications.

📚 New Udemy Course

📲 Let’s plan a meeting

💌 Newsletter

🔥 Resources

🔗 Useful links

Want to learn more?

📋 Table of Content
00:00 - Introduction
01:08 - Assert on Entity
04:00 Remove required attributes
05:02 - Validation on edit

#symfony #framework #symfony6 #php
Рекомендации по теме
Комментарии
Автор

Good to mention Annotations vs Attributes:

Annotations style:
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
* @Assert\Length(min=3)
*/
private $title;

Attributes style:

#[ORM\Column(type: 'string', length: 255)]
#[Assert\NotBlank]
#[Assert\Length(min: 3)]
private $title;

ilyasbakirov
Автор

I am facing a few issues whilst I am practicing this.

1. As I have attributes already then I used annotations only for Assert [I tried the way of attributes #[Assert/NotBlank] but it's giving me an error] ... So, can I use both like this ->
#[ORM\Column(length: 255)]
/**
* @Assert\NotBlank
* @Assert\Length(min=3)
*/

2. If I put this Assert on ImagePath it's not letting me to submit the form though I am uploading image. [why is this happening with me?]

ummehanyarozshandaanny
Автор

thanks for ypur lessons. I'm facing an issue. Cannot create a movie. There is a piece of the video (on 4:57 timecode) where you also face that problem, even though you choose an image, but You cut that piece of the video. Please help. Stuck in this place.

mr_sat
Автор

create video on collection type, dto, mapping

Sadiq
Автор

Dary Update and Delete Blog Items are missing in your playlist

ilyasbakirov
Автор

Why your entity/movie looks diferent here. in earlies movies there was atributes etc..

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\Column(type: 'string', length: 255)]
private $title;

#[ORM\Column(type: 'integer')]
private $releaseYear;

#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $description;

#[ORM\Column(type: 'string', length: 255)]
private $imagePath;

Actor::class, inversedBy: 'movies')]
private $actors;

hekmatyar
Автор

1. ERROR : Expected argument of type "string", "null" given at property path "title".
FIX :

public function setTitle(?string $title): static
{
$this->title = $title ?? '';

return $this;
}

2. ERROR : Expected argument of type "int", "string" given at property path "releaseYear".
FIX :

public function setReleaseYear($releaseYear): static
{
$this->releaseYear = (int) $releaseYear;

return $this;
}

naimchowdhury
Автор

First off, thank you for this awesome tutorial, it's been going really good so far and i have learned a lot, but now i have run into an issue that i'm unable to solve.

When i add #[Assert\NotBlank] to the $imagePath property in Entity\Movie.php, the form will no longer validate.

In MovieController.php, inside the 'create' function, i added some debugging




if ($form->isSubmitted()) {
dd($form->getData());

dd($form->isValid());
}

if ($form->isSubmitted() && $form->isValid()) {


These are the results of each:

dd($form->getData());
App\Entity\Movie {#336 ▼
-id: null
-title: "test5"
-releasYear: 2111
-description: "hgfhssaaaa"
-imagePath: null
-Actors: {#337 ▶}
}


{#57 ▼
-test: false
-originalName: "test.png"
-mimeType: "image/png"
-error: 0
path: "D:\xampp\tmp"
filename: "phpC7D6.tmp"
****
}

dd($form->isValid());
false

So, when i use $form->getData(), imagePath is null, therefore isValid returns false
but when i use $form->get('imagePath')->getData(), it returns an object with the correct data

When i omit the #[Assert\NotBlank] validation, the file uploads correctly when provided but returns a 500 error when left empty.
I've tried using but it still gives a 500 error when submitted with an empty imagePath.

I have no idea how to solve this. I've tried looking into the form\getData() function, to maybe figure out how it works, but i only find the Interface, not the actual code. So i'm kinda stuck.

If anyone can help me out here, that would be awesome.

tryphase_be