Php 实体传递到表单时的TransformationFailedException
我正在使用Symfony 3.3,当我加载我的个人资料页面时,我收到了这个TransformationFailedException错误: 无法转换属性路径“postalcode”的值:应为数字 数据库中此用户的postalcode值为: 34125abc UserProfile实体中定义的postalcode属性:Php 实体传递到表单时的TransformationFailedException,php,forms,symfony,validation,entity,Php,Forms,Symfony,Validation,Entity,我正在使用Symfony 3.3,当我加载我的个人资料页面时,我收到了这个TransformationFailedException错误: 无法转换属性路径“postalcode”的值:应为数字 数据库中此用户的postalcode值为: 34125abc UserProfile实体中定义的postalcode属性: /** * @ORM\Column(type="string") */ private $postalcode; 我的配置文件控制器: class ProfileContr
/**
* @ORM\Column(type="string")
*/
private $postalcode;
我的配置文件控制器:
class ProfileController extends Controller{
/**
* @Route("/edit_profile", name="edit_profile")
*/
public function profileAction(Request $request){
$profile = $this->getDoctrine()->getManager()->getRepository('AppBundle:UserProfile')->findOneBy(['user_id' => $this->getUser()->getUserId()]);
// If no UserProfile exists, create a UserProfile Object to insert it into database after POST
if(null === $profile){
$profile = new UserProfile();
$profile->setUserId($this->getUser()->getUserId());
}
$form = $this->createForm(EditProfileFormType::class);
$form->setData($profile);
// only handles data on POST
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$result = $this->forward('AppBundle:API\User\Profile:update_profile', array(
'profile' => $profile
));
if(200 === $result->getStatusCode()){
$this->addFlash('success', "Profile successfully created!");
}
}
return $this->render(':User/Profile:edit_profile.html.twig', [
'EditProfileForm' => $form->createView(),
]);
}
}
我的EditProfileFormType:
class EditProfileFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', ChoiceType::class, array(
'choices' => array(
'Mr' => 'Mr',
'Mrs' => 'Mrs'
)
))
->add('firstName')
->add('lastName')
->add('street')
->add('postalcode', NumberType::class)
->add('city')
->add('telephone')
->add('mobile')
->add('company')
->add('birthday' , BirthdayType::class)
->add('callback', CheckboxType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\UserProfile',
'validation_groups' => array('edit_profile')
]);
}
public function getBlockPrefix()
{
return 'app_bundle_edit_profile_form_type';
}
}
所以这里的问题似乎是数据库中的字符串值不是数字
34125abc
存储在
$profile
实体对象中,并通过$form->setData($profile)传递给表单
因此,当设置数据时,由于此行中的Numbertype->add('postalcode',Numbertype::class)
而引发错误。提交表单时,是否有方法将postalcode值传递给表单,即使它不是数字且仅检查Numbertype?因为当我将数据传递给表单时,我不需要验证。就在这时,它被提交了。解决方案非常简单,但很难找到。。我变了
->添加('postalcode',NumberType::class)
到
->添加('postalcode',TextType::class)
在我的EditProfileFormType.php中<为什么
因为表单生成器只需要知道数据库中字段的类型。在这种情况下,它不应该关心它是否是数字,因为这是模型约束的任务。在本例中,它是一个字符串,因此在表单中是文本类型。设置表单时应用所有表单类型,但提交表单时仅验证验证组!这正是他们应该做的 您可以验证控制器中的postalcode,并从form type类中删除限制。我必须验证每个控制器中的所有表单字段。那不值得。没有别的办法吗?我想在模型中而不是表单中进行限制。但是我得到了同样的错误。在这样的模型中:
/***@ORM\Column(type=“string”)*@Assert\type(*type=“numeric”,*groups={“edit_profile”}*)*/private$postalcode代码>