Skip to content

[POC] [Form] Support get/set accessors for form fields #37614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Form] Support catching exceptions from an accessor
  • Loading branch information
alcaeus committed Jul 20, 2020
commit 2fc85b01a3520bea54bbb61a3b15c0b0c9ef76b5
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Symfony\Component\Form\Extension\Core\DataMapper;

use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\ExceptionInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormError;
use TypeError;

class AccessorMapper implements DataMapperInterface
{
Expand Down Expand Up @@ -69,7 +72,13 @@ public function mapFormsToData(iterable $forms, &$data)
// Write-back is disabled if the form is not synchronized (transformation failed),
// if the form was not submitted and if the form is disabled (modification not allowed)
if (null !== $this->set && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be possible for $this->set to be null as you checked it a few lines earlier (to fallback on the old datamapper).

Unless I read something wrong ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I'll update this.

$returnValue = ($this->set)($data, $form->getData());
try {
$returnValue = ($this->set)($data, $form->getData());
Copy link
Member

@yceruto yceruto Jul 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given a compound form with two fields (let's say name and active, string and bool respectively) If I understand it correctly, the same 'set' accessor will be used for all (children) form data, is that expected? shouldn't the 'set' accessor of the current child be called instead?

Copy link
Member

@yceruto yceruto Jul 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, I expected something like this $set = $form->getConfig()->getOption('set') somewhere. same for 'get' accessor.

} catch (ExceptionInterface | TypeError $e) {
$form->addError(new FormError($e->getMessage()));
continue;
}

$type = is_object($returnValue) ? get_class($returnValue) : gettype($returnValue);

if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

// Declare strict is necessary here to provoke type errors
declare(strict_types = 1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -17,6 +20,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Extension\Core\DataMapper\AccessorMapper;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Form;
Expand Down Expand Up @@ -236,6 +240,63 @@ public function getEngineClosure()
$this->assertSame('petrol', $data->getEngineClosure());
}

public static function invalidValueProvider(): \Generator
{
yield 'validation error' => ['#Corn is not a valid engine type#', 'corn'];
yield 'type error' => ['#Argument 1 passed to class@anonymous::setEngineClosure\(\) must be of the type string, object given#', new stdClass()];
}

/**
* @dataProvider InvalidValueProvider
*/
public function testSetAccessorCatchesExceptions(string $errorMessagePattern, $value)
{
$this->setupPropertyPathMapper($this->any(), $this->any());

$data = new class('petrol') {
private $engine;

public function __construct(string $engine)
{
$this->engine = $engine;
}

public function setEngineClosure(string $data)
{
if ($data === 'corn') {
throw new class extends RuntimeException
{
public function __construct()
{
parent::__construct('Corn is not a valid engine type');
}
};
}

$this->engine = $data;
}

public function getEngineClosure()
{
return $this->engine;
}
};

$config = new FormConfigBuilder('car', null, $this->dispatcher);
$config->setCompound(true);
$config->setDataMapper($this->createMapper(true, true));
$config->setData($data);
$form = new Form($config);
$form
->add(new Form(new FormConfigBuilder('engine', null, $this->dispatcher)));

$form->submit(['engine' => $value]);
$this->assertFalse($form->isValid());
$this->assertSame('petrol', $data->getEngineClosure());

$this->assertMatchesRegularExpression($errorMessagePattern, (string) $form->get('engine')->getErrors());
}

private function setupPropertyPathMapper(Invocation $dataToFormsMatcher, Invocation $formsToDataMatcher): void
{
$propertyPathMapper = new PropertyPathMapper($this->propertyAccessor);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy