When a form is binded to some values, if the validation fail, all
cleaned values are lost.

Example :

MyForm extends sfForm
{
  public function setup()
  {
    $this->widgetSchema = new sfWidgetFormSchema(array(
      'contract'    => sfWidgetFormSelect(array('choices' => new
sfCallable($this, 'getContracts'))),
      'people'   => sfWidgetFormSelectMany(array('choices' => new
sfCallable($this, 'getPeople')))
    ));

    $this->validatorSchema = new sfValidatorSchema(array(
      'contract'  => new sfValidatorChoice(array('choices' => new
sfCallable($this, 'getContractsKeys'))),
      'people'    => new sfValidatorChoiceMany(array('choices' => new
sfCallable($this, 'getPeopleKeys')))
    ));

    $this->widgetSchema->setNameFormat('form[%s]');
  }

  public function getContracts()
  {
    sfPropelFinder::from('Contract')->find();
  }

  public function getContractsKeys()
  {
    return array_keys($this->getContracts());
  }

  public function getPeople()
  {
    $contract = null;
    if (isset($this->taintedValues['contract']))
    {
      $contract = $this->taintedValues['contract'];
      // We have to validate the contract, even if it has already be
done, because cleaned values are lost ...
      $validator = new sfValidatorChoice(array('choices' => new
sfCallable($this, 'getContractsKeys')));
      try {
        $contract = $validator->clean($contract);
      } catch (sfValidatorError $e) {
        $contract = null;
      }
    }
    else if (isset($this->defaults['contract']))
    {
      $contract = $this->defaults['contract'];
      // we guess that this is a valid value :)
    }

    if ($contract != null)
    {
      $people = sfPropelFinder::from('People')->where('ContractId',
$contract)->find();
      $list = array();

      foreach ($people as $person)
      {
        $list[$person->getId()] = $people->getFullName();
      }
    }
    else
    {
      return array("Please choose a contract");
    }
  }

  public function getPeopleKeys()
  {
    return array_keys($this->getPeople());
  }
}

To re-render the form after a post, the validator for contract is
executed twice, and the validator for peope is executed one time.
Now imagine a lot of drop down list, all dependant each-other ...
argh, it does not scale ...
And in my project, I make call to webservices, so it is slower than
querying the database ...

Maybe I'm wrong.
If there is a better way to do this, could you show me an example
please ?

Otherwise, maybe it could be useful to keep cleaned values instead of
deleting them ? ...


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to