Попробуйте удалить $form->handleRequest($request);
, handleRequest
не работает с вызовами API
Обновление.
Вот простой пример для проверки сущности
Сущность:
use Symfony\Component\Validator\Constraints as Assert;
class SomeClass
{
/**
* @Assert\NotBlank
*/
private $content;
}
FormType:
class SomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SomeClass::class,
]);
}
}
FormHandler:
class FormHandler
{
private $formFactory;
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
public function handleWithSubmit(
array $data,
string $type,
$entity,
array $options = []
) {
$form = $this->formFactory->create($type, $entity, $options);
$form->submit($data);
return $this->processSubmitted($form, $entity);
}
private function processSubmitted(FormInterface $form, $entity)
{
if (!$form->isValid()) {
return $form->getErrors();
}
if (!is_object($entity)) {
$entity = $form->getData();
}
return $entity;
}
}
Ввести FormHandler
в контроллер и вызвать
$handled = $this->formHandler->handleWithSubmit($request->request->all(), SomeType::class, new SomeClass());
if (!$handled instanceof SomeClass) {
// return errors
}