Jump to content

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


Juergen

Recommended Posts

I have tried to find a possible cause for the problem by using a static method, but I did not find one. Maybe it will be caused by the _n() function of ProcessWire, which will be used inside this method (but this is only thought).

Anyway, I have changed the method back to non-static.

Could you please copy the code of the file below to your FormValidation.php

FormValidation-new.php

If it works for you, I will add it to GitHub and bump up the module version, so you can update the module as usual.

Link to comment
Share on other sites

Thank you for your feedback!!

That was a real struggle, but we get it ?!

The only side effect is that I have to update one of my other modules (FrontendLoginRegister) too, because it uses the static method. Otherwise it will not work if 2-factor-authentification is enabled. So I have to add the same method to this module too to get it working flawlessly.

To do now write the same method twice was the reason, why I have switched the method to static in the past.

I will update the other module first and after that I will bump up the version of FrontendForms too.

Cu

Link to comment
Share on other sites

FrontendForms update 2.1.55

Bug fixed on min time configuration input field (name attribute was missing)

Static function secondsToReadable() will be reverted to non-static. The static function leads to some problems under certain conditions, so the best way was to revert it back to non-static (read the discussion above).

IMPORTANT: This has an impact on the FrontendLoginRegister module if you are using 2-factor-authentification, because this module uses the static function. If you are using the FrontendLoginRegister module, please update this module first (or at the same time with FrontendForms) to prevent problems.

Thank you!

  • Like 1
Link to comment
Share on other sites

Thanks!

Doing more exploration I narrowed it down further that the issue is caused by using PW translation functions inside your static function: _() and _n(). Removing these it can be used as a static function and doesn't cause the error. But I guess that defeats the whole point to have time strings translated...

  • Like 1
Link to comment
Share on other sites

That is exactly what comes to my mind too, but I do not know why it works on my side, but not on yours. Thank you for tracing it down to its source. Maybe _() and _n() function has to be written in another way inside static functions or they cannot be used without problems.

Link to comment
Share on other sites

Hello @da²

No, we have both 8.2 installed (but different local machines) and I have not discovered any problems on my side. So it is a little bit mysterious.

Do you use the same syntax in your static methods: _() and _n() instead of $this->_() and $this->_n() ?

Link to comment
Share on other sites

There's always an error message recorded somewhere, if not in PW logs it's probably in web server logs.

If @donatas is using Xampp on Windows, Apache logs are in xampp/apache/logs/error.log.

Trying to understand an error without knowing the message is quite challenging. ?

EDIT : But he should first look in PW Exception|Error logs.

  • Like 1
Link to comment
Share on other sites

Hi @da² PW shows no error log for the crash. But I looked into httpd Apache error_log file and get this after site errors out:

Quote

objc[18354]: +[__SwiftNativeNSStringBase initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.

I work locally on a Mac so it very much might be a bug on platform specific implementation, as I see "SwiftNative" mentioned in the log. Would be interesting to see if other Mac users are experiencing this or it's just a super specific issue on my end ?‍♂️

  • Like 2
Link to comment
Share on other sites

Thanks @donatas for sharing the log file with us. It contains not really useful information for me, but maybe other users can interpret the error message and what has been going on. As I can see, reverting the secondsToReadable() method from static to non-static was the safest step we could take to prevent the issue.

Link to comment
Share on other sites

13 hours ago, da² said:

At first instance I don't feel this is related with using static method in PHP

The static method is not really the problem in this case, but the translateable functions (_(), _n()) inside the static method cause the issue as @donatas pointed out:

20 hours ago, donatas said:

Doing more exploration I narrowed it down further that the issue is caused by using PW translation functions inside your static function: _() and _n(). Removing these it can be used as a static function and doesn't cause the error. But I guess that defeats the whole point to have time strings translated...

But I need the translatable strings inside this method for the return statement, so I had to switch back to a non-static method.

This was not the first problem with a Mac. I can remember at very early versions of this module, a user who uses a Mac had path problems, so he got a lot of errors that classes (or better the files) could not be found.

Link to comment
Share on other sites

hi juergen,

is there an iframe-restriction on submitting a form?

i have a form that is included in an iframe on another domain.

but what works perfect on the main web does not work on the iframe domain. instead of getting the (ajax) success-message i just return to the form. and the form is empty again.

any ideas about that?

best regards!

 

 

Link to comment
Share on other sites

Good morning @herr rilke

To be honest, I have not tested the form submission inside an iframe before.

If you are using an iframe, using Ajax is obsolete, because you will not get a page reload as far as I can remember (I have not used iframes for more than 10 years, so I am not 100% sure), because the form is embedded and not directly inside the code of the page.

Please disable Ajax submission in this case and try it again.

You can disable the Ajax on this form by using the following code:

 $form->setSubmitWithAjax(false); // can be true or false

Let me know if it works now.

Best regards

Link to comment
Share on other sites

@herr rilke

I have tried to submit a form inside an iframe on my localhost and it works with or without Ajax in my case.

But I have to say that the form is on the same domain in this case: The form itself is located on the homepage and I have embedded it via iframe inside a childpage on the same domain.

iframe.thumb.jpg.b451a14745c83c14e9b162b0921b1526.jpg

Unfortunatelly, I cannot test it on different domains. Please check if it works, if you embed the form on the same domain.

Only to mention: The form, you will see in the image is another form and not the one that has been submitted (its from a new module, for creating comments based on FrontendForms).

Link to comment
Share on other sites

hello juergen,

thanks for your quick reply!

but with

 $form->setSubmitWithAjax(false); // can be true or false

i get a server error 500 just while trying to open the page.

oh, wait, updated FrontendForms

2.1.46 => 2.1.55

now i can call the page.

on submit i get a success-message (instead of displaying the form again) on the home domain.
but no change on the iframe domain. and if i try to watch the developer tool's network tab, it looks as if there is a XHR request followed by a reload of the page.

is there a way to be sure to quit ajax calls? can i set a page to redirect to after form submission?

btw:
i had this setup before (calling a form from another domain managed by another CMS, which worked nicely) that's why i wonder if your module could handle things differently than the software before.

all the best

karsten

Link to comment
Share on other sites

38 minutes ago, herr rilke said:

is there a way to be sure to quit ajax calls?

I am using Tracy and I can see if an Ajax call will be taken or not - in my case there is no Ajax call at all if I disable Ajax on per form base. So if there is no Ajax call on the home domain, then there is no Ajax call in the iframe.

Please be sure to clear all caches and refresh modules (just to play safe). I have no explanation why the JavaScript code for the XHR request runs. Maybe caching problem??

41 minutes ago, herr rilke said:

can i set a page to redirect to after form submission?

Of course, you only need to add the redirect on the last line inside the isValid() method:

$form->isValid(){
  ...
  ...
  $session->redirect('path/to/my/page'); // or you can use an URL
}

 

44 minutes ago, herr rilke said:

that's why i wonder if your module could handle things differently than the software before.

No, it should work like other forms.

Link to comment
Share on other sites

Update 2.1.56

This update comes with 2 fixes and 2 new methods.

  • Missing declaration for property $toplabel of type Textelement inside InputCheckboxMultiple.php added

  • Change CAPTCHA label for CAPTCHA validation message to fit better - PLEASE UPDATE YOUR LANGUAGE FILE FOR THE NEW TRANSLATION

  • New method setRedirectURL() added:

    This method forces a redirect after the form has been validated and the code between the isValid() condition has been executed. This can be used to redirect fe to a "Thank you" page after a contact form has been submitted (read more). It does not matter in this case if you are submitting the form via Ajax or not. Therefore the old setRedirectWithAjax() method is no longer needed (but still works)

  • New alias method useAjax() added: This method is an alias method for the existiting setSubmitWithAjax() method.

    The old method forces a form submission via Ajax. The name of the old method is too long, so I decided to create a method with a better and shorter name.

    So the old method still works, but for the future it will be better to use the new useAjax() method instead (read more).

As always, please check if everything is working fine after the update!!

  • Like 1
Link to comment
Share on other sites

@Juergen thanks for the module.
I tried this:

$form = new \FrontendForms\Form('myForm');		

$file1 = new \FrontendForms\InputFile('fileupload1');
$file1->setLabel('Multiple files upload');
$file1->setMultiple(true);
$file1->setRule('allowedFileSize', '100000');
$file1->setRule('allowedFileExt', ['jpg','jpeg','png']);
$form->add($file1);

$button = new \FrontendForms\Button('submit');
$button->setAttribute('value', 'Send');
$form->add($button);

if($form->isValid()){
	print_r($form->getValues());
}
		
echo $form->render();

But it renders this for the fileupload:

<form method="post" action="/path/" id="myForm" name="myForm-1705439668" novalidate="" autocomplete="off" class="form" enctype="multipart/form-data"><div class="fieldwrapper" id="myForm-fileupload1-fieldwrapper">
	<label class="label" for="myForm-fileupload1">Multiple files upload</label>
	<div class="inputwrapper" id="myForm-fileupload1-inputwrapper">
    	<input id="myForm-fileupload1" name="myForm-fileupload1[]" type="file" class="input input-file" multiple="" onchange="showClearLink(event)" accept=".jpg,.jpeg,.png" max-size="2048">
		<div id="myForm-fileupload1-clearlink-wrapper" class="clear-link-wrapper" style="display:none;">
        	<a id="myForm-fileupload1-clear" href="#" class="clear-link" onclick="event.preventDefault();clearInputfield(this); return false;">Clear the input field</a>
        </div>
  	</div>
	<p class="notes">Please do not upload files larger than 2,0 kB<br>Allowed file types: jpg, jpeg, png</p>
</div></form>

Do I have set the attribute allowedFileSize in another way, so that it allows larger files?

  • Like 1
Link to comment
Share on other sites

Good Morning @ngrmm

By default, the file upload field takes care about your max upload file size value that has been set inside your php.ini (server config file).

https://www.php.net/manual/en/configuration.file.php

2048 is the max file upload size as set inside your php.ini in this case.

You can disable this behavior by adding the following rule to your file input field:

$file1->removeRule('phpIniFilesize');

This disables the check of the php.ini max file size and you will get the desired result, but it is not the recommended way. If you have access to your server php.ini file, make your changes there and set a higher value for the max upload file size.

If you have a local installation (fe. Xampp), you can change it by yourself, if you have a site on a shared host, maybe you will need to contact the server admin, if he can change the max. value for file uploads or you can login to your account and make your changes there.

Hope this helps

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