<?php
namespace App\Form;
use App\Entity\Course;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Context\ExecutionContext;
class CourseFormType extends AbstractType
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class, [
'constraints' => [
new Callback(['callback' => function ($value, ExecutionContext $ctx) use ($builder) {
if (strlen($value) > 50) {
$ctx->addViolation("The content should not be more than 50 words");
} elseif (strlen($value) == 0) {
$ctx->addViolation('Please fill in course title');
} else {
$course = $this->entityManager->getRepository(Course::class)->findOneByField('title', $value);
$course_id = $builder->getOption('course_id');
if ($course && $course->getId() != $course_id) {
$ctx->addViolation('This collection should contain only unique elements.');
}
}
}]),
]
])
->add('cover_picture', HiddenType::class, [
'constraints' => [
new Callback(['callback' => function ($value, ExecutionContext $ctx) {
if (empty($value)) {
$ctx->addViolation("Please upload cover_picture");
}
}]),
],
])
->add('description', TextareaType::class, [
'constraints' => [
new Callback(['callback' => function ($value, ExecutionContext $ctx) {
if (strlen($value) > 1000) {
$ctx->addViolation("The content should not be more than 1000 words");
} elseif (strlen($value) == 0) {
$ctx->addViolation('Please fill in course description');
}
}]),
]
])
->add('tag', TextType::class, [
'constraints' => [
new NotNull([], 'Please fill in tag')
]
])
->add('need_certification', IntegerType::class)
->add('certification_name', TextType::class, [
'constraints' => [
new Callback(['callback' => function ($value, ExecutionContext $ctx) {
if ($ctx->getRoot()['need_certification']->getData() == 1) {
if (empty($value)) {
$ctx->addViolation("Please fill incertification name");
} elseif (strlen($value) > 20) {
$ctx->addViolation("The content should not be more than 20 words");
}
}
}]),
],
])
->add('certification_url', HiddenType::class, [
'constraints' => [
new Callback(['callback' => function ($value, ExecutionContext $ctx) {
if ($ctx->getRoot()['need_certification']->getData() == 1) {
if (empty($value)) {
$ctx->addViolation("Please upload certification");
}
}
}]),
],
]
);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Course::class,
// enable/disable CSRF protection for this form
'csrf_protection' => true,
// the name of the hidden HTML field that stores the token
'csrf_field_name' => '_token',
// an arbitrary string used to generate the value of the token
// using a different string for each form improves its security
'csrf_token_id' => 'course_create',
//exclude self
'course_id' => 0
]);
}
}
