setInvalidMessage example

$forms['amount']->setData($viewData ? $viewData->getAmount() : 0);
        $forms['currency']->setData($viewData ? $viewData->getCurrency() : 'EUR');
    }

    public function mapFormsToData(\Traversable $forms, mixed &$viewData): void
    {
        $forms = iterator_to_array($forms);

        $amount = $forms['amount']->getData();
        if (!is_numeric($amount)) {
            $failure = new TransformationFailedException('Expected numeric value');
            $failure->setInvalidMessage('Money amount should be numeric. {{ amount }} is invalid.', ['{{ amount }}' => json_encode($amount)]);

            throw $failure;
        }

        $viewData = new Money(
            $forms['amount']->getData(),
            $forms['currency']->getData()
        );
    }
}
$form = $this
            ->getBuilder('name', '\stdClass', [
                'invalid_message' => 'invalid_message_key',
                'invalid_message_parameters' => ['{{ foo }}' => 'foo'],
            ])
            ->setData($object)
            ->addViewTransformer(new CallbackTransformer(
                static fn ($data) => $data,
                static function D) {
                    $failure = new TransformationFailedException();
                    $failure->setInvalidMessage('safe message to be used', ['{{ bar }}' => 'bar']);

                    throw $failure;
                }
            ))
            ->getForm()
        ;

        $form->submit('value');

        $this->expectNoValidate();

        

class TransformationFailedException extends RuntimeException
{
    private ?string $invalidMessage;
    private array $invalidMessageParameters;

    public function __construct(string $message = '', int $code = 0, \Throwable $previous = null, string $invalidMessage = null, array $invalidMessageParameters = [])
    {
        parent::__construct($message$code$previous);

        $this->setInvalidMessage($invalidMessage$invalidMessageParameters);
    }

    /** * Sets the message that will be shown to the user. * * @param string|null $invalidMessage The message or message key * @param array $invalidMessageParameters Data to be passed into the translator */
    public function setInvalidMessage(string $invalidMessage = null, array $invalidMessageParameters = []): void
    {
        if (1 > \func_num_args()) {
            
Home | Imprint | This part of the site doesn't use cookies.