programmatically validate form data

Dmitry Zholobov asked on January 24, 2017 10:28

Hi guys, I have to integrate my Kentico e-commerce with external services. They send me data that should be stored as a form in Kentico. So I wrote a code that creates bizform record like that:

    BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo(fname, SiteContext.CurrentSiteID);

    DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
    string className = formClass.ClassName;

    ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className);
    BizFormItem formRecord = BizFormItem.New(formClass.ClassName);

but when I'm saving record using

    formRecord.Insert();

I noticed that data I inserted is not validated against rules created for every field at design-time (the simpliest one is a length limitation)

Also I didn't find in documentation or forum any reference on how can I execute validation of form data before saving? I want to send all validation errors back to external service as a structured data, for that service to parse validation errors and display them formatted at their UI.

How can I do that?

Recent Answers


Chetan Sharma answered on January 24, 2017 11:51

You may make use BizForm events. It has events that can be triggered before and after form is submitted and you can put your logic accrodngly

Bizform events - https://docs.kentico.com/k8/custom-development/handling-global-events/reference-global-system-events#Reference-Globalsystemevents-BizFormItemEvents

A working API example - https://docs.kentico.com/k9/custom-development/miscellaneous-custom-development-tasks/working-with-form-data-using-the-api

Let me know if this is sufficient or do you need more info.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Dmitry Zholobov answered on January 24, 2017 12:54

Hi Chetan

I can't find any property at BizFormItemEventArgs class that has information about validation done, was that successfull or not. The record is at database whether it pass validation or not

Regards, Dmitry

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on January 24, 2017 13:17

If you are using BizForm and have properties setup correctly for each field then Form will submit if and only if it passes all the validation. It depends upon while configuring the form are you using the validate tag to enable validation associated with the form?

If this is not working then you will have to do validation of the form Before Form Submit event and validate all values and call event.Cancel() if in case it's invalid.

You may see an example here

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on January 24, 2017 13:55

Form validation should be included in form definition (check CMS_Class table). However it is saved as xml, so you'll have to parse and apply those rules, which is not a simple problem.

As long as you insert form record programmatically I'd recommend implementation of data validation there. It won't be connected to the validation rules defined in form it will just repeat those rules.

0 votesVote for this answer Mark as a Correct answer

Dmitry Zholobov answered on January 24, 2017 16:11 (last edited on January 24, 2017 16:13)

Hi guys, Thank you for attention to my problem. Let me simulate it on a very basic form that has only one string field and one validation rule that requires length to be not less that 10. So the field config looks like pic.1:

Image Text

Then I test validation rule by using standard backend UI and I see the rule is working (pic.2):

Image Text

Finally, I'm trying to create record using very simple code

BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("myTestForm", SiteContext.CurrentSiteID);
DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
string className = formClass.ClassName;
ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className);
BizFormItem formRecord = BizFormItem.New(formClass.ClassName);
formRecord.SetValue("MyTestField", "tooShort");
formRecord.Insert();

And this code successfully submits a record with validation rule violated! That's what I see in form records database page, you may see "tooShort" string stored in DB (pic.3) :

Image Text

What I really need, is to look, what that generic form page or bizform webpart does to validate data, but it is shipped with no source code. It looks like I need to call some method to manually validate data, but I it's not documented and I cannot find it.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on January 25, 2017 07:23

Hi Dmitry,

BizFormInfo object has form definition property Form which contains form fields. So you can use the following methods to get field descriptions:

formObject.Form.GetFields(...)
formObject.Form.GetFormField("fieldName")

Field description has Max/Min values, max length, regular expressions, etc. So you can try to build your own validation rules based on this information about the field.

0 votesVote for this answer Mark as a Correct answer

Dmitry Zholobov answered on January 25, 2017 08:39

Hi Anton, Thank you for your reply. Rules might be quite complex and Kentico macros can be involved to extend them. I would like to avoid programming my own K# interpeter. Especially when Kentico bizform validates all rules perfectly. All I need is just code that make same validation as bizform does, it is very strange why they just didn't add BizFormItem.Validate method that passes all rules and returns collection of violated rules...

Regards, Dmitry

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on January 25, 2017 09:48

Dmitry,
You don't need to develop your own K# interpeter, there is class CMS.MacroEngine.MacroResolver which has method Resolve which can help you. So you can add simple validation rules as in Kentico forms. When you create a field, you select a form type control (Textbox, dropdown, etc). These controls are inherited from FormEngineUserControl class which has 'IsValid' method. And this method is executed when forms are submitted. Unfortunately, it doesn't work for API calls, because you don't have a kentico form with controls which have the validation. There is Kentico Product Ideas website, you can suggest an idea about data validation there. And maybe it will be implemented:)

0 votesVote for this answer Mark as a Correct answer

Dmitry Zholobov answered on January 25, 2017 15:21

Anton, The only thing I wonder - they have all validation already implemented in webpart. It is only a question of exposing this functionality as method. Ok, it seems had never been demanded before. I have two workarounds:

  -Try to programmatically create BizForm control instance in my service and use its properties and methods to proceed validation
  -Or use default form they provide at backend as a additional layer of REST service for validation reasons. 

  Thank you for your answer
0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on January 25, 2017 17:59

It's a pity that this functionality is not an out-of-the-box feature. On one of my projects I implemented my custom validation based on field descriptions which I mentioned above. But it's too complex to impelement all possible validation rules, so I did it only for simple rules.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.