juergenweb / FrontendForms

A module for ProcessWire CMS to create and validate forms on the frontend easily using the Valitron library.
MIT License
15 stars 1 forks source link

Select input needs option for "nothing" selected / validation bug when option value is "space" char. #5

Closed donatasben closed 8 months ago

donatasben commented 8 months ago

I was trying to quickly set up a select input and wanted it to have an empty option - no visible placeholder/label, value would be something that would be easy to filter out as "not selected". It's important that if the field is required - nothing would be preselected for user. If nothing would be chosen - it would show an error.

Not sure how to add a custom validation rule for this - could you give an example how to do it?

If I tried to use a "space" character as value for option - at first submit it shows a good error. But if I still don't choose anything again and click submit button - it throws this error (option value is a "space" character:

TypeError: FrontendForms\Form::encryptDecrypt(): Argument #1 ($string) must be of type string, null given, called in .../FrontendForms/Formelements/FormValidation.php on line 93

How the option is set: $input->addOption(' ', ' ');

How the option is output: <option value=" " selected> </option>

donatasben commented 8 months ago

I guess these are two separate issues:

  1. Request for empty select option
  2. TypeError on secondary submit while there were errors - I'm getting it almost always now on second submit after errors, no matter if all fields are now filled and valid, or not valid again.
juergenweb commented 8 months ago

Hello Donatas

I am not sure if I understand what you want to achive and why the empty option should not have a label.

If you want to add an empty option, there is an inbuilt method. Take a look at the following example:

$php = new \FrontendForms\Select('php'); $php->setLabel('My preferred PHP version is'); $php->addEmptyOption(); $php->addOption('PHP 6', 'PHP 6'); $php->addOption('PHP 7', 'PHP 7'); $php->addOption('PHP 8', 'PHP 8'); $php->setRule('required'); $form->add($php);

This adds "-" as default label, if nothing is entered inside the parenthesis. To get a completely empty label, you will need to add an empty label option like this:

$php->addEmptyOption(' ');

The addEmptyOption() method works with validation.

juergenweb commented 8 months ago

Hello Donatus,

I have tried your code with

$input->addOption(' ', ' ');

but I have not used $input, but another variable name.

$fieldname->addOption(' ', ' ');

This works without problems.

$input is the variable name for the ProcessWire Input class and therefore could lead to problems. So please try it with another variable name instead.

If your problem is solved, you can close this issue report.

Best regards Jürgen

donatasben commented 8 months ago

Thanks Juergen! $input->addEmptyOption(' ') was just what I needed! Not sure how I missed it in docs :)

P.S. $input variable is set and used within a foreach, so no issues with the name.

However I'm still struggling with some errors or unexpected behaviour on submit... But I'll create a new issue as it's quite removed from this one.