[英]Symfony 1.4 Custom Form
Just when I think I've got this framework down .... something that SEEMS like it should be so simple is thwarting my LIFE! 就在我认为我已经放弃这个框架的时候.... SEEMS之类的东西应该是如此简单,却阻碍了我的生活!
Here's What I got: 这是我得到的:
PageMeta:
connection: doctrine
actAs: { Timestampable: ~ }
tableName: page_meta
columns:
id:
type: integer(4)
primary: true
autoincrement: true
site_id:
type: integer(4)
notnull: true
meta_type_id:
type: integer(4)
notnull: true
page_name_id:
type: integer(4)
notnull: true
value:
type: string(65535)
default: ''
notnull: true
The relational id's in all cases are just id | 在所有情况下,关系ID都是id | name tables and are not important. 名称表并不重要。 I am filling those just fine, what I need is this. 我要填补这些就好了,我需要的是这个。
I want to create a Form with an embedded form for each page_name (there are 9 currently). 我想为每个page_name(当前有9个)创建一个具有嵌入式表单的表单。 So I created a custom form 所以我创建了一个自定义表格
class MagicForm extends BaseForm
{
public function configure()
{
CODE TO GET ALL MY NEEDED VARIABLE and MODELS
$forms = new sfForm();
$x = 0;
if(count($pageMetas) < 1)
{
foreach($pages as $page)
{
$pageMeta = new PageMeta();
$pageMeta->PageName = $page;
$pageMeta->setMetaTypeId(1);
$pageMeta->setSiteId($sid);
$pageMetas[] = $pageMeta;
}
}
foreach($pageMetas as $meta)
{
$metaForm = new PageMetaForm($meta);
$metaForm->widgetSchema['value']->setLabel($meta->PageName->getName());
$metaForm->setDefault('value', $meta->getValue());
$metaForm->widgetSchema['page_name_id'] = new sfWidgetFormInputHidden();
$forms->embedForm($x,$metaForm);
$x++;
}
$this->embedForm('TitleTags',$forms);
}
}
this works just fine, I load my form and I get the desired 9 forms with the Value field all ready to be populated. 这工作得很好,我加载了表单,并获得了所需的9个表单,其中的“值”字段都已准备好进行填充。
Here is where my issue lies, I can't get them to save! 这是我的问题所在,我无法让他们保存! I've tried processing them via binding the form and saving it, I get crsf_token errors, and others. 我尝试通过绑定表单并保存来处理它们,遇到crsf_token错误,等等。
I've tried just simply grabbing the post values and creating a new PageMeta model and simply saving it. 我已经尝试仅获取帖子值并创建一个新的PageMeta模型并保存。
i.e.
$pageMeta = new PageMeta();
$pageMeta->setXXX($request->getPostParamater(XXX);
etc;
but after initial save I am getting duplicates. 但最初保存后,我得到了重复。 And Yes I am passing the Id and yes I even tried doing the whole $pageMeta->setNew(false); 是的,我正在传递ID,是的,我什至尝试执行整个$ pageMeta-> setNew(false);。 When it's not a new value, this gives me an error: Unknown record property / related component "new" on "PageMeta" however I can comfirm PageMeta is indeed a PageMeta object.... 当它不是新值时,这会给我一个错误:未知记录属性/“ PageMeta”上的相关组件“ new”,但是我可以确认PageMeta确实是PageMeta对象。
Anyway, in the end I want to have multiple (NOT DYNAMIC I know exactly how many I need) of the same form into a single page and then save them all at once. 无论如何,最后我希望将多个相同表单的表单(不是动态的,我确切知道我需要多少个表单)放入一个页面中,然后一次将它们全部保存。
Thank you reading .. now ... GO! 谢谢你阅读..现在...开始!
Forms aren't that nice in symfony. 形式在symfony中不是很好。 For me it sounds that the crsf_token problems comes with the name oder id in the template. 对我来说,听起来crsf_token问题来自模板中的名称oder id。
eg in your lib/form/(/ ?pagemeta.php /) => $this->wigetSchema....is important!!! 例如,在您的lib / form /(/?pagemeta.php /)=> $ this-> wigetSchema ....中很重要!!!
public function configure() {
$this->setWidgets(array(
'firstname' => new sfWidgetFormInputText(array(), array('size' => '40', maxlength' => '100')),
));
$this->widgetSchema->setNameFormat('pagemeta[%s]');
}
in the template Success.php: 在模板Success.php中:
<?php include_partial('global/standardform',array('form' => $formPageMeta, 'formTarget' => $formTargetPageMeta)) ?>
and in your action.calls 并在您的action.calls中
$this->formPageMeta = new PageMetaForm(array(
'firstname' => $this->endkunde->getFirstname(),
));
$this->formTargetPageMeta = 'test/index'; //module/action
if ($request->isMethod('post')) {
$this->formPageMeta->bind($request->getParameter('pagemeta')); //getParameter is from the action module/action
if ($this->formPageMeta->isValid()) {
$formData = $this->formPageMeta->getValues();
$metaForm->widgetSchema['value']->setLabel($meta->PageName->getName());
}
}
But I do hate forms too, but I guess this is the problem that you didn't set a widgetSchema. 但是我也讨厌表单,但是我想这是您未设置widgetSchema的问题。 Hope i didn't do a mistake! 希望我没有做错!
Craphunter 猎手
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.