domingo, 16 de março de 2014

EmbedForm with new relation

To insert new attributes to a Product in the default form we just need those few steps:

In the productForm class create the relation with the attribute form.

public function configure() {
    /* Create embedForm with an empty attribute */
    $new_attribute = new ProductAttribute();
    $this->embedForm('new_attribute', new ProductAttributeForm($new_attribute));
    $this->embedRelation('Attributes', new ProductAttributeForm());

...and we rewrite the saveEmbeddedForms methods
public function saveEmbeddedForms($con = null, $forms = null) {
    try {
        $new_attribute_form = $this->getEmbeddedForm('new_attribute');
        $values             = $this->getValue('new_attribute');

        /* Validations Rules to save the relation */
        if (empty($values['attribute_id'])) {
    } catch (InvalidArgumentException $e) {
    return parent::saveEmbeddedForms($con, $forms);

Sem comentários:

Enviar um comentário