2011-02-14 141 views
0

我有以下代碼:sfWidgetFormPropelChoice新手問題需要幫助,請

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'] - > getWidget() - >的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'))) 

可以ANYBODY請幫助我我絕望請 非常感謝

回答

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));