指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
<?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
        ]);
    }
}