我正在使用FOS用户捆绑包,我添加了一些自定义字段(名称,姓氏,地址......)以及我在validation.yml配置中所标记的字段(如下所示)。
问题是,在注册表单中,当我将这些字段留空时,我尝试提交表单我不能,因为它们具有所需的html标记加上minlen,maxlen属性(下面)
所以捆绑似乎正在正确使用配置文件。
但这是我发现的问题。我正在使用behat来测试应用程序,我创建了一个测试来验证帐户创建是否有效。在其中一个场景我故意留下名字,姓氏空白,所以我应该看到和错误消息。问题是表单是发送的,因为mink不关心html验证,并且名称和姓氏中的空值未经过验证。一切都以数据库异常结束,尝试在必填字段中为空值插入空白值:
An exception occurred while executing 'INSERT INTO user (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, name, surname, company, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["test@dirital.com", "test@dirital.com", "test@dirital.com", "test@dirital.com", 0, "3njo6hthk8ao040okco08wggk4sk4wk", "namJcPop4lXUwAK4OQXIzRsfKYurv9K+XdvVMTZ4CSEvMf+bLTl4oYTFSKExMI1ROj557mjR89ltnj6L0ETBXA==", null, 0, 0, null, "o7V72h5PK12VlAZBAVH8QSK6PPsOYL9--iVEUTKE8XQ", null, "a:0:{}", 0, null, null, null, null, null]:
│
│ SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: user.name
我有另一个测试,我测试如果我留下空白的电子邮件和密码,我应该收到一条错误消息,并且这些消息正常工作。
所以在验证中我的配置似乎有问题或类似但我不能把手指放在它上面。
validation.yml
UserBundle\Entity\User:
properties:
name:
- NotBlank:
message: fos_user.field.blank
groups: [ "UserRegistration", "UserProfile" ]
- Length:
min: 2
minMessage: fos_user.field.short
max: 100
maxMessage: fos_user.field.long
groups: [ "UserRegistration", "ResetPassword" ]
surname:
- NotBlank:
message: fos_user.field.blank
groups: [ "UserRegistration", "DiritalUserProfile" ]
- Length:
min: 2
minMessage: fos_user.field.short
max: 100
maxMessage: fos_user.field.long
groups: [ "UserRegistration", "ResetPassword" ]
表单中生成的HTML验证代码 名称
</div>
<div class="form-group">
<label class="control-label required" for="fos_user_registration_form_surname">Surname</label>
<input type="text" id="fos_user_registration_form_surname" name="fos_user_registration_form[surname]" required="required" maxlength="100" pattern=".{2,}" class="form-control" />
</div>
behat测试:
Scenario: When the user doesn't fill all the fields of the form error messages should appear
Given I am on "/create-account/"
When I press "Send"
# This works
Then I should see "The email is required to create your account"
When I fill in "Email" with "test@domain.com"
And I press "Send"
# This works as well, only the password error shown, the email field has been filled
Then I should not see "Please fill in the email"
And I should see "Please fill in the password"
When I fill in "Password" with "12345"
And I press "Send"
# This also works, different passwords should not be sent
Then I should see "The entered passwords don't match"
When I fill in "Password" with "12345"
And I fill in "Repeat password" with "12345"
And I press "Send"
# And there is where I get the exception instead the error message for not filling the name, surname fields
And print last response without tags