Refactor edit form.

Rewrites most of the edit form handling.

There were some bugs with the previous spaghetti, especially when
making an edit which caused a validation error. When rendering a form
with a validation error, fields were displayed incorrectly.

It also fixes several issues with merge conflicts.

To fix this issue properly I had to rewrite most of the form handling,
so here.