DrupalGap icon indicating copy to clipboard operation
DrupalGap copied to clipboard

Select List on Custom Form

Open igenroot opened this issue 8 years ago • 7 comments

Hi everyone,

I having an issue node_save when use select list type on custom form. From console output

POST 
XHR 
http://localhost/[HTTP/1.1 406 Not Acceptable : An illegal choice has been detected. Please contact the site administrator. An illegal choice has been detected. Please contact the site administrator. 198ms]
"POST: http://localhost/?q=drupalgap/node.json - 406 - Not Acceptable : An illegal choice has been detected. Please contact the site administrator. An illegal choice has been detected. Please contact the site administrator."  jdrupal-7.0.5.min.js:2:547
{"form_errors":{"field_article_type][und":"An illegal choice has been detected. Please contact the site administrator."} 

Here's the source code

function my_module_articles_form(form, form_state, node) {
 if (node.nid) {
   form.id += '_' + node.nid;
   form.elements['nid'] = {
       type: 'hidden',
       required: true,
       default_value: node.nid
     };
 }
 form.elements['type'] = {
       type: 'hidden',
       required: true,
       default_value: 'articles'
 };
 form.elements['articletype'] = {
     title: 'Type Article',
     required: false,
     type: 'select',
     options: {
       0: 'Article A',
       1: 'Article B',
       2: 'Article C'
     }, 
     default_value: '0'
 };
 form.elements['submit'] =  {
   type: 'submit',
   value: t('Save')
 };
 return form;
}

function my_module_articles_form_submit(form, form_state) {
 try {
   
   var data_to_save = {
     language: form_state.values.language,
     type: form_state.values.type,
     field_article_type: { und: [ { value: form_state.values.articletype } ] },
   };
 
   node_save(data_to_save, {
     success: function(result) {
       drupalgap_goto('node/' + result.nid, { reloadPage: true });
     }
   });
 
 }
 catch (error) { console.log('my_module_articles_form_submit - ' + error); }
}

Is there any workaround to overcome this issue.

Thanks in advance.

igenroot avatar Jul 29 '17 13:07 igenroot

Should it be form.elements['article_type']?

mkinnan avatar Jul 29 '17 14:07 mkinnan

@mkinnan i try but still got error. The problem when using select list type. If use textfield type working fine.

igenroot avatar Jul 29 '17 17:07 igenroot

@igenroot Did you create the values in your Drupal field?

 options: {
   0: 'Article A',
   1: 'Article B'
   2: 'Article C'
 }, 

mkinnan avatar Jul 29 '17 18:07 mkinnan

@mkinnan yup.

From my drupal site.

1st Screenshot Image of First

2nd Screenshot Image of Second

igenroot avatar Jul 30 '17 05:07 igenroot

Do the single quotes around the 0 need to be removed: default_value: '0'

Does the error happen when you save a new node without changing the article_type field? Or does it also happen when you select an item too?

Have you tried completely removing the default value line of code and selecting an item (e.g., Article B) to see if it works on save?

mkinnan avatar Jul 30 '17 15:07 mkinnan

This happen when save new node. If i select or not select an item, error still appear. I already try remove the quotes and remove default value still give same result. May i know what you mean changing article_type field? Is it change machine name or form element?

igenroot avatar Jul 31 '17 05:07 igenroot

@igenroot I was referring to whether selecting an item made a difference. I would inspect the form object on submit using console.log. i would also try replacing form_state.values.articletype with one of the select list values to help track down the source of the error.

mkinnan avatar Jul 31 '17 14:07 mkinnan