Skip to content

Instantly share code, notes, and snippets.

@somatonic
Last active August 29, 2016 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save somatonic/5415646 to your computer and use it in GitHub Desktop.
Save somatonic/5415646 to your computer and use it in GitHub Desktop.
form with fields rendered in a table example
<?php
/**
* Example form using PW API
*
* A workaround to get fields display in a table
* Those fields are marked with a property added to the fields $field->tablerow
*
* Approach is to grab those fields after form is put together and maybe processed,
* loop each row and render out the fields along with possible errors and add it to a string variable $table
* while we remove the field from the form at the same time.
* Later we add that html back to the form using a markup inputfield (InputfieldMarkup).
*
* This workaround allow to keep processing intact and show error validation for those fields, and have
* the form rendered in one call at the end.
*
*/
$form = $modules->get("InputfieldForm");
$form->attr("action", "./");
$form->attr("name", "someform");
// ROW 1
$field = $modules->get("InputfieldText");
$field->label = "Row 1 Label";
$field->tablerow = 1; // save row information in field for later use
$form->add($field);
$field = $modules->get("InputfieldText");
$field->required = 1;
$field->tablerow = 1;
$field->attr("name",'field_a1');
$form->add($field);
$field = $modules->get("InputfieldEmail");
$field->required = 1;
$field->tablerow = 1;
$field->attr("name",'field_a2');
$form->add($field);
$field = $modules->get("InputfieldCheckbox");
$field->required = 1;
$field->tablerow = 1;
$field->attr("name",'field_a4');
$form->add($field);
// ROW 2
$field = $modules->get("InputfieldText");
$field->label = "Row 2 Label";
$field->tablerow = 2;
$form->add($field);
$field = $modules->get("InputfieldText");
$field->required = 1;
$field->tablerow = 2;
$field->attr("name",'field_b1');
$form->add($field);
$field = $modules->get("InputfieldEmail");
$field->required = 1;
$field->tablerow = 2;
$field->attr("name",'field_b2');
$form->add($field);
$field = $modules->get("InputfieldCheckbox");
$field->required = 0;
$field->tablerow = 2;
$field->attr("name",'field_b3');
$form->add($field);
$field = $modules->get("InputfieldSubmit");
$field->attr("name",'submit');
$field->value = "submit";
$form->append($field);
// if form submited, process it
if($input->post->submit){
echo "sent";
$form->processInput($input->post);
if(count($form->getErrors())){
// do something
} else {
// form submited successfully
// do something
}
}
// generate table
$table = "<table width='100%'>";
$table .= "<thead>";
$table .= "<th>Expense</th>";
$table .= "<th>Description</th>";
$table .= "<th>Value</th>";
$table .= "<th>Value</th>";
$table .= "</thead>";
$table .= "</tbody>";
$i = 1;
while($i <= 2){ // loop through rows
$table .= "<tr>";
$j = 1;
foreach($form->children("tablerow=$i") as $field){
$table .= "<td>";
if($j == 1){
$table .= $field->label; // output label only
} else {
$table .= $field->render(); // render the field
// print errors, (true) to clear errors, or they will stack
foreach($field->getErrors(true) as $e) {
$table .= "<p class='error'>$e</p>";
}
}
$table .= "</td>";
$form->remove($field);
$j++;
}
$table .= "</tr>";
$i++;
}
$table .= "</tbody>";
$table .= "</table>";
// make the rendered table a markup field and add it back to form
$markup = $modules->get('InputfieldMarkup');
$markup->markupText = $table;
$form->prepend($markup);
// render form in one slap
echo $form->render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment