Jump to content

Create simple forms using API

Recommended Posts

On 4/25/2018 at 8:22 PM, Ivan Gretsky said:

Thanks for the thread @Soma. One of the most important and cited ones in the whole forum history!

In the original post there are a few points that seem to be not discussed later in the thread, but which are extremely interesting to me.


Are there any good examples of those to dig into? Gists maybe?


Looking here it seems arrays are not allowed. I might be not understanding it right or things might change since when it was written. Is there actually a way to process input from a page or an array?


There was already a question about what kind of validation processInput does and @adrian's answer too. I read the code a few times but still not sure should I sanitize values after processInput before saving to page fields or not. Is it necessary?

Thanks again! Learning ProcessWire is still fun (or am I doing it too slow?!)

1. There's a example in the code itself with email:

if($email && (strpos($email->value,'@hotmail') !== FALSE)){        // attach an error to the field
        // and it will get displayed along the field
        $email->error("Sorry we don't accept hotmail addresses for now.");


Anything the API provides is possible. I can't go into details as there's too much and don't have any examples ready.

2. Of course an array is possible, input->post is also and array. You just have to make it a WireInputData array. https://github.com/processwire/processwire/blob/341342dc5b1c58012ae7cb26cffe2c57cd915552/wire/core/WireInputData.php

3. "Validation" is per Inputfield and it depends. Some inputfields do some sanitations to make sure an expected format or type of value is given (for example email https://github.com/processwire/processwire/blob/341342dc5b1c58012ae7cb26cffe2c57cd915552/wire/modules/Inputfield/InputfieldEmail.module) but most do nothing at all. You can look at all the Inputfields processInput() method to see what it does. Text and Textarea have some Inputfield settings to strip tags for example. It depends also on what the Form is used for and where you use the data afterwards. It's never an issue to save data to DB ie Pages, it's always about what and where you output the data (website, email, ...).

  • Like 2

Share this post

Link to post
Share on other sites
On 7/9/2013 at 8:32 PM, Soma said:

You could even use the form building method in this thread starting post and render fields where you like. There'll be still markup being outputed depending on the inputfieldtype, but maybe allows for more flexibility.


$showform = true;

$form = $modules->get("InputfieldForm");

$field = $modules->get("InputfieldEmail");
$field->attr("name", "email");
$field->label = "Email";


if($input->post->submit) $form->processInput($input->post);

if($form->getErrors()) {
    $showform = true;
} else {
    $showform = false;
   // form valid, do something

if($showform) {
  echo "<form action='#' method='post'>";
  echo "<div class='row'>" . $form->get('email')->render() . "<div>";

Anything is possible, just use what you need. Think it's almost same as you would work with fields or pages. 

Hi Soma using form api with this method but form errors always shows after first submit. How to clear after reload page ?


$form = $modules->get("InputfieldForm");

// create a text input
$field = $modules->get("InputfieldText");
$field->label = "Name";
$field->attr('id+name', 'name');
$field->required = 1;
$form->append($field); // append the field to the form

// create email field
$field = $modules->get("InputfieldEmail");
$field->label = "E-Mail";
$field->attr('id+name', 'email');
$field->required = 1;
$form->append($field); // append the field

// oh a submit button!
$submit = $modules->get("InputfieldSubmit");
$submit->attr("value", "Subscribe");
$submit->attr("id+name", "submit");

if ($input->post->submit) {

echo "<form action='./' method='post'>";
echo "<div class='row'>" . session()->CSRF()->renderInput() . "</div>";
echo "<div class='row'>" . $form->get('name')->render() . "</div>";
echo "<div class='row'>" . $form->get('name')->getErrors()[0] . "</div>";
echo "<div class='row'>" . $form->get('email')->render() . "</div>";
echo "<div class='row'>" . $form->get('email')->getErrors()[0] . "</div>";
echo "<div class='row'>" . $form->get('submit')->render() . "</div>";
echo "</form>";


Share this post

Link to post
Share on other sites

calling $inputfield->getErrors(true) after form render solves this. 

another question if InputfieldFile in InputfieldFieldset form not get " enctype="multipart/form-data" " attr, this is bug ?

$form = $modules->get("InputfieldForm");

$col1 = $modules->get("InputfieldFieldset");
        'type' => 'InputfieldFile',
        'name' => 'images',
        'label' => 'Images',
        'destinationPath' => $upload_path,
        'extensions' => 'jpg jpeg gif png',
        'maxFiles' => 3,
        'maxFilesize' => 2*1024*1024,
        'required' => 1



Share this post

Link to post
Share on other sites

On the frontend template I have this in an edit form:

    $field = $modules->get("InputfieldCheckbox");
    $field->label = "Discard";
    $field->attr('id+name', 'prop_discard');
    if($page->discard == 1) $field->attr("checked");

Because $field->value = $page->discard does not work, nor does anything else I've tried. How do I get the checkbox to reflect if it is checked? Right now on save if it was checked it needs to be re-checked every time.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...