{
    "href": "/post/2006/04/14/instant-forms-from-tables-with-solar/",
    "relId": "2006/04/14/instant-forms-from-tables-with-solar",
    "title": "Instant Forms from Tables with Solar",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/solar/",
            "relId": "solar",
            "title": "Solar",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2006-04-14 13:18:36 UTC",
    "updated": [
        "2006-04-14 13:18:36 UTC"
    ],
    "html": "<p>I was talking with <a href=\"http://weierophinney.net/matthew\">Matthew</a> this morning, and he asked if I was setting up a methodology to create forms using table definitions.  My answer was \"yes!\".</p>\n<p>Here's an example:  2 lines to start <a href=\"http://solarphp.com/\">Solar</a>, 2 lines to connect to an existing <a href=\"http://solarphp.com/index.php/docs/Solar_Sql_Table\">Solar_Sql_Table</a>, 2 lines to create a <a href=\"http://solarphp.com/index.php/docs/Solar_Form\">Solar_Form</a> using the table columns, and 2 lines to display it with <a href=\"http://solarphp.com/index.php/docs/Solar_View\">Solar_View</a> using the associated <a href=\"http://solarphp.com/index.php/docs/Solar_View_Helper_Form\">form helper</a>.  (The \"Solar_Model_Nodes\" table is part of the existing Solar_Content domain model.)</p>\n<pre><code>&lt;?php\n// prelims\nrequire 'Solar.php';\nSolar::start();\n\n// sql and table connection\nSolar::register('sql', 'Solar_Sql');\n$table = Solar::factory('Solar_Model_Nodes');\n\n// get a form and load elements from the table\n$form = Solar::factory('Solar_Form');\n$form-&gt;load('Solar_Form_Load_Table', $table);\n\n// display the form\n$view = Solar::factory('Solar_View');\necho $view-&gt;form($form);\n\n// done!\nSolar::stop();\n?&gt;</code></pre>\n<p>This creates an element for each column and a form-level validation based on the column datatype.  Number and string columns get a properly-sized \"text\" element, CLOBs get a \"textarea\", and booleans get a checkbox.  If the column has its own validation of \"inList\" or \"inKeys\", it gets a \"select\" element of the allowed values.  Soon, when I add Ajax-y widgets, date/time/timestamp columns will get date-picker elements.</p>\n<p>The $form-&gt;load() call can use 'Solar_Form_Load_Xml' too, if you want to define forms using XML.  (I don't, but Matthew liked the idea and contributed the code.)</p>\n<p>If you only want certain columns, you can pass a list of them as the third param to $form-&gt;load():</p>\n<pre><code>$cols = array('email', 'subj', 'body');\n$form-&gt;load('Solar_Form_Load_Table', $table, $cols);</code></pre>\n<p>Now, this is not all sweetness and light; the magic requires that you do the work of definining your columns in the Solar_Sql_Table class.  Lucky for us, that's pretty easy.  More on that in another post.</p>\n"
}
