From 247f9e6a01bba85ee1031e413907b6b1b39c9cbd Mon Sep 17 00:00:00 2001 From: Hugo PRADIER Date: Wed, 5 Jun 2024 11:36:46 +0200 Subject: [PATCH] add form species --- composer.json | 2 +- composer.lock | 29 +++++++++++----------- src/Controller/SpeciesController.php | 37 ++++++++++++++++++++++++++++ src/Entity/Species.php | 4 +++ src/Form/SpeciesType.php | 28 +++++++++++++++++++++ templates/species/index.html.twig | 20 +++++++++++++++ templates/species/species.html.twig | 3 +++ 7 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 src/Controller/SpeciesController.php create mode 100644 src/Form/SpeciesType.php create mode 100644 templates/species/index.html.twig create mode 100644 templates/species/species.html.twig diff --git a/composer.json b/composer.json index d59ef97..7fbd14b 100644 --- a/composer.json +++ b/composer.json @@ -106,7 +106,7 @@ "symfony/browser-kit": "7.0.*", "symfony/css-selector": "7.0.*", "symfony/debug-bundle": "7.0.*", - "symfony/maker-bundle": "^1.0", + "symfony/maker-bundle": "^1.59", "symfony/phpunit-bridge": "^7.0", "symfony/stopwatch": "7.0.*", "symfony/web-profiler-bundle": "7.0.*" diff --git a/composer.lock b/composer.lock index 9e8ea25..75b7896 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "888e1953f86d0f1fe9b271b85aab0ed0", + "content-hash": "c3dc9b5ee79b98f2d1fabe3668860864", "packages": [ { "name": "composer/semver", @@ -3507,16 +3507,16 @@ }, { "name": "symfony/form", - "version": "v7.0.7", + "version": "v7.0.8", "source": { "type": "git", "url": "https://github.com/symfony/form.git", - "reference": "b4df6a399a2b03782a0163807239db342659f54f" + "reference": "1d0128e2f7e80c346ec51fa4d1ce4fec0d435eeb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/form/zipball/b4df6a399a2b03782a0163807239db342659f54f", - "reference": "b4df6a399a2b03782a0163807239db342659f54f", + "url": "https://api.github.com/repos/symfony/form/zipball/1d0128e2f7e80c346ec51fa4d1ce4fec0d435eeb", + "reference": "1d0128e2f7e80c346ec51fa4d1ce4fec0d435eeb", "shasum": "" }, "require": { @@ -3583,7 +3583,7 @@ "description": "Allows to easily create, process and reuse HTML forms", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/form/tree/v7.0.7" + "source": "https://github.com/symfony/form/tree/v7.0.8" }, "funding": [ { @@ -3599,7 +3599,7 @@ "type": "tidelift" } ], - "time": "2024-04-28T11:44:19+00:00" + "time": "2024-05-31T14:55:39+00:00" }, { "name": "symfony/framework-bundle", @@ -6887,16 +6887,16 @@ }, { "name": "symfony/validator", - "version": "v7.0.7", + "version": "v7.0.8", "source": { "type": "git", "url": "https://github.com/symfony/validator.git", - "reference": "ab4e75b9d23ba70e78480aecbe4d8da15adf10eb" + "reference": "23af65dff1f4dfee9aab3a0123a243e40fa3d9cf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/validator/zipball/ab4e75b9d23ba70e78480aecbe4d8da15adf10eb", - "reference": "ab4e75b9d23ba70e78480aecbe4d8da15adf10eb", + "url": "https://api.github.com/repos/symfony/validator/zipball/23af65dff1f4dfee9aab3a0123a243e40fa3d9cf", + "reference": "23af65dff1f4dfee9aab3a0123a243e40fa3d9cf", "shasum": "" }, "require": { @@ -6941,7 +6941,8 @@ "Symfony\\Component\\Validator\\": "" }, "exclude-from-classmap": [ - "/Tests/" + "/Tests/", + "/Resources/bin/" ] }, "notification-url": "https://packagist.org/downloads/", @@ -6961,7 +6962,7 @@ "description": "Provides tools to validate values", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/validator/tree/v7.0.7" + "source": "https://github.com/symfony/validator/tree/v7.0.8" }, "funding": [ { @@ -6977,7 +6978,7 @@ "type": "tidelift" } ], - "time": "2024-04-28T11:44:19+00:00" + "time": "2024-06-02T15:49:03+00:00" }, { "name": "symfony/var-dumper", diff --git a/src/Controller/SpeciesController.php b/src/Controller/SpeciesController.php new file mode 100644 index 0000000..4cae6ce --- /dev/null +++ b/src/Controller/SpeciesController.php @@ -0,0 +1,37 @@ +createForm(SpeciesType::class, $species); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + + $species = $form->getData(); + + $entityManager->persist($species); + $entityManager->flush(); + + return $this->redirectToRoute('_profiler_home'); + } + + return $this->render('species/species.html.twig', [ + 'form' => $form->createView(), + ]); + } +} diff --git a/src/Entity/Species.php b/src/Entity/Species.php index 9243896..2dd9528 100644 --- a/src/Entity/Species.php +++ b/src/Entity/Species.php @@ -6,6 +6,7 @@ use App\Repository\SpeciesRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Validator\Constraints as Assert; #[ORM\Entity(repositoryClass: SpeciesRepository::class)] class Species @@ -16,12 +17,15 @@ class Species private ?int $id = null; #[ORM\Column(length: 255)] + #[Assert\NotBlank] private ?string $scientific_name = null; #[ORM\Column(length: 255)] + #[Assert\NotBlank] private ?string $vernacular_name = null; #[ORM\Column(length: 255)] + #[Assert\NotBlank] private ?string $region = null; /** diff --git a/src/Form/SpeciesType.php b/src/Form/SpeciesType.php new file mode 100644 index 0000000..0d6cab9 --- /dev/null +++ b/src/Form/SpeciesType.php @@ -0,0 +1,28 @@ +add('scientific_name') + ->add('vernacular_name') + ->add('region') + ->add('save', SubmitType::class, ['label' => 'Create Species']); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => Species::class, + ]); + } +} diff --git a/templates/species/index.html.twig b/templates/species/index.html.twig new file mode 100644 index 0000000..3f56295 --- /dev/null +++ b/templates/species/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello SpeciesController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %} diff --git a/templates/species/species.html.twig b/templates/species/species.html.twig new file mode 100644 index 0000000..2727393 --- /dev/null +++ b/templates/species/species.html.twig @@ -0,0 +1,3 @@ +{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} + {{ form_widget(form) }} +{{ form_end(form) }}