bolt / forms

MIT License
21 stars 27 forks source link

Symfony config parameters cannot be used in form configuration #99

Open benr77 opened 2 years ago

benr77 commented 2 years ago

Bolt Forms 1.4.17

I'm using the reCaptcha v3 field type and need to specify the Google API keys. Currently it seems that you cannot use any Symfony configuration parameters or environment variables in this config file, like you can with Symfony YAML configuration, so I'm forced to hard-code the API keys, meaning they get stored in the Git repo and are visible to all.

Is this a known limitation?

Thanks

david-saisondor commented 2 years ago

Hello, Symfony uses a compiler pass (ResolveParameterPlaceHoldersPass()) for parsing parameters placeholders and replace them with actual values. As far I know, Bolt Forms doesn't store his configurations in Symfony's container at all. It's YAML parser is a simple service, injected in every place the configuration is needed, and the YAML files are parsed on demand. Conclusion : no use of container = no compiler pass = no placeholders params replacement.

benr77 commented 2 years ago

Thanks for your reply. I can see what you are referring to.

This does seem rather strange to me, as Bolt is based on Symfony, and yet has rolled it's own configuration reading instead of just using the default Symfony config system.

As a consequence, I have to hard-code API keys into files instead of using environment variables or even the Symfony Secrets system.

Bolt Forms could be a Symfony bundle, and have a standard bundle configuration file. I don't know the reasoning behind why this is not the case, but I'm guessing @bobdenotter will be able to shed some light on this.

david-saisondor commented 2 years ago

I encounter the same problem as you : I have 4 forms with ReCaptcha, and I have to centralize Google keys. I've not tested it yet, but there is a possible solution :

  1. Write you keys in .env.local or the server environment vars
  2. In service.yaml, define a couple of parameters based on two previous var params
  3. In YAML forms config, use dummy params for ReCaptcha keys
  4. In your own implementation of @boltforms/form.twig, use the configuration overrides capabilities of boltforms() Twig function, to pass the keys to the ReCaptcha fields, since Twig has access to the params container.
  5. Take a well deserved rest.
benr77 commented 2 years ago

Aha that looks interesting (the config overrides) - thanks. I'll give that a try when I'm next back on that project.