I'm not sure if I'm being clever or making things more difficult. I'm working within a custom MVC framework, and within some of my views which contain forms I'm preserving input and styling errors as follows:<form action=submit method=POST><label for=name class=italic>Name:</label><input <?php if (in_array('name', $error)) { echo 'class=form-error'; } ?> type=text name=name value=<?php if (isset($name)) { echo $name; } elseif (isset($valid_user)) { echo $valid_user['name']; } ?> maxlength=20 required=required /><br /><input type=submit class=submit value=Update />In this example, $valid_user['name'] would be the default value for existing data (when the form is first loaded), and $name would be the posted value being displayed here again in case there was a user error that needs to be fixed.Embedding all of these values is very time consuming when creating new forms, so I'm trying to make things easier on myself. I want to stick with a server-side solution since I believe websites should be complimented by Javascript, not dependent on it. I figure I could probably do all of this dynamically using PHP's DOMDocument:$dom = new DOMDocument();$dom->preserveWhiteSpace = false;$dom->loadHTMLFile($view);$forms = $dom->getElementsByTagName('form');$total_forms = $forms->length;for ($i = 0; $i < $total_forms; $i++) {    $inputs = $forms->item($i)->getElementsByTagName('input');    $total_inputs = $inputs->length;    if ($total_inputs > 0) {        foreach ($inputs as $input) {            $type = $input->getAttribute('type');            switch ($type) {                case 'text':                    // check for errors                    $attr_name = $input->getAttribute('name');                    if (in_array($attr_name, $error)) {                        $input->setAttribute('class', 'form-error');                    }                    // check for default / posted values                    if (isset($$attr_name)) {                        $input->setAttribute('value', $$attr_name);                    } elseif (isset($_POST[$attr_name])) {                        $input->setAttribute('value', $_POST[$attr_name]);                    }                    break;            }        }    }}echo $header;// remove unwanted tags that are being automatically addedecho preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());echo $footer;Going with this approach will obviously require writing a lot of extra code, so before going any further I'm just curious what your opinions are or if there are any potential downfalls I'm maybe not thinking of? Would it be better to stick with my original approach or are there other solutions to make this less tedious?
Automating form error handling
design;php;programming practices;error handling;webforms
The first thing I can suggest you is to take out the Logic from Mark-up or basically avoid to write in-line logic in your mark-up. So you can re-write your first example like this:if ( in_array('name', $error) ) {    $class = 'form-error';} else {    $class = null;}// or ...$class = in_array( 'name', $error ) ? 'form-error' : null;if ( isset($name) ) {    $value = $name;} else if ( isset($valid_user) ) {    $value = $valid_user['name'];}And then you can simply re-write the mark-up like this, which is much simpler:<input class=<?php echo $class; ?> value=<?php echo $value; ?> ...If you want to go for the second way I suggest you to use Jade instead of PHP's DOM class. Also to me either ways seems fine, however the later one could be an overkill.