I have read al the examples @tylerfrankenstein.com
The problem is all these examples work with the build in forms of drupalgap.

As tylersfankenstein says here https://www.drupal.org/node/2852527
Its better to create your own form.
I can build a form but can't seem to find how to save the input of the form to a node.

Could someone show an example of a custom form that is used to create a new node?
That would help me out a lot.
Thanks

Comments

GoempieK created an issue. See original summary.

GoempieK’s picture

a bit more explaining of what i don't get.

The problem is i don't understand how i can go from a custom form to the json input needed by node_save.

http://docs.drupalgap.org/7/Forms/Creating_a_Custom_Form
Here the form is submit to an alert.

/**
 * Define the form's submit function.
 */
function my_module_custom_form_submit(form, form_state) {
  try {
    drupalgap_alert('Hello ' + form_state.values['name'] + '!');
  }
  catch (error) { console.log('my_module_custom_form_submit - ' + error); }
}

Now i would like to save the input to a new node, this should be done with Node save like this

var node = {
  title:"Hello World",
  type:"article"
};
node_save(node, {
  success:function(result) {
    alert("Saved new node #" + result.nid);
  }
});

I don't get how i can make the input of the form into the json var node shown above.
Feeling pretty dumb after 2 day's of searching.

GoempieK’s picture

Solved.

The example code doesn't work because it says node.type instead of team.

Here it says default_value: node.type

f  form.elements['type'] = {
    type: 'hidden',
    required: true,
    default_value: node.type
  };

Maybe make it clearer in the docs that you have to change this to the actual node type like below

  form.elements['type'] = {
    type: 'hidden',
    required: true,
    default_value: node.type //Fil in the actual node type here 'team' in our example
  };

Would have saved me a lot of ours, on the other hand i've learned a lot in my quest :-)

tyler.frankenstein’s picture

Status: Active » Closed (works as designed)

I've glad this is working for you now. As you found, you can build the node JSON in your form's submit handler, then pass it along to the node_save() function.

function my_custom_form_submit(form, form_state) {
  var node = {
    title: form_state.values.title,
    type: 'team',
    body: { und: [ { value: 'Hello' } ] },
  };
  node_save(node, {
    success: function(result) {
      console.log(result.nid + ' node saved!');
    },
    error: function(xhr, status, msg) { drupalgap_alert(msg); }
  });
}

You can either statically set your node's "type" value above, or you can dynamically place it in a hidden input field and grab its value from the form_state.values.

I'm glad you learned some good stuff along the way, enjoy!