We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Form embedding

How I can embed some form, for example, based on some Option model entity to another form, based on Product entity?



7.9k

You can setup the required relation with the options in the Products model (see the topics about Model and Volt in the documentation)

I'm about something else. Okay, try to describe another example. I have model named <News>, it has only <id> and <created> field. And i have model named like <NewsTranslation>, which contains multilanguage field, such as <title>, <description>, <content> etc. Question: Can I embed <NewsTranslation> as sub-form into <News> form for further processing as <News> object with embedded relations? The result that I want to get, something like this:

$news = new News();
$news->created = '2013-06-17';
$news->translation['en']->title = 'My Title';
$news->translation['en']->description = 'My Description';
$news->translation['de']->title = 'Meine Titel';
$news->translation['de']->description = 'Meine Beschreibung';

this chapter https://symfony.com/legacy/doc/jobeet/1_4/en/19?orm=Doctrine#chapter_19_sub_admin_generator describes what i mean



7.9k

Yes, there are two ways (I can think of):

  1. create a json field (normal text field in the table) and store inside all the translations (the bad part: you will have to decode it on load an encode it on save)
  2. create translations table in db and use:
    $news->getTranslation(" lang = 'en' ")->title

    (the bad part: you will have a lot more queries)

You choose which is best for you.

Damn >.< You don't understand what I need. I want to collect many forms in one form as sub-forms %)



7.9k

Ok but how will you represent the data in the view? If $sub1 and $sub2 are two separate forms you can create them as explained here: https://docs.phalcon.io/en/latest/reference/forms.html (by the way in HTML you cannot have subforms - one form inside of the other)

You are mistaken. Embedded forms could be simulated with element name format. For example:

<input name="news[en][title]" type="text" />

This will return associative array with correct structure

edited Apr '14

What Dmitry Korniychuk is asking for is this kind of behavior:

$sub1 = $form->addContainer('first');

$sub1->addText('name[]', 'Your name:');

$sub1->addText('email[]', 'Email:');

$sub2 = $form->addContainer('second');

$sub2->addText('name[]', 'Your name:');

$sub2->addText('email[]', 'Email:');

This will allow you to create via javascript new form set, validate it where name(first) and name(second) are tereated as different elements, and submit the form, which will result in two database row entries. Unfortunately so far i cannot find solution in phalconphp framework but this is definitely a real life scenario, so if any one can help, pelase you are welcome!

So was this ever solved? This is a pretty trivial use of forms..