根据PHP类创建HTML表单

If I have PHP class for example like this:

class Person {    
    private $dateOfBirth;
    private $height;

    public function getDateOfBirth() {
        return $this->dateOfBirth;
    }

    public function setDateOfBirth($dateOfBirth) {
        $this->dateOfBirth = $dateOfBirth;
    }

    public function getHeight() {
        return $this->height;
    }

    public function setAge($height) {
        $this->height= $height;
    }
}

Is there some nice solution how to dynamically generate HTML form according to this class?

<form action="action.php" method="post">
    <input type="date" name="DateOfBirth" value="1900-01-01"/>
    <input type="number" name="Height" value="180"/>
    <input type="submit" class="submit" value="Submit"/>
</form>

The only solution that I came up with is to iterate all properties of class and generate inputs. And if the name of the property contains e.g. 'date' make 'input type="date"'. But I don't really like this solution. Given that property Height is obviously number type, its name doesn't indicate it. And I don't want to rename it. Property names must remain the same.

Thanks for responses

Maybe the most precise solution will be to create DB table with details for every class and generate form according to this table.

form_fields http://www.cloud.vnovak.cz/form_fields.png

Compound key on class and order columns.

If you need to generate a HTML form, i think you should be looking at jQuery(JavaScript). Javascript can be used to manipulate the DOM of the HTML page.

You can also toggle hide and show of an element (a form or div) with jquery

$(document).ready(function(){
  $('form').hide(); 
  $('button').click(function(){
    $('form').toggle(); //this can be used in place of the hide and show methods
    //$('form').show(); 
    //$('form').hide(); 
  });
});

The code above hides the form element when the page loads and when a submit button is clicked, it toggles the visibility of the element.

This is an easy way to go around it.

Grace...

Why not add a function to the class which creates the form?

public function getForm() {
      $form = '<form action="action.php" method="post">';
      $form .= '<input type="date" name="DateOfBirth" value="1900-01-01"/>';
      $form .= '<input type="number" name="Height" value="180"/>';
      $form .= '<input type="submit" class="submit" value="Submit"/>';
      $form .= '</form>';
      return $form;
}

Then just echo out the returned form where you need it. It isn't quite what you were asking for, but it is very simple. Am I right in thinking the reason you wanted this is so that you don't need to keep creating a form? If so, this will solve the issue.

Another solution could be to add form types to each variable in your class. E.g.

$heightType = "number";
$nameType = "text";

Then you can iterate over these and you will have the correct input types. Personally, I think the first solution is much easier.

Took so long to write this that someone already suggested my second option haha!