Jump to content

Module: FormHelper


pwFoo

Recommended Posts

You shouldn't use FrontendContentManager (FCM) because it's unstable and after some changes at PW dev FormHelper amd FCM won't work as expected in future releases.

 
 

At the moment I remove the FormHelper dependency for FrontendUserLogin and FrontendUserRegister modules because of the native support for form fields as array data. Maybe I'm rewrite / redesign FormHelp to simplify form process and file handling, but have to think about first.

After current work is done, I'll start a new try to build a FCM module.

If you need a module to frontend edit pages you should try Fredi for example.

Link to comment
Share on other sites

Hello JoZ3,

sorry for the delay... 

I installed both (FormHelper and FrontendContentManager) via PW backend 

  1. Navigate to Admin -> Modules -> New
  2. Fill in FormHelper (second time FrontendContentManager) as ModuleClassName
  3. Click Download and Install

No errors during install the current module versions. Could You try it again?

Link to comment
Share on other sites

Hi, I have this strange error when try to install:

Parse Error: syntax error, unexpected '=', expecting ')' (line 276 of /home/myserver/public_html/site/modules/FormHelper/FormHelper/FormHelper.module)
This error message was shown because you are logged in as a Superuser. Error has been logged.

My php version is 5.4.35

This is because of assignments inside function calls, which should be avoided at all costs, e.g. in line 329 of FormHelper.module (279 is the other one):

if (!empty($files = $this->files[$fieldName])) {

Strict mode barks on this because assigning to passed variables like this breaks pass-by-reference. Consider these two snippets:

$x = 1
byref($x);
echo $x . "\n";

function byref(&$val)
{
  $val = 3;
}

This correctly echoes "3". Now lets do the same and assign $x in the function call:

byref($x = 1);
echo $x . "\n";

function byref(&$val)
{
  $val = 3;
}

Now our output is "1", which is of course not what we would have expected after assigning to a variable passed by reference - our assignment has turned the pass-by-reference into a pass-by-value. The original line should be written as:


$files = $this->files[$fieldName];
if (!empty($files)) {



			
		
  • Like 1
Link to comment
Share on other sites

New testing branch (readme file includes an example usage / template fiel):

https://bitbucket.org/pwFoo/formhelper/src/14d3cf18768b?at=testing

*UPDATE*

Supports forms based on

  • Page
  • Template
  • Fields
  • InputfieldForm (form fields)
  • and should also work with array (PW 2.5.5+).

You can define optional field settings

  • callback (additional field processing via dynamic function)
  • set / clear form field value or get unformatted page field value
  • skip a field (not added to form)
  • ignore a field (no additional field processing, skip field after form process in getForm() function)
  • set the CKEditor pwImage plugin page id (plugin image source)
  • set a sanitizer to field and you will get a sanitized value with getValue() function

FormHelper prepare file fields and give you a file array to work with (path + filename)

Edited by pwFoo
  • Like 1
Link to comment
Share on other sites

New testing branch commit with a shortcut to combine source() with create()

// Set source, optional $fhOptions and $refPage and chain it with form render
$fh->form($source, $fhOptions = null, $refPage = null)->render();

Thought about field save / create page feature, but should be done with another module because it would unnecessary overhead if you sending a mail or something else.

Current version:
https://bitbucket.org/pwFoo/formhelper/src/99d0ea09a8012446fd8fba71e7b34ede319551ad/?at=testing

Link to comment
Share on other sites

  • 2 weeks later...

Bump version to 0.5.3

Replaced fhOption "callback" with "callbackPrepare" (executed before field added to form) and callbackProcess (executed after form process to do additional processing).

Examples

callbackProcess

// You can use "$field" (current field object) and "wire()")
$fhOptions['title']['callbackProcess'] = function ($field) {
    // Modify a form field (wire('fh') is the FormHelper instance / object which is added to "wire()")
    wire('fh')->form->get('body')->error('CALLBACK-ERROR to another field...');

    // modify current field by callback
    $field->error("CALLBACK {$field->name} ERROR");
};

callbackPrepare

$fhOptions['title']['callbackPrepare'] = function ($field) {
    // replace field value (executed after clear / value / unformatted feature)
    $field->value = "TEST PREPARE CALLBACK";

    // or skip the field just before it will be added to the form...
    wire('fh')->fhOptions($field, array('skip' => true));
};

FormHelper creates forms by Page, Template, Fields, Inputfields, InputfieldForm (fields) or an array with field data with 371 lines of code. Maybe it could be separated into a basic form process module and an sub module to handle additional features (Template based form with file upload, CKEditor image plugin, ...) but I don't know if it should be necessary.

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

While look deeper and deeper inside Processwire and hooks I'll have a new reduced FormHelper testing branch...

FormHelper TESTING branch extends InputfieldForm. So you can use the pw form api with some extensions.

How FormHelper could be used.

 
// Load FormHelper module and also set wire('fh') variable
$fh = $modules->get('FormHelper');

// Create a (first) form
$form = $fh->create();

// Add some fields to the PW form object

// Add default sanitizer
$field->fhSanitizer = 'text';

// Add preprocess and processed callbacks
$field->addHook('fhCallbackPreprocess', function($event) {
    // called before form processing
});
$field->addHook('fhCallbackProcessed', function($event) {
    // called after form processing
});
 
// Your callbacks need more arguments than $event or $this (class inside the callback is written)? Set additional arguments as array
$field->fhCallbackArgs = array($arg1, $arg2);   // access arguments via $event->arguments
 
// process form
// adds submit button, callback execution, CSRF check + reset and processInput()
// error handling, also WireUpload errors
if ($form->fhProcessForm()) {
// true = submitted without errors
// false = submitted with errors
// null = not submitted
 
// Get value sanitized (fhSanitizer) echo $form->fhValue($fieldname);
 
// Get value sanitized with given sanitizer
echo $form->fhValue($fieldname, 'pageName');
 
// Get raw value and ignore fhSanitizer setting
echo $form->fhValue($fieldname, false);
}
 
Submit button is added in the beginning of the fhProcessForm() method. Get the submit button to modify it previously...
// Get submit button
$submitBtn = $form->fhSubmitBtn;

Missing features will be moved to FormHelperExtra (if needed)

  • pwImage plugin
  • InputfieldCKEditor stuff (hidden page id field...)
  • form by template / type "Field" fields with a hidden storage page to get file uploads working without saved page
  • ...

FormHelper testing branch 

https://bitbucket.org/pwFoo/formhelper/src/2a7e7261cf347622a82612d381cabdc63bed4c08?at=testing

*UPDATED*

Edited by pwFoo
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

hi pwFoo
 
this error i shown in my module?
 
here's the part in the module when it becomes error
 
 

public function ___install() { 
        $fg = new Fieldgroup();
        $fg->name = self::$tempUploadPage;
        $fg->add('title');
        $fg->save();
        $this->message('FormHelperExtra temp file storage Fieldgroup "' . self::$tempUploadPage . '" created.');
        
        $t = new Template();
        $t->name = self::$tempUploadPage;
        $t->flags = Template::flagSystem;
        $t->noParents = 1;
        $t->fieldgroup = $fg;
        $t->save();
        $this->message('FormHelperExtra temp file storage Template "' . self::$tempUploadPage . '" created.');
        
        $storage = new Page();
        $storage->template = $this->templates->get($t);
        $storage->name = self::$tempUploadPage;
        $storage->parent = $this->pages->get('/admin/');
        $storage->title = self::getModuleInfo()['title'] . 'StoragePage'; // <---  here's the error part
        $storage->addStatus(Page::statusHidden);
        $storage->save();
        $this->message('FormHelperExtra temp file storage Page "' . self::$tempUploadPage . '" created.');
}

Parse Error: syntax error, unexpected '[' (line 170 of D:\home\www\tfdold\public_html\animalbitecenter\site\modules\FormHelper\FormHelper\FormHelperExtra.module)
This error message was shown because you are logged in as a Superuser. Error has been logged.

Link to comment
Share on other sites

That's a limitation of PHP before 5.4

http://php.net/manual/en/language.types.array.php

"As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable."

It's an easy fix to support PHP 5.3 though:

$moduleInfo = self::getModuleInfo();
$storage->title = $moduleInfo['title'] . 'StoragePage';
Link to comment
Share on other sites

Released 0.7.2

  • removed FormHelper php 5.4 dependency (historical, have its seeds in an old version based on anonymous functions instead of PW hooks)
  • FormHelper (0.1.1) php 5.3 compatibility update (thanks to Adrian and naldrocks98 for testing / help / feedback)

But I have to install an php 5.3 test vm for compatibility testing soon... ;)

Link to comment
Share on other sites

  • 2 weeks later...

Version 0.8.2

Moved methods from FormHelperExtra to Formhelper because could be used without FormHelperExtra.

  • jsConfig() and fhCkeditorImagePlugin() needed by InputfiedCKEditor
  • load admin (backend) form styles
Link to comment
Share on other sites

I'm trying this out and having some trouble. In my usecase, I want to generate a form based on a template. Here's my code:

$fh = $modules->get('FormHelper');
$form = $fh->create($templates->get('basic-page'));
echo $form->render();

This is outputting a form without fields except for a hidden token.

Can't figure out what I'm doing wrong.

Link to comment
Share on other sites

Can't find any errors either on the Apache log or PW's.

I'm using PHP 5.6.3 and PW 2.5.29 dev.

Here's the output I'm getting:

<form id="fhForm1" class="InputfieldForm" name="fhForm1" method="post" action="./" data-colspacing="1"><input type="hidden" name="TOKEN1463213210X1433263864" value="fXJQW/rrJn/a1VrW5nGRJXbxA3MrczWb" class="_post_token">
</form>
Link to comment
Share on other sites

I get an empty form (with submit button and hidden token field) if the template not exists... 

There was a commit problem yesterday in the evening... Maybe it's not the latest version.

Could you uninstall, remove and install it again to be sure it's the latest version / master commit?

Link to comment
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
×
×
  • Create New...