我目前正在使用Symfony的验证组件作为独立工具。我正在使用Lumen和Doctrine2构建一个API,我试图找出是否有办法在Symfony2框架之外使用UniqueEntity约束。我跟着this example尝试使用UniqueEntity,但我注意到名称空间Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
甚至不存在于独立包中。有没有其他人得到这项工作,还是我必须编写自己的唯一性验证器?
答案 0 :(得分:0)
我使用yml创建验证,您也可以创建介绍模型。
就我而言,这是一个例子:
# src/SClinicBundle/Resources/config/validation.yml
Cf\SClinicBundle\Entity\CfIndicationsTemplates:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: name
message: cf.indications_template.name.unique
properties:
name:
- NotBlank: { message: "cf.indications_template.name.not_blank" }
- Length: { min: 2, max: 100, minMessage: "cf.indications_template.name.min_length", maxMessage: "cf.indications_template.name.max_length" }
templateIndication:
- NotBlank: { message: "cf.indications_template.templateIndication.not_blank" }
status:
- NotBlank: { message: "cf.indications_template.status.not_blank" }
- Choice: { choices: [0, 1], message: "cf.indications_template.status.choices" }
datetimeR:
- DateTime: { message: "cf.indications_template.datetime_r.bad_datetime" }
Cf\SClinicBundle\Entity\CfReportTemplates:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: name
message: cf.report_template.name.unique
properties:
name:
- NotBlank: { message: "cf.report_template.name.not_blank" }
- Length: { min: 2, max: 100, minMessage: "cf.report_template.name.min_length", maxMessage: "cf.report_template.name.max_length" }
status:
- NotBlank: { message: "cf.report_template.status.not_blank" }
- Choice: { choices: [0, 1], message: "cf.report_template.status.choices" }
datetimeR:
- DateTime: { message: "cf.report_template.datetime_r.bad_datetime" }
Cf\SClinicBundle\Entity\CfMedicalStudy:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: name
message: cf.medical_study.name.unique
properties:
name:
- NotBlank: { message: "cf.medical_study.name.not_blank" }
- Length: { min: 2, max: 100, minMessage: "cf.medical_study.name.min_length", maxMessage: "cf.medical_study.name.max_length" }
administerDosesZone:
- NotBlank: { message: "cf.medical_study.administerDosesZone.not_blank" }
status:
- NotBlank: { message: "cf.medical_study.status.not_blank" }
- Choice: { choices: [0, 1], message: "cf.medical_study.status.choices" }
datetimeR:
- DateTime: { message: "cf.medical_study.datetime_r.bad_datetime" }
您可以查看我的示例,并尝试与您的代码进行比较。