Basic Entity qodlysource question

I added an entity qodly source to my page: “newacct”. (a cs.ACCOUNTEntity)

I have a few text inputs on the page with qodlysources like newacct.contactname, newacct.contactemail etc.

User fills in these text inputs with data.

User clicks submit for data to be validated and if valid, entity is saved.

I pass the newacct entity as the parameter to my validation function.

Qodly debug says can’t consume parameter 1 (newacct cs.ACCOUNTEntity)??

My expectation is that I am passing an unsaved cs.ACCOUNTEntity, with attribute values as entered on the page, to my function to review, validation, and save if valid. What am I missing? Thx!

Hello Robert,

To help us better understand your situation, could you provide us screenshots of your form?

Warm regards.

Below is the screen. It is a form a user fills in to setup a new account. I tried to use an AccountEntity class to store the data on the form for vaidation and saving when the submit button is clicked.

I could not get the function to work when I passed the newacct qodlysource as a paramter. its said cannot consume 1st parameter.

To solve:
I changed newacct to an object with attributes that match exactly to the Account table fields in the model. It passed fine to the function and within the function I just used:

newaccountentity.fromObject(newacct) to convert to an entity witin the function.

Did not know why could not used entity on the form.

I think my basic question is: Can we setup a qodlysource on a form that is an entity.

Qodlysource: name: newacct, type: Entity, Depth: ?? Initial Value: Empty or None??

Then, on the page, add string fields like:

newacct.legal_name etc. that match the DB attributes of the entity.

Then, pass the new, unsaved, undelcared ? entity to a function which checks the values and, if all good, saves it to the DS?

Hi @musseman ,

If I understand well your question, what you want is perfectly possible.

Declare your “newacct” entity, initial value does not matter at this point, neither depth.

On your page, setup the init event to a standard action “new”.
This way, when your page loads, a new entity will be created, yet unsaved.

Then map this entity’s attributes to whatever component on your page.

Add a save button, map it to the “save” standard action. Also map it to another “new” action.

Result: clicking on save will save the new entity in db, and will create a new one, yet unsaved, ready to be saved.

Then you can elaborate on this:

  • instead of the new and save standard actions, you can map custom functions you implement at dataclass and entity level: ACCOUNT.myCreate() returning a new entity and ACCOUNTEntity.mySave() (called via an event on the save button: newacct.mySave()). This way you can control default values for a newly created entity, and also check values before saving.

If you need to deal with this entity accross several pages, you can declare it in a namespace.

1 Like

Ahhh! There it is! The part i was missing was to add the “new/create” standard action to the Entity qodlysource on init.

I did not do any such create action on init so it must have been passing a nothing entity to the function and so function was unhappy.

I was kind of scratching my head about when to do new(). Now is see.

So just to confirm:
-add entity qodlysource to page
-setup multiple text inputs and bind them to that source’s attributes
-add on init event to create new, so values can be entered into textinputs and be passed along
-application user enters values into textinputs and clicks save
-save standard action can be triggered on the entity, or:
-want to confirm…: the entity qodly source itself can be passed to a function as a param for validating, modifying, and saving within the function.

I see now and will try some of your bonus thoughts as well.

Thanks!!

hmmm…tried it again but still seem to be having strange behavior when I pass the new, but unsaved entity qodlysource to a function after entering some of its attribute values on the page.

Do I need to save entity with the values entered before validating via function?

When the validate function is called, and the debugger flags me and i check the value of the variable, the newacct cs.ACCOUNTEntity variable only displays as “[object entity]” in the debugger. Its either not saving the attributes as they are entered on the age, or something else?

I figured it out. Please see my other post about empty values in OBJECTS that I’m trying to get a handle on. ie for example when an object is setup, boolean attributes do not have an “unselected” option, they default to false and can only be true or false. So how do i know if the user chose false, or forgot to select at all?

String is “” for empty I believe, still not sure about numbers etc.?

This is important as I need to test if the user has entered values of the correct type in various attributes.

OBJECTS seem to have these nichy rules about “empty” values which depend on type. So i wrote my validation code to accomodate those rules best I could.

Now ..change to an ENTITY source-same attributes: I dug through docs and looks like EVERY single empty entity attribute, no matter what the type, is set to null initially. So switching to entity following your tip worked until I tried to validate user entred values using same code as I had for objects.

For example if i have a text field attached to an attribute and want to make sure its not left empty i need to:

if( (entity.stringattribute == null || entity.stringattribute == “” || length(entity.stringattribute)>25)
[fails vailidation and msg is returned] [must test for null and “” in case user entered then deletes]
end

It looks like I need to test for the null first as well, or the other tests will stop execution if the value is null and I try to test any of the other conditions 1st before the null test.

Hopefully this seems right?..validation code sees to work as i go line by line and update to consider null values first in every test with ENTITY Source.

I am trying to get this to work and am getting close. Does this make sense?:

All Entities when created set all attribute values to Null automatically.

When I tred to test if(somentity.attribute == null) i got stuck and things just did not work, so I went back to using objects with attributes that matched the dataclass attributes until all its attributes were validated and confirmed, then I created an entity using .fromObject, then saved the entity.

I was able to work on why i got stuck and had to switch, and it seems that for entities:

(someentity.attribute == null) does not work. BUT I can do this:

(valueType(someentity.attribute) == 255) (the code number for null) and the test works

If a user forgets to enter anything the name attribute for example, i can use the test above and let them know it needs to be completed.

One minor catch. If a user enters a name into the attribute, but then, for whatever reason, deletes their entry the valuetype of the empty required name attribute is no longer 255/null. The actual type is assigned and it is now an empty string value instead of a null.

Apologies if this is obvious but maybe will help someone! If it does not seem right, or there is a better way, happy to learn.