-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input validation bug: Must be of type string, null given #171
Comments
It's no just if you enter spaces - it's also a problem if a browser does not respect the HTML5 validation. In fact there is no server-side validation of any user input on any form. |
There are tow options to "fix" this problem: a) Make the property nullable in Contact model. This will break the clean code which you try to archive via PHPStan. Additionally the server side validation should be added. https://symfony.com/doc/current/validation.html#constraints-in-form-classes |
Good catch, not sure how I missed the form validation. I'll put it on my list to add proper server side validation to all forms :). And I prefer option B, empty string as default if no value has been entered. I don't like to make the datamodel less strict by allowing null, as that should never be an accepted value. More strict and clean is always better |
So I kind of stumbled into your project while looking up other Symfony browser games on GitHub, and thought I'd drop one fo the ways my project fixed this, which follows the Symfony guides from v5 (confirmed to work with v6), using your registration form as an example: # src/Form/RegistrationType.php
use Symfony\Component\Validator\Constraints\Length;
...
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'email',
EmailType::class,
[
'label' => 'label.email'
]
)
->add(
'username',
TextType::class,
[
'label' => 'label.username'
]
)
->add(
'plainPassword',
RepeatedType::class,
[
'type' => PasswordType::class,
'first_options' => [
'label' => 'label.password'
],
'second_options' => [
'label' => 'label.password_repeat'
],
'constraints' => [
new Length([
'min' => 8,
'minMessage' => 'password.must.be.eight.char.min',
'max' => 4096,
]),
],
]
)
->add(
'agreeTerms',
CheckboxType::class,
[
'mapped' => false,
'label' => 'label.accept_rules'
]
)
->add(
'captcha',
ReCaptchaType::class,
[
'mapped' => false,
'type' => 'checkbox' // (invisible, checkbox)
]
)
->add(
'submit',
SubmitType::class,
[
'label' => 'label.register'
]
);
} This would be validated by the $form->isValid() check automatically, and limit your efforts to just the forms, and not also the controllers. Max length is 4096 per https://symfony.com/doc/current/security/passwords.html#creating-a-custom-password-hasher |
Several TypeErrors have been reported in server log. I'm able to reproduce these errors by using spaces as input. Several forms impacted, tested and reproduced on both contact form and registration form.
The text was updated successfully, but these errors were encountered: