Skip to content

Commit

Permalink
fix: Name validation error while trying to save an edited command (ba…
Browse files Browse the repository at this point in the history
…ckport for 5.x) (#105)

Co-authored-by: Christopher Georg <[email protected]>
  • Loading branch information
Chris53897 and Chris8934 authored Feb 2, 2024
1 parent 227246e commit 705bbf3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Controller/DetailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class DetailController extends AbstractBaseController
*/
public function edit(Request $request, $id = null): Response
{
$validationGroups = [];
$scheduledCommand = $id ? $this->getDoctrineManager()->getRepository(ScheduledCommand::class)->find($id) : null;
if (!$scheduledCommand) {
$scheduledCommand = new ScheduledCommand();
$validationGroups[] = 'new';
}

$form = $this->createForm(ScheduledCommandType::class, $scheduledCommand);
$form = $this->createForm(ScheduledCommandType::class, $scheduledCommand, [
'validation_groups' => $validationGroups
]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand Down
2 changes: 1 addition & 1 deletion Entity/ScheduledCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
#[ORM\Entity(repositoryClass: ScheduledCommandRepository::class)]
#[ORM\Table(name: "scheduled_command")]
#[UniqueEntity(fields: ["name"])]
#[UniqueEntity(fields: ["name"], groups: ['new'])]
class ScheduledCommand
{
#[ORM\Id, ORM\Column(type: Types::INTEGER), ORM\GeneratedValue(strategy: 'AUTO')]
Expand Down
24 changes: 18 additions & 6 deletions Form/Type/ScheduledCommandType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -20,14 +22,24 @@ class ScheduledCommandType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add(
'name',
TextType::class,
[
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void
{
/** @var ScheduledCommand $scheduledCommand */
$scheduledCommand = $event->getData();
$form = $event->getForm();
$options = [
'label' => 'detail.name',
'required' => true,
]
);
];

if (! is_null($scheduledCommand->getId())) {
$options['attr'] = ['readonly' => 'readonly'];
}

$form->add('name',
TextType::class,
$options);
});

$builder->add(
'command',
Expand Down

0 comments on commit 705bbf3

Please sign in to comment.