Skip to content

Commit

Permalink
Fix: Name validation error while trying to save an edited command (#76)
Browse files Browse the repository at this point in the history
* fixed validation error on saving an edited command; fixed german translation for the flash message to start a command immediately

* removed translation changes

* modified command scheduler form to set the name field to readonly when editing a command
  • Loading branch information
DieWallSoCom authored Feb 2, 2024
1 parent 5abe4d8 commit e7e50b8
Show file tree
Hide file tree
Showing 3 changed files with 23 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 @@ -17,7 +17,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 @@ -11,6 +11,8 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -22,14 +24,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

0 comments on commit e7e50b8

Please sign in to comment.