sfWidgetFormPropelChoice 导致 500 错误(缺少参数?)

问题描述 投票:0回答:1

我有以下代码:

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(); 
              } 
            ?> 

我的问题出在这行:

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

它不起作用。我收到一个错误。如果输入了服务代码,我想将 catcher 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'))) 
php symfony1 symfony-1.4
1个回答
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));
© www.soinside.com 2019 - 2024. All rights reserved.