Ask HN: What is the best way to verify forms

1 points by __manuels__ ↗ HN
To be honest, I hate GUI programming. You always have to verify the data a user enters and convert this data into your data structures. You could do it the easy way and just make the GUI elements reflect the schema of your data structure, but often this approach is not very user-friendly.

E.g. it's easier for the user to enter a list of IP addresses separated by commas, but your data structure holds an array of IP addresses. If the user enters invalid data, this must be shown in the GUI as an error.

And anytime the user modifies something, the data structures have to be modified (or not, depending on whether you decide to save automatically or have a 'save' button) and certain parts of your GUI have to be updated, too.

Usually when I start writing something like this, it becomes incredibly complex code.

What is the best concept to handle serialize and de-serialize user input in a GUI? Any good videos of talks about this?

I am not looking for a specific library in JS, C++ or what ever, but rather for a concept/programming pattern/whatever to deal with this situation.

1 comment

[ 2.9 ms ] story [ 15.4 ms ] thread
You develop a feel for it. I'm not sure if there is a silver bullet to this. Specific examples and cases are easier to give answers to. Your list of IP addresses is a solid one. You might think a comma separated list is easiest (and maybe it is), but you could likely have a single input field and a "+ add another IP" link that dynamically adds a new IP input field.

You either make it more structured and need less validations and transforms or you make it more free form and need more validations and transforms. And it depends on the case. In the end, you probably want to construct some JSON that maps to part or all of your data structure. Depending on how important it is to get the data "right," you can also prompt the user with a "does this look right?" confirmation. Also, don't forget all the "falsehoods programmers believe about" articles.