我知道symfony 1.4 Form系统不是最好的,给很多人带来了很多麻烦,但是我需要完成这个任务,我需要帮助。
我需要将子表单嵌入到嵌入在主表单中的子表单中,并通过Ajax实现。
基本上我有aForm, bForm和cForm和aForm嵌入bForm和bForm可以有一个或多个cForm。
当我嵌入他们通常从配置()函数的每个形式它的工作原理。但是当我尝试使用Ajax将多个cform嵌入到bForm中时,我无法绑定它们。
这是有效的版本。一切都是嵌入ok和验证ok。
class BookingForm extends BaseBookingForm
{
public function configure()
{
$this->embedRelation('Journey as journey');
}
}
[...]
class JourneyForm extends BaseJourneyForm
{
public function configure()
{
$pickup_form = new JourneyItineraryForm();
$this->embedForm('pickup_form', $pickup_form);
}
}
[...]
class JourneyItineraryForm extends BaseJourneyItineraryForm
{
public function configure()
{
}
}
[...]
现在,如果我尝试通过ajax嵌入JourneyItineraryForm,我设法在模板中显示小部件,但它无法绑定它们。它告诉我意外的额外表单字段名为"waypoint"
查看下面代码:
class JourneyForm extends BaseJourneyForm
{
public function configure()
{
$waypoint_form = new JourneyItineraryForm();
$this->embedForm('waypoint', $waypoint_form);
}
public function addNewWaypoint($number)
{
/*
* Called from actions.class.php after an ajax request
*/
$new_waypoints = new BaseForm();
for($i=0; $i <= $number; $i+=1)
{
$waypoint = new JourneyItinerary();
$waypoint_form = new JourneyItineraryForm($waypoint);
$new_waypoints->embedForm($i,$waypoint_form);
}
$this->embedForm('waypoint', $new_waypoints);
}
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
$new_occurrences = new BaseForm();
foreach($taintedValues['waypoint'] as $key => $new_occurrence)
{
$occurrence = new JourneyItinerary();
$occurrence_form = new JourneyItineraryForm($occurrence);
$new_occurrences->embedForm($key,$occurrence_form);
}
$this->embedForm('waypoint',$new_occurrences);
parent::bind($taintedValues, $taintedFiles);
}
}
在我的模板中,我设法显示像
这样的路点小部件$form['journey']['waypoint'][0]['field_name']->renderRow();
我还试图覆盖BookingForm的bind方法,但我不知道我是否做对了:
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
$new_occurrences = new sfForm();
foreach($taintedValues['journey']['waypoint'] as $key => $new_occurrence)
{
$occurrence = new JourneyItinerary();
$occurrence_form = new JourneyItineraryForm($occurrence);
$new_occurrences->embedForm($key,$occurrence_form);
}
$this->embedForm('journey',$new_occurrences);
parent::bind($taintedValues, $taintedFiles);
}
我遵循这个教程:http://tech.cibul.net/embedded-forms-with-symfony-1-4-and-jquery/并阅读官方文档http://symfony.com/legacy/doc/more-with-symfony/1_4/en/06-Advanced-Forms
任何帮助都非常感谢:)谢谢。
你可以试试这个插件ahDoctrineEasyEmbeddedRelationsPlugin,它是简单和快速的。但你不能让嵌入嵌入,但在这种情况下,你有一个表单2嵌入表单。