У меня есть класс формы
<?php
namespace App\Form;
use App\Mapper\ProfileMapper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank;
final class UserType extends AbstractType
{
/**
* @var ContainerInterface
*/
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$allowedMimeTypes = $this->container->getParameter(
'allowed_mime_types'
);
$allowedMimeTypesString = implode(', ', $allowedMimeTypes);
$builder
->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('username', TextType::class)
->add('email', EmailType::class)
->add('imageFile', FileType::class, [
'constraints' => [
new Image(
[
'mimeTypes' => $allowedMimeTypes,
'maxSize' => $this->container->getParameter(
'max_uploaded_avatar_size'
),
'mimeTypesMessage' =>
'allowed image
formats are ('. $allowedMimeTypesString .')'
]
)
]
])
->add('currentPassword', PasswordType::class, [
'mapped' => false,
'constraints' => [
new NotBlank(),
new UserPassword()
]
])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'invalid_message' => 'The New Password and
New Password Repeat must match'
])
->add('submit', SubmitType::class)
->setDataMapper(new ProfileMapper());
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'empty_data' => null
]);
}
}
Есть Data Mapper
<?php
namespace App\Mapper;
use App\DTO\ProfileDto;
use App\Exception\InvalidTypeException;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\FormInterface;
final class ProfileMapper implements DataMapperInterface
{
/**
* @param mixed $data
* @param FormInterface[]|\Traversable $forms
* @throws InvalidTypeException
*/
public function mapDataToForms($data, $forms)
{
if (is_null($data)) {
return;
}
//if (!$data instanceof ProfileDto) {
//throw new InvalidTypeException();
//}
/** @var FormInterface[]|\Traversable $forms */
$forms = iterator_to_array($forms);
$forms['firstName']->setData($data->getFirstName());
$forms['lastName']->setData($data->getLastName());
$forms['username']->setData($data->getUsername());
$forms['email']->setData($data->getEmail());
$forms['plainPassword']->setData($data->getPlainPassword());
$forms['imageFile']->setData($data->getImageFile());
}
public function mapFormsToData($forms, &$data)
{
/** @var FormInterface[]|\Traversable $forms */
$forms = iterator_to_array($forms);
$data = new ProfileDto(
$forms['firstName']->getData(),
$forms['lastName']->getData(),
$forms['username']->getData(),
$forms['email']->getData(),
$forms['plainPassword']->getData(),
$forms['imageFile']->getData()
);
}
}
+ сам DTO
Создание формы в контроллере
/** @var User $user */
$user = $security->getUser();
$form = $this->createForm(UserType::class, $user);
Всю валидацию в данном случае я делаю в файле config/validator/validation.yml для DTO
App\DTO\ProfileDto:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [email, workspace_id]
entityClass: App\Repository\User\DoctrineUserRepository
em: doctrine.orm.default_entity_manager
properties:
firstName:
- NotBlank:
- Length:
min: 2
max: 255
Передал туда репозиторий пользователя и был необходим entity manager, передал его, но пишет, что такой не найден. Что-то не так передаю?
Заранее спасибо!