Problems with Symfony embedded forms -
i trying achieve following scenario:
auction , category entities (many-to-one). category entity has one-to-many relationship categoryattribute entity allows unlimited number of attributes of various types added category. let's cars category have make , year attributes. categoryattribute entity has widgettype property defines how render attribute (input, select etc.), attributevalues data attribute (populating select etc.) , isrequired property tell whether property required or not. far good. managing attributes piece of cake but:
on auction side of things want when user selects given category list render attributes category filled in. translated related auctionattribute entity (many-to-one auction in attributes property in class). auctionattribute has reference categoryattribute, attributevalue hold input or selected value.
well, whole ajax request , fill in of attributes selected category not problem. problem(s) arise when submit form. there 2 issues.
how bind attributes part of form actual form validation. let's have car category selected , make attribute required, how validate attribute?
how bind attributes input auctionattribute entity in form?
i know embedded forms need hook formevents::pre_submit event not sure how transform attribute entity in there.
in terms of code, have following:
when getting attributes category, create auctionattributeformtype , render twig form helper , return html in ajax request:
$form = $this->createform(new type\auctionattributeformtype(), null, array('csrf_protection' => false)); foreach ($categoryattributes $attribute) { $form->add('attribute_'.$attribute->getid(), $attribute->getwidgettype(), array('label' => $attribute->getname(), 'required' => $attribute->isrequired()); }
when auction form submitted, hook pre_submit event , when whether there attribute submitted , belongs set of attributes of category far went before got stuck:
$builder->addeventlistener( form\formevents::pre_submit, function (form\formevent $event) { $auction = $event->getdata();
if (null !== $auction['category']) { $categoryattributes = $this ->repository ->findattributesforcategory($auction['category']) ->getresult(); if (count($categoryattributes) > 0) { $attribute_values = array(); foreach ($categoryattributes $attribute) { if (isset($auction['attribute_'.$attribute->getid()])) { $attribute_values[$attribute->getid()] = $auction['attribute_'.$attribute->getid()]; } } } } }
);
i need values attribute_values array auctionattribute entities bound auction entity. idea how achieved. think should done through kind of data transformer not sure transform data - should form->add field, or directly touch auction entity filled in data.
any suggestions?
edit:
i made work use of model transformer there problem, when editing record, if there more 1 attribute, first 1 populated data. here sample gist of code:
my suggestion not convert submitted data via event listeners, use data transformer, attach form field so:
$formbuilder->add( $formbuilder ->create('field_name', 'field_type', [ ... field_options ... ]) ->addmodeltransformer(new somemodeltransformer()) )
and "somemodeltransformer" class should this:
class seatingtonumbertransformer implements datatransformerinterface { /** * transforms object norm data model data * norm data field value. have integer field, $normdataobject int. * in case: need instantiate several new auctionattribute objects , persist them maybe */ public function transform($normdataobject) { $transformedobject = $this->sometransformaction($normdataobject); return $transformedobject; } /** * reverts transform * in case: auctionattribute int */ public function reversetransform($modeldataobject) { $transformedobject = $this->someothertransformaction($modeldataobject); return $transformedobject; } }
more info can found here
if need more help, let me know.
Comments
Post a Comment