为什么断言跳过验证我的symfony5表单用户名字段?



我的问题涉及用户表单中的一个字段,即用户名。由于断言验证适用于实体中的其他字段,我发现这样的行为很奇怪-跳过断言规则,我确实指向用户实体中的用户名属性并将null属性传递给生成错误的userFormNewHandler,特别是当我发现它与其他字段并没有真正不同时。我想知道,我错过了什么?

UserType.php:

<?php
namespace AppUserBundleFormType;
use AppUserBundleEntityUser;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormExtensionCoreTypeEmailType;
use SymfonyComponentFormExtensionCoreTypePasswordType;
use SymfonyComponentFormExtensionCoreTypeRepeatedType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentValidatorConstraintsNotBlank;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', TextType::class, ['required'=>true,
'invalid_message' => 'Username must not be empty!'])
->add('plainPassword', RepeatedType::class, array(
'type'              => PasswordType::class,
'mapped'            => false,
'first_options'     => array('label' => 'New password'),
'second_options'    => array('label' => 'Confirm new password'),
'invalid_message' => 'The password fields must match.',
'required' => true,
'constraints' => [
new NotBlank([
'message' => 'Password field must not be blank!'
])]
))
->add('active_status', ChoiceType::class, [
'choices'  => [
'Active' => true,
'Inactive' => false,
],])
->add('first_name',TextType::class, [
'required'=>true, 'invalid_message' => 'First name must not be empty!'])
->add('last_name', TextType::class, [
'required'=>true, 'invalid_message' => 'Last name must not be empty!'])
->add('email', EmailType::class, [
'required'=>true, 'invalid_message' => 'Email must not be empty!']);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
这是我的user。php实体:
<?php
namespace AppUserBundleEntity;
use AppUserBundleRepositoryUserRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentValidatorConstraints as Assert;

/**
* @ORMEntity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=180, unique=true)
* @AssertNotBlank(message="Fill username field")
*/
private $username;
/**
* @ORMColumn(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORMColumn(type="string")
*/
private $password;
/**
* @ORMColumn(type="boolean")
*/
private $active_status;
/**
* @ORMColumn(type="string", length=30)
* @AssertNotBlank(message="Fill first name field")
*/
private $first_name;
/**
* @ORMColumn(type="string", length=30)
* @AssertNotBlank(message="Fill last name field")
*/
private $last_name;
/**
* @ORMColumn(type="string", length=40)
* @AssertNotBlank(message="Fill email field")
*/
private $email;
/**
* Representation of account status
*/
public function getActiveStatus(): bool
{
return $this->active_status;
}
/**
* Setting account status
*/
public function setActiveStatus(bool $active_status): self
{
$this->active_status = $active_status;
return $this;
}
/**
* Representation of username
*/
public function getUsername(): string
{
return (string) $this->username;
}
/**
* Setting username for user
*/
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
//...

和_form.html。小枝,窗体在这里被渲染:

{{ form_start(form, { attr: {novalidate: 'novalidate'} }) }}
{{ form_widget(form) }}
<button class="btn" >{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

我找到了!这是因为我确实指出setter用户名的方法严格寻找字符串,我想这阻止了我传递空属性的形式。现在PHP验证可以正常工作了:)

零件生成错误:

User.php

/**
* Setting username for user
*/
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}

修复:

/**
* Setting username for user
*/
public function setUsername($username): self
{
$this->username = $username;
return $this;
}

相关内容

  • 没有找到相关文章

最新更新