带选项解析器的symfony验证表单

时间:2018-10-26 10:12:51

标签: symfony

尝试验证拼音形式会引发以下错误。

该问题可能是由使用选项解析器的国家/地区字段引起的。

之所以这样说,是因为消除了国家,错误就消失了。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, array(
            'required' => true,
        ))
        ->add('phone', TextType::class, array(
            'required' => false,
        ))
        ->add('email', TextType::class, array(
            'required' => false,
        ))
        ->add('info', TextType::class, array(
            'required' => false,
        ))
        ->add('address_adv', TextType::class, array(
            'required' => false,
            'mapped' => false
        ))
        ->add('country', ChoiceType::class, array(
            'required' => false,
            'multiple' => false,
            'empty_data'  => null,
            'choices'  => $options['countries_1'],
            'choices_as_values' => false,
        ))

        ->add('countries', CountryAgencyType::class, array('multiple' => true,
        ))

        ->add('region', ChoiceType::class, array(
            'expanded' => true,
            'required' => false,
            'multiple' => true,
            'empty_data'  => null,
            'choices'  => $this->getChoicesForRegion($options['regions']),
            'data' => $this->checkedChoicesForRegion($options['regions'], $options['selectedRegions']),
            'choices_as_values' => false,
        ))
    ;
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Agency',
        'countries_1' => array(),
        'regions' => array(),
        'selectedRegions' => array(),
    ));

}

public function getChoicesForRegion($regionsList){
    $regions = array();
    foreach($regionsList as $region){
        array_push($regions, $region);
    }

    return $regions;
}

public function checkedChoicesForRegion($regionsList, $selectedRegions){
    $regions = array();

    for($i = 1;$i <= count($regionsList);$i++){
        for($j = 0;$j < count($selectedRegions);$j++){
          if($selectedRegions[$j] == $regionsList[$i]){
              array_push($regions, ($i - 1));
          }
        }
    }

    return $regions;
}

我不明白为什么交响乐会阻止我。

Symfony错误屏幕截图:

symfony error screenshot

该错误可能是由于学说中实体的错误实施导致的?

控制器:

public function createAction(Request $request)
{
    $addressNum = $request->get('address_num');
    $addressSimple = $request->get('address_simple');
    $zipcode = $request->get('zipcode');
    $district = $request->get('district');
    $province = $request->get('province');
    $region = $request->get('region');
    $country = $request->get('country');
    $lat = $request->get('lat');
    $lng = $request->get('lng');

    $countries_1 = $this->get('app.utility')->getCountries();
    $regions = $this->get('app.utility')->getRegions();

    $agency = $this->get('app.entity.agency')->getNewEntity();

    $form = $this->createForm(AgencyForm::class, $agency, ['countries_1' => $countries_1, 'regions' => $regions])->handleRequest($request);

    if ($form->isValid()) {


        if (empty($addressSimple) || empty($zipcode) || empty($district) || empty($province))
        {
            $agency->setAddress($form->get("address_adv")->getData());
        }
        else
        {
            $agency->setAddress($addressSimple.", ".$addressNum);
            $agency->setZipcode($zipcode);
            $agency->setDistrict($district);
            $agency->setProvince($province);
            $agency->setCountry($country);

        }
        $agency->setLatitude($lat);
        $agency->setLongitude($lng);

        $arrayIds = $form->get("region")->getData();

        $region = "";
        $counter = 0;
        foreach($arrayIds as $arrayId){
            if($counter != 0){
                $region .= ',';
            }
            $region .= $this->get('app.utility')->getRegion($arrayId+1);

            $counter++;
        }

国家/地区型号:

/**
 * @var integer
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string $iso
 *
 * @ORM\Column(name="iso", type="string", length=2)
 */
protected $iso;

/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=128)
 */
protected $name;

/**
 * Returns a string representation of the entity.
 *
 * @return string
 */
public function __toString()
{
    return (string)$this->id;
}

/**
 *
 * @ORM\ManyToMany(targetEntity="Agency", inversedBy="countries", cascade={"persist", "merge"})
 */
protected $agency;

public function getName()
{
    return $this->name;
}

代理商模型:

/**
 * @param string $country
 * @return Agency
 */
public function setCountry($country)
{
    $this->country = $country;
    return $this;
}

/**
 * @return string
 */
public function getCountry()
{
    return $this->country;
}


/**
 * Add countries
 *
 * @param \AppBundle\Entity\Countries $countries
 * @return Agency
 */
public function addCountries(Technology $countries)
{
    $this->countries[] = $countries;

    return $this;
}

/**
 * Remove countries
 *
 * @param \AppBundle\Entity\Countries $countries
 */
public function removeTechnology(Technology $countries)
{
    $this->countries->removeElement($countries);
}

/**
 * Get countries
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getCountries()
{
    return $this->countries;
}

/**
 * Get countries
 *
 * @param string $lang
 * @param string $sep
 *
 * @return string
 */
public function getCountriesStr($lang, $sep = ',')
{
    $result = "";
    foreach ($this->countries as $i => $tech)
    {
        $result .= $i > 0 ? $sep : '';
        $result .= $tech->getName();
    }

    return $result;
}

0 个答案:

没有答案