Skip to content

Commit

Permalink
Allow setting checkDirty using attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Apr 10, 2024
1 parent 8c39469 commit e796df2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/AbstractForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function getTemplateFile(): string
}


public function isPreventLeavingWhenDirty(): bool
{
$rc = new ReflectionClass($this);
return (bool) $rc->getAttributes(PreventLeavingWhenDirty::class);
}


public function setTranslator(Translator $translator = null): void
{
$this->translator = $translator;
Expand Down Expand Up @@ -278,6 +285,11 @@ public function render(): void
$template->setParameters([
'layout' => $this->layout,
'form' => $form,

'formOptions' => [
'data-check-dirty' => $this->isPreventLeavingWhenDirty(),
'data-form-name' => $this->getName(),
],
]);

$template->render();
Expand Down
15 changes: 15 additions & 0 deletions src/Attributes/PreventLeavingWhenDirty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2024
* @license MIT License
*/

namespace JuniWalk\Form\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class PreventLeavingWhenDirty
{
}
2 changes: 1 addition & 1 deletion src/templates/@layout-accordion.latte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{default $headColor = null}

<div id="{$control->getName()}" n:class="card, 'card-'.$color, !isset($headColor) || $headColor != $color ? card-outline">
<form n:name="form" data-form-name="{$control->getName()}">
<form n:name="form" n:attr="...$formOptions">

<a class="d-block w-100 collapsed" data-toggle="collapse" href="#{$control->getUniqueId()}-content">
<div n:class="card-header, isset($headColor) ? 'bg-'.$headColor">
Expand Down
2 changes: 1 addition & 1 deletion src/templates/@layout-card.latte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{default $headColor = null}

<div id="{$control->getName()}" n:class="card, 'card-'.$color, !isset($headColor) || $headColor != $color ? card-outline">
<form n:name="form" data-form-name="{$control->getName()}">
<form n:name="form" n:attr="...$formOptions">

<div n:class="card-header, isset($headColor) ? 'bg-'.$headColor">
<h3 class="card-title" n:ifset="block title">{include #title|noescape}</h3>
Expand Down
4 changes: 2 additions & 2 deletions src/templates/@layout-modal.latte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="modal fade" id="{$control->getName()}" n:attr="... $modalOptions" tabindex="-1">
<div class="modal fade" id="{$control->getName()}" n:attr="...$modalOptions" tabindex="-1">

{default $size = null}

Expand All @@ -9,7 +9,7 @@
{default $contentClass = null}
{default $contentPos = 'top'}

<form n:name="form" class="w-100" data-form-name="{$control->getName()}">
<form n:name="form" class="w-100" n:attr="...$formOptions">

<div class="modal-content">

Expand Down

0 comments on commit e796df2

Please sign in to comment.