为表单小部件分配一个值

发布于 2024-10-17 23:53:11 字数 3570 浏览 7 评论 0原文

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

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

创建了基类:

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

但它不起作用。

i have a form widget in _form.php

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

symfony created the base class:

<?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';
  }
}

and i re-created the dropdown list manually so i can incorporate a "onchange" event:

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

how can i assign a value to echo $form['catcher_id'] because now when i select a value from the dropdown and click submit the validator says that catcher_id is required(because i created dropdown manually), so how can i set the value manually???

i have:

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

But it's not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

权谋诡计 2024-10-24 23:53:11

我相信这应该在您的模板中起作用:

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

...或在您的操作中:

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

I believe this should work in your template:

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

... or in your action:

$this->form->setDefault('catcher_id', 123);
挽袖吟 2024-10-24 23:53:11

我不会重新编码选择,而是使用选择的 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
});

Instead of recoding the select, I would use the select's ID and bind the event in 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
}

Or, with jQuery,

$('#lpm_service_catcher_id').change( function() {
  // do your stuff here
});
千鲤 2024-10-24 23:53:11

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

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

You need to make your manually created select's name match the one that would've been generated by symfony.

Assuming lpm_service is the value returned by your form's getName() call, and that you use the default name format, the name of the select needs to be lpm_service[catcher_id].

萌无敌 2024-10-24 23:53:11

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

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

验证器仅搜索 id

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

这就是验证器失败的原因,并且它不会为您设置默认值。

所以我真正要做的是使用默认渲染,像 Tom 所说的那样设置值

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

,并像 yitznewton 所说的附加 onchangehandler 。

The problem is that you're assigning distinct values to the select as the validator does. You are using:

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

And the validator searchs only for id

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

Thats why the validator is failing and it doesn't sets you the default value.

So what I would really do is use the default render, set the value with

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

like Tom said and attach the onchangehandler like yitznewton said.

手心的海 2024-10-24 23:53:11

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

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

并像往常一样呈现表单。

You don't have to do that manually. You can set any attribute you want on a widget. You should do this in LpmServiceForm

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

and render the form as usual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文