Symfony Optional Phone Entry validation Integrity Error: Column 'phone' cannot be null
Symfony Optional Phone Entry validation Integrity Error: Column 'phone' cannot be null
我有以下实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The surname is too short.",
* maxMessage="The surname is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $surname;
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $organization;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $occupation;
public function __construct()
{
parent::__construct();
}
public function setName(string $name)
{
$this->name=$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setSurname(string $surname)
{
$this->surname=$surname;
return $this;
}
public function getSurname()
{
return $this->name;
}
public function setPhone($phoneNum)
{
$this->phone=$phoneNum;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setOrganization($organization)
{
$this->organization=$organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
public function setOccupation($occupation)
{
$this->occupation=$occupation;
return $this;
}
public function getOccupation()
{
return $this->occupation;
}
}
如您所见,它有一个名为 $phone
:
的字段
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
按照我的逻辑,这个字段应该是可选的,而不是注册所必需的,所以为了做到这一点,我相应地扩展了 FosUserBundle 表单:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('phone',PhoneNumberType::class,["label"=>"register.phonenum",'translation_domain' => 'FOSUserBundle'])
->add('organization',TextType::class,["label"=>"register.organization","required"=>false,'translation_domain' => 'FOSUserBundle'])
->add('occupation',TextType::class,["label"=>"register.position","required"=>false,'translation_domain' => 'FOSUserBundle']);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
如您所见,我将其设置为 NotRequired,如您在以下代码段中所见:
->add('phone',PhoneNumberType::class,["label"=>"register.phonenum",'translation_domain' => 'FOSUserBundle'])
但是当我尝试测试注册时出现以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'phone' cannot be null
我也是为了避免这类错误我把定义改成了:
/**
* @ORM\Column(type="phone_number")
*/
protected $phone;
但我仍然得到错误。是否可以通过某种方式将字段设置为空?
我还使用 giggsey/libphonenumber-for-php
coposer 包进行验证。
/**
* @ORM\Column(type="phone_number", nullable=true)
* @AssertPhoneNumber
*/
protected $phone;
我相信这会奏效。
你在 Doctrine Annotation 中缺少一个参数:
/**
* @ORM\Column(type="phone_number", nullable=true)
* @AssertPhoneNumber
*/
protected $phone;
并且不要忘记 运行 d:s:u --dump-sql|--force
或生成您的迁移 ;)
我有以下实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The surname is too short.",
* maxMessage="The surname is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $surname;
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $organization;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $occupation;
public function __construct()
{
parent::__construct();
}
public function setName(string $name)
{
$this->name=$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setSurname(string $surname)
{
$this->surname=$surname;
return $this;
}
public function getSurname()
{
return $this->name;
}
public function setPhone($phoneNum)
{
$this->phone=$phoneNum;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setOrganization($organization)
{
$this->organization=$organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
public function setOccupation($occupation)
{
$this->occupation=$occupation;
return $this;
}
public function getOccupation()
{
return $this->occupation;
}
}
如您所见,它有一个名为 $phone
:
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
按照我的逻辑,这个字段应该是可选的,而不是注册所必需的,所以为了做到这一点,我相应地扩展了 FosUserBundle 表单:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('phone',PhoneNumberType::class,["label"=>"register.phonenum",'translation_domain' => 'FOSUserBundle'])
->add('organization',TextType::class,["label"=>"register.organization","required"=>false,'translation_domain' => 'FOSUserBundle'])
->add('occupation',TextType::class,["label"=>"register.position","required"=>false,'translation_domain' => 'FOSUserBundle']);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
如您所见,我将其设置为 NotRequired,如您在以下代码段中所见:
->add('phone',PhoneNumberType::class,["label"=>"register.phonenum",'translation_domain' => 'FOSUserBundle'])
但是当我尝试测试注册时出现以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'phone' cannot be null
我也是为了避免这类错误我把定义改成了:
/**
* @ORM\Column(type="phone_number")
*/
protected $phone;
但我仍然得到错误。是否可以通过某种方式将字段设置为空?
我还使用 giggsey/libphonenumber-for-php
coposer 包进行验证。
/**
* @ORM\Column(type="phone_number", nullable=true)
* @AssertPhoneNumber
*/
protected $phone;
我相信这会奏效。
你在 Doctrine Annotation 中缺少一个参数:
/**
* @ORM\Column(type="phone_number", nullable=true)
* @AssertPhoneNumber
*/
protected $phone;
并且不要忘记 运行 d:s:u --dump-sql|--force
或生成您的迁移 ;)