Jump to content


Grocery crud 2.7.6 and custom fields.


  • Please log in to reply
3 replies to this topic

#1 dlaynes

dlaynes

    Newbie

  • Members
  • Pip
  • 6 posts

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



#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,100 posts
  • LocationLondon

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.groceryc...te-custom-model

 

Let me know if that helped.

 

Regards

Johnny


Posted Image

#3 dlaynes

dlaynes

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 03 July 2019 - 02: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.


 



#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,100 posts
  • LocationLondon

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


Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users