I guess you can look at FormAPI's source code to see how it does that https://github.com/jojoe77777/FormAPI