[英]Symfony3.4 : dynamic registration form
我对Symfony有点陌生,所以我在动态表单方面颇为挣扎。 我正在尝试将用户保存在数据库中,并在提交表单时对他们的地址进行地址解析。
这是我的用户实体:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Veuillez inscrire votre prénom.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=255,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=255,nullable=true)
*/
protected $lastname;
/**
* @var string
*
* @ORM\Column(name="address", type="string", length=255, nullable=true)
*/
protected $address;
/**
* @var string
*
* @ORM\Column(name="zipcode", type="string", length=10, nullable=true)
*/
protected $zipcode;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255, nullable=true)
*/
protected $city;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=255, nullable=true)
*/
protected $country;
/**
* @var string
*
* @ORM\Column(name="latitude", type="string", length=255, nullable=true)
*/
protected $latitude;
/**
* @var string
*
* @ORM\Column(name="longitude", type="string", length=255, nullable=true)
*/
protected $longitude;
public function __construct()
{
parent::__construct();
$this->enabled = false;
$this->roles = array();
$this->inscriptiondate = new \Datetime();
}
/**
* Set firstname
*
* @param String $firstname
* @return User
*/
public function setFirstName($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get lastname
*
* @return String
*/
public function getLastName()
{
return $this->lastname;
}
/**
* Set lastname
*
* @param String $lastname
* @return User
*/
public function setLastName($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Set address
*
* @param string $address
*
* @return User
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set zipcode
*
* @param string $zipcode
*
* @return User
*/
public function setZipCode($zipcode)
{
$this->zipcode = $zipcode;
return $this;
}
/**
* Get zipcode
*
* @return integer
*/
public function getZipCode()
{
return $this->zipcode;
}
/**
* Set city
*
* @param string $city
*
* @return User
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set country
*
* @param string $country
*
* @return User
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set latitude
*
* @param string $lat
*
* @return User
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* @return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* @param string $longitude
*
* @return User
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* @return string
*/
public function getLongitude()
{
return $this->longitude;
}
}
这是我的注册表:
<?php
// src/AppBundle/Form/RegistrationType.php
namespace AppBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use UserBundle\Repository\GenderRepository;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('firstname', TextType::class, array(
'label' => 'First name'))
->add('lastname', TextType::class, array(
'label' => 'Last name'))
->add('address')
->add('zipcode')
->add('city')
->add('country');
$builder->addEventListener(
FormEvents::SUBMIT,
function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getForm()->getData();
$address = $data->getAddress();
$zipcode = $data->getZipCode();
$city = $data->getCity();
$country = $data->getCountry();
$fulladdress = urlencode($address)."+".$zipcode."+".$city."+".$country;
$geocode = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$fulladdress.'&key=KEY');
$output = json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
dump($fulladdress); // Works fine, I see full address in dump
$form->add('latitude', HiddenType::class, array('data' => $latitude))
->add('longitude', HiddenType::class, array('data' => $longitude)); // Does not save data in database
}
);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
}
对于第一个测试,我在$ builder-> addEventListener(FormEvents :: PRE_SET_DATA,function(FormEvent $ event))中编写了一个静态地址,它工作正常,因此地理编码看起来不错。
当我尝试从表单输入中动态获取地址并显示表单时,我得到了: 错误:在null上调用成员函数getAddress()
看起来像$ address = $ data-> getAddress(); 返回NULL,因为在查看FormEvents :: PRE_SET_DATA时尚未提交表单 。 我尝试玩其他事件,但无法使其正常工作。
当我尝试将其放入FormEvents :: PRE_SUBMIT时 ,窗体显示正常,但在Submit上得到: 错误:调用数组中的成员函数getAddress()
当我尝试将其放在FormEvents :: SUBMIT中时 ,表单显示正常,提交正常,没有错误,但是纬度和经度列在数据库中为NULL。
我也尝试过$ address = $ data ['address']; 而不是$ address = $ data-> getAddress(); ,但没有更好。
这只是我的第二个symfony项目,所以不是专家。 我想念什么? 任何帮助将不胜感激。
问题在那里:
$form = $event->getForm();
$data = $form ->getData();
您应该从表单加载数据。
您为该地址获得的错误并不意味着GetAddress()
返回null
而是$data
为null
并且您无法访问null
变量的成员。
编辑:
通过与评论中相同的链接:
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$sport = $event->getForm()->getData();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.