-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from SBSEDV/message-resolver
Message resolver
- Loading branch information
Showing
17 changed files
with
128 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use SBSEDV\Bundle\FormBundle\MessageResolver\ChainMessageResolver; | ||
use SBSEDV\Bundle\FormBundle\MessageResolver\DoctrineTypeMessageResolver; | ||
use SBSEDV\Bundle\FormBundle\MessageResolver\MessageResolverInterface; | ||
|
||
return function (ContainerConfigurator $container): void { | ||
$container->services() | ||
->set(ChainMessageResolver::class) | ||
->arg('$messageResolvers', tagged_iterator('sbsedv_form.message_resolver')) | ||
|
||
->alias(MessageResolverInterface::class, ChainMessageResolver::class) | ||
|
||
->set(DoctrineTypeMessageResolver::class) | ||
->tag('sbsedv_form.message_resolver', ['priority' => -100]) | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace SBSEDV\Bundle\FormBundle\MessageResolver; | ||
|
||
use Symfony\Component\Form\FormError; | ||
|
||
class ChainMessageResolver implements MessageResolverInterface | ||
{ | ||
/** | ||
* @param iterable<MessageResolverInterface> $messageResolvers | ||
*/ | ||
public function __construct( | ||
private readonly iterable $messageResolvers | ||
) { | ||
} | ||
|
||
public function resolveMessage(FormError $formError): ?string | ||
{ | ||
foreach ($this->messageResolvers as $messageResolver) { | ||
$msg = $messageResolver->resolveMessage($formError); | ||
|
||
if (null !== $msg) { | ||
return $msg; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace SBSEDV\Bundle\FormBundle\MessageResolver; | ||
|
||
use Symfony\Bridge\Doctrine\Form\Type\DoctrineType; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
use Symfony\Component\Form\FormError; | ||
|
||
class DoctrineTypeMessageResolver implements MessageResolverInterface | ||
{ | ||
public function resolveMessage(FormError $formError): ?string | ||
{ | ||
$type = $formError->getOrigin()?->getConfig()->getType()->getInnerType(); | ||
$cause = $formError->getCause(); | ||
|
||
// Incase the "multiple" option is used and an invalid id is supplied | ||
if (!$type instanceof DoctrineType || !$cause instanceof TransformationFailedException || !\str_starts_with($cause->getMessage(), 'The choices "')) { | ||
return null; | ||
} | ||
|
||
// Currently this will always be NULL, someday symfony will maybe add translations for that error | ||
if (null !== $cause->getInvalidMessage()) { | ||
return \strtr($cause->getInvalidMessage(), $cause->getInvalidMessageParameters()); | ||
} | ||
|
||
return $cause->getMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace SBSEDV\Bundle\FormBundle\MessageResolver; | ||
|
||
use Symfony\Component\Form\FormError; | ||
|
||
interface MessageResolverInterface | ||
{ | ||
/** | ||
* Resolve an error message. | ||
* | ||
* @param FormError $formError The FormError object to resolve from. | ||
* | ||
* @return string|null The error message or null if not supported. | ||
*/ | ||
public function resolveMessage(FormError $formError): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters