Jump to content
Soma

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.

1.

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

2.

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?

3.

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";

$form->append($field);

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 ?

 

<?php
$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");
$form->append($submit);


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

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");
$col1->importArray([
    [
        'type' => 'InputfieldFile',
        'name' => 'images',
        'label' => 'Images',
        'destinationPath' => $upload_path,
        'extensions' => 'jpg jpeg gif png',
        'maxFiles' => 3,
        'maxFilesize' => 2*1024*1024,
        'required' => 1
    ],  
]);

$form->add($col1);

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...