请sfWidgetFormPropelChoice新手问题帮助

时间:2011-02-14 14:10:34

标签: php symfony1 symfony-1.4

我有以下代码:

abstract class BaseLpmServiceForm extends BaseFormPropel 
{ 
  public function setup() 
  { 
    $this->setWidgets(array( 
      'id'                   => new sfWidgetFormInputHidden(), 
      'name'                 => new sfWidgetFormInputText(), 
      'wap_home'             => new sfWidgetFormInputText(), 
      'call_center_number'   => new sfWidgetFormInputText(), 
      'catcher_id'           => new 
sfWidgetFormPropelChoice(array('model' => 'LpmCatcher', 'add_empty' => 
false)), 
      'price_description'    => new sfWidgetFormInputText(), 
      'logo'                 => new sfWidgetFormInputText(), 
      'invalid_msisdn_text'  => new sfWidgetFormInputText(), 
      'terms_and_conditions' => new sfWidgetFormInputText(), 
      'service_code'         => new sfWidgetFormInputText(), 
    )); 
    $this->setValidators(array( 
      'id'                   => new sfValidatorChoice(array('choices' 
=> array($this->getObject()->getId()), 'empty_value' => $this- 
>getObject()->getId(), 'required' => false)), 

      'name'                 => new 
sfValidatorString(array('max_length' => 64, 'required' => false)), 
      'wap_home'             => new 
sfValidatorString(array('max_length' => 256, 'required' => false)), 
      'call_center_number'   => new 
sfValidatorString(array('max_length' => 13, 'required' => false)), 
      'catcher_id'           => new 
sfValidatorPropelChoice(array('model' => 'LpmCatcher', 'column' => 
'id')), 
      'price_description'    => new 
sfValidatorString(array('max_length' => 128, 'required' => false)), 
      'logo'                 => new 
sfValidatorString(array('max_length' => 255, 'required' => false)), 
      'invalid_msisdn_text'  => new 
sfValidatorString(array('max_length' => 255, 'required' => false)), 
      'terms_and_conditions' => new 
sfValidatorString(array('max_length' => 750, 'required' => false)), 
      'service_code'         => new 
sfValidatorString(array('max_length' => 3, 'required' => false)), 
    )); 
    $this->widgetSchema->setNameFormat('lpm_service[%s]'); 
    $this->errorSchema = new sfValidatorErrorSchema($this- 
>validatorSchema); 

    parent::setup(); 
  } 
  public function getModelName() 
  { 
    return 'LpmService'; 
  } 
} 

然后我在_form.php中有以下片段:

<th><?php echo $form['catcher_id']->renderLabel(); 
              $catcher_id = $form->getObject()->getCatcherId(); 
              $catcher = LpmCatcherPeer::getByCatcherId($catcher_id); 
              $catcher_name = $catcher->getName(); 
        ?></th> 
        <td> 
          <?php echo $form['catcher_id']->renderError(); 
          ?> 
           <!-- <input type="hidden" name="$value" id="$value"/> --> 
           <select name="services" 
onchange="refreshPage(this.form.services)" id="droplist"> 
           <?php 
              //echo $form['catcher_id']; 
              //echo var_dump($_GET($form['catcher_id'])); 
              //echo var_dump($_POST($form['catcher_id'])); 
              //$catcher_names =  explode(" ",$form['catcher_id']); 
              $catcher_names = LpmCatcherPeer::getByAllNames(); 
              foreach($catcher_names as $row) 
              { 
                  ?> 
                    <option value="<?php echo $row->getName();?>" <? 
php 
                      if($row->getName() == $catcher_name) echo 
'selected="selected"';?>><?php echo $row->getName();?></option> 
                    <?php 
              } 
                   ?> 
            </select> 
            <?php 
              //$catcher_names =  explode(" ",$form['catcher_id']); 
              //$user = ""; 
              //$message = '<script type="text/ 
javascript">refreshPage("'.$user.'")</script>'; 
              //echo $message; 
              $form['service_code']->getWidget()- 
>setAttribute('disabled', 'true'); 

              echo $form['service_code']->renderLabel(); 
              echo $form['service_code']->renderError(); 
              echo $form['service_code']; 
              $service_code = $form['service_code']->getValue(); 
              if ($service_code != null) 
              { 
                  $catcher = LpmServicePeer::getByName($form['name']- 
>getValue()); 

                  $catcher->setCatcherId(11); 
                  $catcher->setServiceCode($service_code); 
                  $form['catcher_id']->getWidget()- 
>setAttribute('value','"11"'); 

                  $form->save(); 
              } 
            ?> 

我的问题在于:     $形式[ 'catcher_id'] - &GT; getWidget() - &GT;的setAttribute( '值', ' “11”');它 不工作我得到一个错误.. 如果输入了服务代码,我想将捕手ID设置为该值 11 错误是:

500 | Internal Server Error | sfValidatorErrorSchema 
catcher_id [Required.] 

还有:

 $errorSchema = new sfValidatorErrorSchema($this); 
    // check that post_max_size has not been reached 
    if (isset($_SERVER['CONTENT_LENGTH']) && (int) 
$_SERVER['CONTENT_LENGTH'] > $this- 
>getBytes(ini_get('post_max_size'))) 

任何人都可以帮助我,我绝望了 非常感谢

1 个答案:

答案 0 :(得分:1)

您需要在控制器中使用setDefault而不是setValue

$this->form = new BaseLpmServiceForm();
$this->form->setDefaults(array('service_code' => 11));

我知道在symfony 1.4中你可以将默认值传递给构造函数。

$this->form = new BaseLpmServiceForm(array('service_code' => 11));