⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Grocery crud 2.7.6 and custom fields.



dlaynes

dlaynes
  • profile picture
  • Member

Posted 02 July 2019 - 01:15 AM

Hi, when adding custom fields to the Add Form page (and submitting the form), I'm getting this error:

this.cachedInitialData.fieldTypes[field.name] is undefined

 

Code:

 

->fieldTypeAddForm('entered_at','datetime')
->fieldTypeAddForm('initial_payment','numeric')
->fieldTypeAddForm('daily_cost','numeric')
->addFields(['car_type_id','plate','entered_at','initial_payment','daily_cost'])
->displayAs('entered_at','Name 1')
->displayAs('initial_payment','Name 2')
->displayAs('daily_cost','Name 3')

 

Javascript function with error:
 

getPostDataFromForm: function getPostDataFromForm($jqueryForm) {
var postData = {};
_.each($jqueryForm.serializeArray(), function eachFormFields(field) {
var dataType = this.cachedInitialData.fieldTypes[field.name].dataType;
if (dataType === 'relational_n_n' || dataType === 'native_relational_n_n') {
if (postData[field.name] === undefined) {
postData[field.name] = [field.value];
} else {
postData[field.name].push(field.value);
}
} else {
postData[field.name] = field.value;
}
}.bind(this));
return postData;
},

I have cleared the GroceryCRUD cache and the error persists.
Thank you


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 03 July 2019 - 12:57 PM

Hi, when adding custom fields to the Add Form page (and submitting the form), I'm getting this error:

this.cachedInitialData.fieldTypes[field.name] is undefined

 

Code:

 

->fieldTypeAddForm('entered_at','datetime')
->fieldTypeAddForm('initial_payment','numeric')
->fieldTypeAddForm('daily_cost','numeric')
->addFields(['car_type_id','plate','entered_at','initial_payment','daily_cost'])
->displayAs('entered_at','Name 1')
->displayAs('initial_payment','Name 2')
->displayAs('daily_cost','Name 3')

 

Javascript function with error:
 

getPostDataFromForm: function getPostDataFromForm($jqueryForm) {
var postData = {};
_.each($jqueryForm.serializeArray(), function eachFormFields(field) {
var dataType = this.cachedInitialData.fieldTypes[field.name].dataType;
if (dataType === 'relational_n_n' || dataType === 'native_relational_n_n') {
if (postData[field.name] === undefined) {
postData[field.name] = [field.value];
} else {
postData[field.name].push(field.value);
}
} else {
postData[field.name] = field.value;
}
}.bind(this));
return postData;
},

I have cleared the GroceryCRUD cache and the error persists.
Thank you

Hello @dlaynes,

 

Unfortenetly I can't reproduce your issue (just checked with the version 2.7.6

from the error that you are getting it seems to me that the field doesn't exist at the database (e.g. it is a new field). Is that the case? If so then the only solution is to use customModel. For more about customModel you can see at the example here: https://www.grocerycrud.com/enterprise/examples-3/create-custom-model

 

Let me know if that helped.

 

Regards

Johnny


dlaynes

dlaynes
  • profile picture
  • Member

Posted 03 July 2019 - 14:18 PM

Hi @web-johnny. Yes. the fields do not exist in the database.
I managed to insert a record (and a dependant record) in the database using the Custom model. Thank you.

These were the steps I followed:

 

- I created the custom model with the extra fields.

- The cache was deleted.

- The original field definitions should stay, even if they are repeated in the model.

- In the current CRUD, a columns() call with only existing fields was added, otherwise there will be an SQL error.
- I created a variable (array) to be used in callbackBeforeInsert and callbackAfterInsert, in both methods it should be passed by reference within the use() rule.
- Some data was copied to the global array, and the extra fields in $stateParameters->data are unset in the callbackBeforeInsert call, in order to prevent SQL errors.

As a side note, when validating the form using custom rules, I didn't know that "Insert" and "AddForm" are different values returned by $crud->setState(), perhaps they should be listed in the guide.

Regards.


 


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 04 July 2019 - 05:38 AM

Hello @dlaynes and thank you for sharing your steps with us.

 

I have to say that these were more steps than I was expected but you did follow the correct path. At the future I would like to refactor the way that custom models works so it can be easier for users to change things. Currently the implementation works... however it requires lot of steps!

 

I am glad that you could find solution to your problem by the way  :D

 

Regards

Johnny