0
votes

Is it possible to change the contents of a form dropdown that is part of a form collection that is populated using propel but the data is not mapped. Example of code to get the data below:

AddressType:

public function buildForm(FormBuilderInterface $builder, array $options){

   $builder->
      ->add("addressOne", new addressOneType()),
      ->add("addressTwo", new addressTwoType(), array(
         "required" => false,
       )),
}

addressOneType:

public function buildForm(FormBuilderInterface $builder, array $options){

   $builder->
      ->setMethod('POST')
      ->add('Country', 'model', array(
          'mapped' => 'bundle\nameBundle\Model\Countries',
          'required' => true,
          'multiple' => false,
          'expanded' => false,
          'property' => 'label',
          'query' => CountryQuery::create()->find(),
       ))
       ->getForm();
}

This collection is used for a particular part of an application however in this part in need to call a service from the form itself. Would this be possible as I've tried to extend the ContainerInterface and declare this inside of the construct method however this just throws an error.

However, I beleive this to be due to the fact that the form builder is not declared as a service.

Is there an easier way of changing the data of the drop down menu by injecting a new model to override the original. For example:

$form = $this->createForm(new AddressType());

$newData = CountriesQuery::create()
           ->orderBy("different_field");

$form['collectionName']['fieldname']->setData($newData);

Doing the above doesn't change or override the original model that is changing the data. With or without the ->find() at the end of the $newData field.

Does anyone know of a way to overwrite the data set by the model?

1

1 Answers

0
votes

A very simple way for pass specific options to form is in constructor ...

class addressOneType
{
    protected  $countryQuery;
    public function __constructor( $countryQuery = null )
    {
        $this->countryQuery = $countryQuery;
    }

    public function buildForm(FormBuilderInterface $builder, array $options){
         $query = $this->countryQuery ? $this->countryQuery : 
              CountryQuery::create();

          $builder
             ->setMethod('POST')
             ->add('Country', 'model', array(
                 'mapped' => 'bundle\nameBundle\Model\Countries',
                 'required' => true,
                 'multiple' => false,
                 'expanded' => false,
                 'property' => 'label',
                 'query' => $query->find(),
              ))
       ->getForm();
   }
}

... and you can call to form in this way ...

$cQuery = CountriesQuery::create()->orderBy("different_field");
$form = $this->createForm(new AddressType($cQuery));