0

I am looking to find out how I can create a form which the definition of the form is coming from a REST API as a JSON object. The form can be 1 column, 2 columns or 10 columns.

I have been able to do this by just using the native table theming option, but I want to be able to do this with the native form options so that I can utilize all the validations and everything else etc.

Does anyone have an idea as to how I can accomplish a multi-column dynamic form?

Thanks in advance!

  • Hello and welcome. I think your question is too broad now. We can't help with whole process, from reading a JSON object, thorough Form API generation to theming and CSS columns. Tell us which parts you have figured out already, and what is your specific problem. – Mołot Nov 13 '14 at 14:04
  • I just really need to understand how to create a form with dynamic columns. The data source is not really relevant. For the concept, just imagine any kind of data store. I really just need the part about the dynamic form column layout. – Ethan Presberg Nov 13 '14 at 14:48
  • Have you tried anything? Sorry, but we don't provide manuals here. We only answer questions. Reasonably scoped ones. And building form from scratch is not. If you have a problem with wrapping form elements in DIVs suitable for use in CSS to create column, for example, that would be reasonable scope. So go as far as you can on your own, show us what you did, and then ask about first step that caused problems. – Mołot Nov 13 '14 at 14:50
  • Dude. Why are you making assumptions like that? I have spent around 30 hours working on this. I have tried many things. I am not asking you for anything. If you don't want to help, don't. No need for manuals or negativity. I just want to know if this is possible, and how can I do that. It is a generic question. As I said, if you aren't interested in helping, that's fine. Just don't be negative man. – Ethan Presberg Nov 13 '14 at 14:53
  • I don't care how much effort you put into the problem. All I care is effort and code shown in question - and without any assumptions we can clearly see your question does not show any effort. That's all what matters. This site is about questions, not people. I'm sorry for all the time you spent trying, but that does not make your question better scoped if you don't share it in that question, right? – Mołot Nov 13 '14 at 15:07
1

The way you group form items (as you will need to do to create a single column) is to use the FAPI grouping mechanism, which is described here :

https://www.drupal.org/node/262758

then you can use a loop and some vars to name them, for example :

<?php

  // How ever many columns your rest callback wants to make.
  $col_limit = 10;

  for ($x = 0, $x < $col_limit, $x++) {
    $form["group_$x"] = array(
      '#type' => 'fieldset',
      '#title' => t('Your Name'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form["group_$x"]["first_name_$x"] = array(
      '#type' => 'textfield',
      '#title' => t('First Name'),
    );
    $form["group_$x"]["last_name_$x"] = array(
      '#type' => 'textfield',
      '#title' => t('Last Name'),
    );
  }

  $form["col_limit"] = array(
    '#type' => 'hidden',
    '#value' => $col_limit,
  );

?>

Will create 10 groups, with form items. You can then use CSS or jQuery to create columns or tabs or format how ever you wish.

The hidden variable is there to tell you submit handler how many coumns there are and therefore how many times it has to loop looking for

$form_state['values']["last_name_$x"]
  • Or try Display Suite for Forms with the Renderable Elements module. You're basically adding some groups (grouping) in HTML for css by hand. These community modules let you try to achieve this same mechanism but with supported APIs – tenken Nov 13 '14 at 16:02
  • I'm not sure that will work. REL saves the form layout it creates in the database, but the the OP is asking about creating layouts for form elements created dynamically according to the result of his REST call. But definitely worth the heads-up. – jowan sebastian Nov 13 '14 at 16:20
  • just use a form generator function to build any 1 of 10 forms against the webservice data. – tenken Nov 13 '14 at 16:42
  • ... and then arrange them all in DS ? sounds a lot of work and a lot of developer UI modules to achieve something quite simple. I suppose knowing the scope better would help. OP ? – jowan sebastian Nov 13 '14 at 17:36
  • Well, I am creating an integration with Drupal and our software product. We design forms in our software product and via the REST API it is possible to retrieve the JSON definition. Since it is using Bootstrap, it stores the colspan. I was hoping to find a way to accomplish the multi column layout via the 12 column span, since for each field I know the col span. – Ethan Presberg Nov 13 '14 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.