Closed donatasben closed 8 months ago
I guess these are two separate issues:
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.
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
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.
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:
How the option is set:
$input->addOption(' ', ' ');
How the option is output:
<option value=" " selected> </option>