【问题标题】:assigning a value to form widget为表单小部件赋值
【发布时间】:2011-02-17 09:20:35
【问题描述】:

我在 _form.php 中有一个表单小部件

echo $form['catcher_id']->renderLabel();  //the label
echo $form['catcher_id']->renderError();  //the validator

symfony 创建了基类:

<?php
/**
 * LpmService form base class.
 */
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(),
     [color=#FF4000] 'catcher_id'           => new sfWidgetFormPropelChoice(array('model' => 'LpmCatcher', 'add_empty' => false)),[/color]
      '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';
  }
}

我手动重新创建了下拉列表,这样我就可以合并一个“onchange”事件:

<select name="services" onchange="refreshPage(this.form.services)" id="droplist">
           <?php
              $catcher_names = LpmCatcherPeer::getByAllNames();
              foreach($catcher_names as $row)
              {
                  ?>
                    <option value="<?php echo $row->getName()."/".$row->getId(); ?>" <?php 
                      if($row->getName() == $catcher_name) echo 'selected="selected"'?>><?php echo $row->getName();?></option>
                    <?php
              }
                  ?>
            </select> 

我如何为echo $form['catcher_id'] 分配一个值,因为现在当我从下拉列表中选择一个值并单击提交时,验证器说 catcher_id 是必需的(因为我手动创建了下拉列表),那么我该如何手动设置该值? ??

我有:

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

但它不起作用。

【问题讨论】:

    标签: php symfony1 symfony-1.4 symfony-forms


    【解决方案1】:

    我相信这应该适用于您的模板:

    <?php $form->setDefault('catcher_id', 123) ?>
    

    ...或者在你的行动中:

    $this->form->setDefault('catcher_id', 123);
    

    【讨论】:

    • 不,恐怕我仍然得到验证器说 catcher_id 是必需的
    【解决方案2】:

    我不会重新编码选择,而是使用选择的 ID 并在 Javascript 中绑定事件:

    function init()
    {
      var el_to_bind = document.getElementById( 'lpm_service_catcher_id' );
      el_to_bind.onchange = my_onchange_handler;
    }
    
    function my_onchange_handler( el )
    {
      // do your stuff here
    }
    

    或者,使用 jQuery,

    $('#lpm_service_catcher_id').change( function() {
      // do your stuff here
    });
    

    【讨论】:

      【解决方案3】:

      您需要使您手动创建的 select 的名称与 symfony 生成的名称匹配。

      假设 lpm_service 是您的表单的 getName() 调用返回的值,并且您使用默认名称格式,则选择的名称需要为 lpm_service[catcher_id]

      【讨论】:

        【解决方案4】:

        问题是您像验证器一样为选择分配不同的值。您正在使用:

        <select>
        <option value='name/id' ...>
        </select>
        

        验证器只搜索 id

        $this->setValidators(array(
        ...
              'catcher_id' => new sfValidatorPropelChoice(
                              array('model' => 'LpmCatcher', 'column' => 'id')),
        ...
            ));
        

        这就是验证器失败并且没有为您设置默认值的原因。

        所以我真正要做的是使用默认渲染,用

        设置值
        $this->form->setDefault('catcher_id', 123);
        

        Tom 所说的那样,并像 yitznewton 所说的那样附加 onchangehandler。

        【讨论】:

          【解决方案5】:

          您不必手动执行此操作。您可以在小部件上设置所需的任何属性。您应该在LpmServiceForm 中执行此操作

          $this->getWidget('catcher_id')->setAttribute('onchange', 'refreshPage(this.form.services)');
          

          并像往常一样呈现表单。

          【讨论】:

            猜你喜欢
            • 2014-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多