[英]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.