Skip to content

Instantly share code, notes, and snippets.

@josedigital
Created April 27, 2015 04:09
Show Gist options
  • Save josedigital/829d9e55728d2a4f4d6b to your computer and use it in GitHub Desktop.
Save josedigital/829d9e55728d2a4f4d6b to your computer and use it in GitHub Desktop.

Revisions

  1. @alexrdz alexrdz created this gist Apr 27, 2015.
    52 changes: 52 additions & 0 deletions form-from-input-fields.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    // Get the page you need to edit
    $mypage = $pages->get('/some/page/');

    // Populate with the names of the fields you want to exclude OR include (see instructions below)
    // Leave empty to output all the fields
    $myfields = array('body', 'email');
    $form = $modules->get('InputfieldForm');
    $fields = $mypage->getInputfields();

    // If array is not empty use it to filter the fields
    if ($myfields){
    foreach($fields as $f){
    // Output all the fields minus the ones listed in the $myfields array
    // Instead, to output only the fields that are in the array, remove the (!) from the condition
    if (!in_array($f->name, $myfields)){
    $form->append($f);
    }
    }
    }

    // Else, include all the fields
    else {
    $form->append($fields);
    }

    // Add save button
    $field = $this->modules->get('InputfieldSubmit');
    $field->attr('id+name', 'submit_save');
    $field->attr('value', 'Save');
    $field->label = "submit herei";
    $form->append($field);

    // Process the form
    // (code replaced by a new one provided by Ryan)
    if($input->post->submit_save) {
    $form->processInput($input->post);
    if(!$form->getErrors()) {
    $mypage->of(false); // turn off output formatting before setting values
    foreach($mypage->fields as $f) {
    $mypage->set($f->name, $form->get($f->name)->value);
    }
    }
    }

    include("./head.inc");

    // Render the form
    echo $form->render();

    include("./foot.inc");