Jump to content

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


Juergen

Recommended Posts

Hello @Andy

  On 1/31/2025 at 3:54 PM, Andy said:

Is there any way to tell the form that it is invalid? And make $form->isValid() = false.

Expand  

I will take your issue as an idea for a new feature and I will take a look if I could find a good working solution. At the moment I will wait if there are some unwanted negative side effects concerning the last update. If everything works as expected and no issues occur/will be reported I will go on to the next step and try to find a solution to your issue.

Best regards

  • Like 1
Link to comment
Share on other sites

Good news @Andy

I have taken a look at the Valitron library, which I use to validate form fields and this library offers an easy to use solution to add custom rules. Take a look here.

To add a custom rule to ProcessWire you need to add it to the site/init.php. For demonstration puroses I use the example from the Valitron docs. So please add this code (which always returns false) to your site/init.php


\Valitron\Validator::addRule('alwaysFail', function($field, $value, array $params, array $fields) {
    // here you can write your validation code - this should return true or false
    return false;
}, 'Everything you enter is not correct. You fail.');

Now you have added a new custom rule, which you can use sitewide like all other validation rules.

Here is a form example containing this rule:

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

$inputText = new \FrontendForms\InputText('text');
$inputText->setLabel('Input Text');
$inputText->setRule('required');
$inputText->setRule('alwaysFail'); // here is the new validation rule
$form->add($inputText);

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

if ($form->isValid()) {

}

echo $form->render();

If you validate the form, you will always get the error message "Everything you enter is not correct. You fail." because this rule always returns false.

You can take this example as a starting point for your own validation rule. The validation code must return a boolean value (true or false).

Hope this helps

  • Like 1
Link to comment
Share on other sites

Hi @Juergen

I continue to admire you. Yes, that's exactly what I meant. That's what I was thinking about writing a new validation rule, just haven't decided how to do it yet. I was going to use hooks. You've shown a logical solution. Thank you.

I think FrontendForms users can share their useful validation rules in this thread.

  • Like 1
Link to comment
Share on other sites

Hello @Andy

I will write a new section in the docs in the next time about this possibility.

Please note:

If you want to use API variables inside your custom validation rule I guess you need to add this rule to the site/ready.php instead of the site/init.php. I have not tested it, but I guess you need to do it this way, but you can try it out.

  On 2/1/2025 at 11:32 AM, Andy said:

I think FrontendForms users can share their useful validation rules in this thread.

Expand  

That is a good idea and if a validation rule seems to be useful for others I will add it to the FrontendForms validation rules file, so everyone can use it. I guess there are around 60 validation rules available at the moment, but more is more in this case. 😉

  • Like 1
Link to comment
Share on other sites

  On 2/1/2025 at 9:52 AM, Juergen said:

 

$wire->addHookAfter('Errormessage::render', function(HookEvent $event) {
    $msg = $event->object;
    $errorText = $msg->getText();
    if($errorText){
        $fontAwesome = '⚠ ';
        $event->return = $fontAwesome.$errorText;
    }
});
Expand  

Thank you @Juergen

You accidentaly placed the old Hooking example 2 on the docs which now the font awesome icon displayed always even if there are no errors. Replace it with the above hook which works properly.

  • Like 2
Link to comment
Share on other sites

Hi @Juergen

Good news everybody. Custom validation rules work. I'll add one more proven validation rule to the general collection.

Check to enter a Cyrillic first, last or middle name.

In ready.php

//
// Cyrillic name may contain lowercase and uppercase а-я and hyphen
//
\Valitron\Validator::addRule('cyrillicName', function ($field, $value) {
    $regex = '/[^а-яё\-]/iu';
    if (!preg_match($regex, $value)) {
        return true;
    }
    return false;
},'contains not allowed characters. Cyrillic name may contain lowercase and uppercase а-я and hyphen (no whitespaces).');

 

  • Like 1
Link to comment
Share on other sites

Hello @Andy

Glad that it works like you want!!

  On 2/1/2025 at 1:05 PM, Andy said:

Check to enter a Cyrillic first, last or middle name.

Expand  

For this check, it is not really necessary to write a custom rule. You can also use the regex validation rule and add the regex there, but it is not wrong if you need this validation rule often.😉

  • Like 1
Link to comment
Share on other sites

Hi @Juergen

Thanks for the information. I will read the Valitron documentation more carefully.

However, I need this rule quite often. I think that adding the 'cyrillicName' rule is more correct for the code.

Thanx

Link to comment
Share on other sites

Hello @Andy

I have added your rule as the first community provided validation rule to FrontendForms. Take a look here.

This validation rule is included in the next update, but if you do not want to wait until the next update, you only have to replace the code of the CustomRules.php from Github with yours and you are done.

  • Thanks 1
Link to comment
Share on other sites

Hi @Juergen

Got the ability to make flexible rules and a multi-step input form. The ability to make your own validation rules is a very powerful thing.

Thanks.

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

  • 2 weeks later...

Hello @Frank Vèssia

Thanks for reporting this issue. Unfortunately I cannot reproduce this error in my console.

size() and length should return the same. Take a look at this discussion. Could you please replace the size() function with length to see if the error is gone now. If so, I can replace it on Github too, because it should not have a negative side effect.

Thanks Jürgen

Link to comment
Share on other sites

Hello @Juergen the settings for a custom framework are not working correctly. I am using FrontendForms v2.2.28.

I have a tailwindcss.json file in site/assets/files/FrontendForms/frameworks.

The Tailwindcss option appears in the Output rendering select field.

image.thumb.png.2a4a06a30d76ef2e46683e6445165fd7.png

But when I select it, the output on the frontend uses default classes instead of my tailwind classes.

Even if I enter the custom path "site/assets/files/FrontendForms/frameworks" in the corressponding settings field, it doesn't work.

If I copy my tailwindcss.json file to site/modules/FrontendForms/CSSClasses it works.

Maybe you could look into this issue?

Another hint: It would be good to rename the module to .module.php so it get's correct syntax highlighting, and enables static analyzers or Rector to recognize the file. This is also the recommended way of custom modules (don't know where this is mentioned, but I remember reading it).

Regards, Jens

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