我对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写了一个静态地址-
当我试图从表单输入动态获取地址并显示表单时,我得到了:错误:调用空的成员函数get地址()
看起来像$地址=$data-
当我试图把它放在FormEvents::PRE_SUBMIT,表单显示罚款,但提交我得到:错误:调用数组上的成员函数get地址()
当我尝试将其放入FormEvents::SUBMIT时,表单显示良好,提交正常,并没有错误,但数据库中的纬度和经度列为空。
我也尝试了$地址=$数据['地址'];而不是$地址=$data-
这只是我的第二个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();