Php 为表单小部件赋值
我在_form.php中有一个表单小部件Php 为表单小部件赋值,php,symfony1,symfony-1.4,symfony-forms,Php,Symfony1,Symfony 1.4,Symfony Forms,我在_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 BaseFo
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';
}
}
但它不起作用。我认为这应该适用于您的模板:
<?php $form->setDefault('catcher_id', 123) ?>
我相信这应该适用于您的模板:
<?php $form->setDefault('catcher_id', 123) ?>
我将使用select的ID并用Javascript绑定事件,而不是重新编码select:
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
});
我将使用select的ID并用Javascript绑定事件,而不是重新编码select:
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
});
您需要使手动创建的select名称与symfony生成的名称匹配
假设lpm_服务是表单的
getName()
调用返回的值,并且使用默认名称格式,则select的名称需要是lpm_服务[catcher\u id]
,您需要使手动创建的select名称与symfony生成的名称匹配
假设lpm_服务是表单的
getName()
调用返回的值,并且您使用默认的名称格式,select的名称需要是lpm_服务[catcher\u id]
,问题是您正在像验证器那样为select分配不同的值。您正在使用:
<select>
<option value='name/id' ...>
</select>
这就是验证程序失败的原因,它没有为您设置默认值
所以我真正要做的是使用默认渲染,使用
$this->form->setDefault('catcher_id', 123);
像Tomsaid一样附加onchangehandler,像yitznewtonsaid一样。问题在于,您正在像验证器一样为select分配不同的值。您正在使用:
<select>
<option value='name/id' ...>
</select>
这就是验证程序失败的原因,它没有为您设置默认值
所以我真正要做的是使用默认渲染,使用
$this->form->setDefault('catcher_id', 123);
像汤姆所说的那样,连接onchangehandler,像牛顿所说的那样。你不必手动操作。您可以在小部件上设置任何想要的属性。您应该在
LpmServiceForm
$this->getWidget('catcher_id')->setAttribute('onchange', 'refreshPage(this.form.services)');
然后像往常一样呈现表单。您不必手动执行此操作。您可以在小部件上设置任何想要的属性。您应该在
LpmServiceForm
$this->getWidget('catcher_id')->setAttribute('onchange', 'refreshPage(this.form.services)');
然后像往常一样提交表单。不,恐怕我仍然收到验证器说需要catcher\u id不,恐怕我仍然收到验证器说需要catcher\u id