EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Disabled DateField on non-required field still editable

Open mpserellis opened this issue 2 years ago • 6 comments

I have a form where I want most of the fields to be disabled because they were input elsewhere (on the frontend). The fields are displayed within EasyAdmin so they can be reviewed. There are 2 DateField's and both are set to disabled. One of the fields is set as required and it is rendered non-editable in the browser as expected. The another is set as not required and it is rendered as still editable in the browser despite being set as disabled and it can be cleared with the "leave empty" checkbox.

I would expect that a disabled field regardless of whether it is required or not is non-editable.

This is with version 4.2.2

My model has "appliedOn" which is not nullable and dateOfBirth which is nullable.

In the crud controller configureFields function I have:

DateField::new('appliedOn')->setColumns(12)->addCssClass('horizontal')->setDisabled(true),
...
DateField::new('dateOfBirth')->hideOnIndex()->setColumns(12)->addCssClass('horizontal')->setDisabled(true),

If I add setRequired(true) to the second field it renders as disabled, if I add setRequired(false) to the first field it renders as editable.

I hope that makes sense, let me know if you need any further information.

mpserellis avatar May 24 '22 13:05 mpserellis

@mpserellis the last part of your message is a bit confusing to me. Please, show the full code that works and the full code that fails. In the first case for example, I'm not sure if it works with ->setDisabled()->setRequired(false) or with ->setDisabled() or with ->setRequired(false). Thanks!

javiereguiluz avatar Jun 03 '22 18:06 javiereguiluz

Sorry for the confusion. The Entity has the following field:

  /**
   * @var null|DateTime
   *
   * @ORM\Column(name="date_of_birth", type="date", nullable=true)
   */
  private $dateOfBirth;

The crud controller configureFields function has the following:

DateField::new('dateOfBirth')->hideOnIndex()->setColumns(12)->addCssClass('horizontal')->setDisabled(true);

This renders as editable in the browser (see screen shot below):

easy_admin_disabled_field

If I change it to the below it renders disabled:

DateField::new('dateOfBirth')->hideOnIndex()->setColumns(12)->addCssClass('horizontal')->setDisabled(true)->setRequired(true);

easy_admin_disabled_field_with_required

As you can see in both instances disabled is being set to true but only required fields are being rendered as disabled.

The other field is the opposite. The Entity has the below:

  /**
   * @var DateTime
   * @ORM\Column(name="applied_on", type="datetime")
   * @Gedmo\Timestampable(on="create")
   */
  private $appliedOn;

The crud controller function has:

DateField::new('appliedOn')->setColumns(12)->addCssClass('horizontal')->setDisabled(true);

This renders correctly as disabled but if I change it to make it non-required as shown below then it renders as editable:

DateField::new('appliedOn')->setColumns(12)->addCssClass('horizontal')->setDisabled(true)->setRequired(false);

Please note: if you are not seeing this the browser please force reload with Ctrl+F5 or check the html source for the disabled flag on the input.

I hope that makes it clearer but if not please let me know.

Thank you very much Mark

mpserellis avatar Jun 04 '22 08:06 mpserellis

Same problem here, disabled not working on Date(Time)Field, same code as above
Also, with or without required, it still editable for me

AntoninHuaut avatar Aug 23 '22 14:08 AntoninHuaut

I've checked the issue and replicated on demo, here: https://github.com/Holicz/easyadmin-demo/tree/5246. It is visible in EA backend, when creating Post, on field Published at.

The issue is with checkbox "Leave empty" for non-required field which will make disabled fields editable.

I've also prepared fix: https://github.com/EasyCorp/EasyAdminBundle/pull/5376

Holicz avatar Aug 23 '22 14:08 Holicz

I've checked the issue and replicated on demo, here: https://github.com/Holicz/easyadmin-demo/tree/5246. It is visible in EA backend, when creating Post, on field Published at.

The issue is with checkbox "Leave empty" for non-required field which will make disabled fields editable.

I've also prepared fix: #5376

I have tested your changes and the checkbox disappears well, so it's fixed. Thanks

AntoninHuaut avatar Aug 24 '22 12:08 AntoninHuaut

Hello,

Thank you for the fix, however, after applying the change proposed to form_theme.html.twig I am getting an error on date fields that are not required and not disabled. Otherwise it works:

Twig\Error\RuntimeError:
Key "disabled" for array with keys "required, row_attr, label, widget, html5" does not exist.

  at vendor/easycorp/easyadmin-bundle/src/Resources/views/crud/form_theme.html.twig:106

My Entity has the following field:

  /**
   * @var null|DateTime
   * @ORM\Column(name="proposed_end", type="date", nullable=true)
   */
  private ?DateTime $proposedEnd = null;

And my CRUD controller has the following in configureFields:

DateField::new('proposedEnd')->hideOnIndex()->setColumns(12)->addCssClass('horizontal'),

If I add ->setDisabled(false) then I don't get the error so perhaps it is needing a default set or a test to see if the disabled property is set?

Screenshot_2022-09-06_10-10-05

mpserellis avatar Sep 06 '22 09:09 mpserellis

Closing as explained in https://github.com/EasyCorp/EasyAdminBundle/pull/5376#issuecomment-1258434334 Thanks.

javiereguiluz avatar Sep 26 '22 18:09 javiereguiluz