为表单小部件分配一个值
我在 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信这应该在您的模板中起作用:
...或在您的操作中:
I believe this should work in your template:
... or in your action:
我不会重新编码选择,而是使用选择的 ID 并在 Javascript 中绑定事件:
或者,使用 jQuery,
Instead of recoding the select, I would use the select's ID and bind the event in Javascript:
Or, with jQuery,
您需要使手动创建的选择名称与 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 belpm_service[catcher_id]
.问题是您像验证器一样为选择分配不同的值。您正在使用:
验证器仅搜索 id
这就是验证器失败的原因,并且它不会为您设置默认值。
所以我真正要做的是使用默认渲染,像 Tom 所说的那样设置值
,并像 yitznewton 所说的附加 onchangehandler 。
The problem is that you're assigning distinct values to the select as the validator does. You are using:
And the validator searchs only for 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
like Tom said and attach the onchangehandler like yitznewton said.
您不必手动执行此操作。您可以在小部件上设置您想要的任何属性。您应该在
LpmServiceForm
中执行此操作并像往常一样呈现表单。
You don't have to do that manually. You can set any attribute you want on a widget. You should do this in
LpmServiceForm
and render the form as usual.