Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Name validation error while trying to save an edited command #76

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 17 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,23 @@ 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