Jump to content

FrontendForms - A module for creating and validating forms on the frontend


Juergen

Recommended Posts

This issue has nothing to do with the module itself, but it seems that the $_SERVER array is not loaded at that moment.

You can try the following: 

// Replace the following line inside the FrontendForms.module file on line 858

if (strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0) {

with this line

if (!is_null($_SERVER) && strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0) {

Let me know if it works

Link to comment
Share on other sites

This gives the same error.

I fill this is related with the module because this is not the first time I bootstrap PW and never had an issue before to install this module.

You are using REQUEST_URI, but when bootstrapping, isn't it expected that it's not defined?

It's easy to test, just include PW index.php in any script and run it directly (no web page involved).

Edited by da²
Link to comment
Share on other sites

Then try the following:

if (!is_null($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0) {

Have not tried it with bootstrapping but the code inside the if condition is not important for running forms. It is only there to add CSS and JS files to the backend, so you can prevent the running of the code inside the condition by checking the existence of $_SERVER['REQUEST_URI'] first.

Link to comment
Share on other sites

On 8/30/2025 at 9:59 PM, Juergen said:
if (!is_null($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0) {

This gives a warning, better like this:

if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0) {

Will you publish a fix?

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hello,

Is there a way to put some fields into a div, so I can manage form layout more precisely? Is it with the FieldsetOpen/FieldsetClose elements?

EDIT:

Another thing, after form submission I'd like the form to be still displayed for another use, it is used to filter data in a table so it should always be visible. Actually after submission the form is hidden and replaced with the message "Thank you for your message.".

I tried to use $form->showForm(true); but there is a bug, the value shown in the form is not always the value I selected before to submit (in a Select element).

Edited by da²
Link to comment
Share on other sites

7 minutes ago, da² said:

Is there a way to put some fields into a div, so I can manage form layout more precisely?

Of course, there are several options. Which one you should use depends on your preferred output.

1) Wrap multiple inputfields inside a fieldset

Take a look here for more information or here for a working example. Sometimes a fieldset is the best option to wrap some inputfields syntactically correct.

2) Wrap multiple inputfields within a div container by using the append() and prepend() methods

Take a look here for more information and here is an example using this methods.

If you want to wrap for example 4 inputfields inside an extra div container, the idea is to add the opening div tag via the prepend method to the fist field and the closing div tag via the append() method to the last input field.

3) Use the setMarkUp() method to add the opening and closing div

I think this is the most practicable method for you if you do not want to use a fieldset. You will find more information here.

Example for wrapping multiple fields between 2 div tags:

$markup = new Markup();
$markup->setMarkup('<div>'); // opening div
$form->add($markup);
  
// here enter your inputfields

 	....
  	....
  
$markup = new Markup();
$markup->setMarkup('</div>'); // closing div
$form->add($markup);

Best regards

  • Like 1
Link to comment
Share on other sites

Thank you @Juergen

I edited my message with more questions while you were answering:

  • Another thing, after form submission I'd like the form to be still displayed for another use, it is used to filter data in a table so it should always be visible. Actually after submission the form is hidden and replaced with the message "Thank you for your message.".
    I tried to use $form->showForm(true); but there is a bug, the value shown in the form is not always the value I selected before to submit (in a Select element).
  • And another question, following the documentation I added $form->setMethod('get'); but get an error: "::setMethod does not exist or is not callable in this context"
  • Like 1
Link to comment
Share on other sites

12 minutes ago, da² said:

And another question, following the documentation I added $form->setMethod('get'); but get an error: "::setMethod does not exist or is not callable in this context"

I found in your code that it is $form->setAttribute('method', 'get'); (documentation needs an update 🙂 ). But now the page is empty after submission, empty head and body. I don't understand what's happening.

EDIT: Looks like there is an exit() somewhere, because no code in my template is executed after $form->isValid() when I use GET method. 🧐

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

25 minutes ago, da² said:

I tried to use $form->showForm(true); but there is a bug, the value shown in the form is not always the value I selected before to submit (in a Select element).

Strange in my case it shows exactly the value that I have chosen before the form submission. Is this a simple select element or a select multiple in your case?

Before submission:

image.png.245d962cd44c97f4b22ea9ddaeef3cef.png

 

After submission

image.png.09390f197f2879a1e46c0050774cf4cc.png

 

As you can see the value "custom" is before and after the submission present.

Link to comment
Share on other sites

21 minutes ago, da² said:

EDIT: Looks like there is an exit() somewhere, because no code in my template is executed after $form->isValid() when I use GET method.

I can confirm this behavior but I must take a closer look to find out what is happening. 🤔I am always using POST and not GET in my forms, so I have not discovered this issue until now.

  • Like 1
Link to comment
Share on other sites

It's a simple Select:

image.png.ebf7ae12584b19cb7afc5613ae7dbad8.png

 

I tried again and find this is a random bug, sometimes it works, sometimes not. I made a video, we don't see the dropdown because I recorded only Firefox window, but I can confirm the list is always rendered in the same order as the top line "Mystery,PKO,Vanilla,", that is the order used to fill the Select element, so this is not a problem caused by a random order when initializing the form.

 

 

  • Like 1
Link to comment
Share on other sites

Ok, I found the cause for the white page: The CSRF validation is responsible for it.

Fast fix:

Please replace the this line inside the Form.php inside the isValid() method with this line

if((strtolower($this->getAttribute('method')) === 'get') || $validation->checkCSRFAttack($this->getCSRFProtection())){

This disables the CSRF validation if GET is chosen as the method. This is only a fast fix to get it working, but I must dive a little deeper to find out why the CSRF session always returns false in this case. 

  • Like 1
Link to comment
Share on other sites

enum TournamentType:string
{
    case MYSTERY = "Mystery";
    case PKO = "PKO";
    case VANILLA = "Vanilla";
}

class FilterSolvesForm extends AbstractFrontendForm
{
    protected function buildForm(): void
    {
        $tournamentTypeSelect = new Select("tournamentType");
        $tournamentTypeSelect->setLabel(__("Type de tournoi"));

        foreach (TournamentType::cases() as $tournamentType) {
            $tournamentTypeSelect->addOption($tournamentType->value, $tournamentType->value);
        }

        $this->add($tournamentTypeSelect);
    }
}

abstract class AbstractFrontendForm extends Form
{
    public function __construct(string $id)
    {
        parent::__construct($id);

        $this->buildForm();
        $this->addSubmit();
    }

    private function addSubmit():void
    {
        $button = new Button('submit');
        $button->setAttribute('value', __("Valider"));
        $this->add($button);
    }

    abstract protected function buildForm(): void;
}

// TEMPLATE PHP FILE

$command = new SelectSolvesDatabaseCommand(1, 10);
$filtersForm = new FilterSolvesForm("solvesFilterForm");

if ($filtersForm->isValid()) {
//    DevUtils::prettyPrintObject($filtersForm->getValue('tournamentType'));
//    $command->addFilter('tournamentType', $filtersForm->getValue('tournamentType'));
}

$filtersForm->showForm(true);

$command->execute();
$solves = $command->getPaginatedSolves();

Twig::render('browse-solves',
    [
        'paginatedSolves' => $solves,
        'filtersForm' => $filtersForm,
    ]
);

 

Link to comment
Share on other sites

27 minutes ago, Juergen said:

This is only a fast fix to get it working, but I must dive a little deeper to find out why the CSRF session always returns false in this case. 

OK, this CSRF method only works on POST request (take a look here). I will try to find a solution and provide a fix in the next update to get it working with GET requests too.

  • Like 1
Link to comment
Share on other sites

@Juergen I am experimenting with your module to see if it fits my needs, I was wondering if you forgot to include the "InputSelect" and "InputSelectMultiple" Classes in your module?

I get an error when trying to create a select field and the class file is missing in the InputElements folder:

Error

Class "FrontendForms\InputSelect" not found

 

  • Like 1
Link to comment
Share on other sites

29 minutes ago, Stefanowitsch said:

I get an error when trying to create a select field and the class file is missing in the InputElements folder:

That is because you are calling the wrong class names: The correct class names are "Select" and "SelectMultiple".

Take a look here for a Select and here for a SelectMultiple.

Best regards

  • Like 1
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...