API-平台,执行 POST 时出错。尽管数据已发送,但断言触发器
API-Platform, Error when doing POST. Assert trigger despite that the data is sent
当我使用 API-平台 (Swagger/Postman) POST 新数据时出现错误。有人可以解释吗?
我有一个按预期工作的固定装置。
curl -X POST "http://localhost:8080/countries" -H "accept: application/json" -H "Content-Type: application/json" -d "[{\"name\":\"Aruba2\",\"iso2\":\"ZZ\",\"iso3\":\"ZZZ\",\"region\":\"Americas\",\"subregion\":\"Caribbean\",\"capital\":\"Oranjestad\",\"latitude\":12.5,\"longitude\":-69.96666666,\"isEc\":false,\"isEfta\":false}]"
数据结构合理且有效。
我收到的错误
{
"type": "https://tools.ietf.org/html/rfc2616#section-10",
"title": "An error occurred",
"detail": "name: The country name should not be blank\nname: The country name should not be null\niso2: The iso-2 cannot be blank\niso3: The iso-3 cannot be blank\nregion: The region cannot be blank\nsubregion: The sub-region cannot be blank\ncapital: The capital cannot be blank\nlatitude: The latitude cannot be blank\nlongitude: The longitude cannot be blank\nisEc: The field cannot be blank\nisEfta: The field cannot be blank",
"violations": [
{
这是实体代码。
/**
*
* @ApiResource(
* iri="https://schema.org/Country",
* routePrefix="/",
* shortName="Countries",
* description="API Access to Country Entity",
* normalizationContext={"groups"={"country:read"}},
* denormalizationContext={"groups"={"country:write"}},
* collectionOperations={"GET","POST",
* "GETLIST"={
* "method"="GET",
* "description"="Retrieves a limited set of properties of Country resources",
* "path"="country_list_short",
* "normalization_context"={"groups":"country:list:short:read"}
* }
* }
* )
*
* @ORM\Entity(repositoryClass=CountryRepository::class)
*
*/
class Country extends AbstractEntity
/**
*
* @ORM\Column(
* name="name",
* type="string",
* length=128,
* nullable=false,
* options={"comment":"Country name (English)"}
* )
*
* @Assert\Unique(message="The name {{ value }} already in the table")
* @Assert\NotBlank(message="The country name should not be blank")
* @Assert\NotNull(message="The country name should not be null")
* @Assert\Length(
* min="2",
* minMessage="The Country name expected minimum length is {{ limit }}",
* max="128",
* maxMessage="The Country name expected maximum length is {{ limit }}"
* )
*
* @Groups({"country:read","country:write","country:list:short:read"})
*/
private $name;
您发送的是 JSON 数组而不是 JSON 对象。
只需删除前置和附加的方括号:
{\"name\":\"Aruba2\",\"iso2\":\"ZZ\",\"iso3\":\"ZZZ\",\"region\":\"Americas\",\"subregion\":\"Caribbean\",\"capital\":\"Oranjestad\",\"latitude\":12.5,\"longitude\":-69.96666666,\"isEc\":false,\"isEfta\":false}
编辑:
您还使用了 @Assert\Unique
注释。这是个错误。此注释必须是 used with collections. Given the error message and your property type, you want to use @Assert\UniqueEntity
`,这是一个 class 约束检查 属性 在 table.
中是否唯一
当我使用 API-平台 (Swagger/Postman) POST 新数据时出现错误。有人可以解释吗? 我有一个按预期工作的固定装置。
curl -X POST "http://localhost:8080/countries" -H "accept: application/json" -H "Content-Type: application/json" -d "[{\"name\":\"Aruba2\",\"iso2\":\"ZZ\",\"iso3\":\"ZZZ\",\"region\":\"Americas\",\"subregion\":\"Caribbean\",\"capital\":\"Oranjestad\",\"latitude\":12.5,\"longitude\":-69.96666666,\"isEc\":false,\"isEfta\":false}]"
数据结构合理且有效。
我收到的错误
{
"type": "https://tools.ietf.org/html/rfc2616#section-10",
"title": "An error occurred",
"detail": "name: The country name should not be blank\nname: The country name should not be null\niso2: The iso-2 cannot be blank\niso3: The iso-3 cannot be blank\nregion: The region cannot be blank\nsubregion: The sub-region cannot be blank\ncapital: The capital cannot be blank\nlatitude: The latitude cannot be blank\nlongitude: The longitude cannot be blank\nisEc: The field cannot be blank\nisEfta: The field cannot be blank",
"violations": [
{
这是实体代码。
/**
*
* @ApiResource(
* iri="https://schema.org/Country",
* routePrefix="/",
* shortName="Countries",
* description="API Access to Country Entity",
* normalizationContext={"groups"={"country:read"}},
* denormalizationContext={"groups"={"country:write"}},
* collectionOperations={"GET","POST",
* "GETLIST"={
* "method"="GET",
* "description"="Retrieves a limited set of properties of Country resources",
* "path"="country_list_short",
* "normalization_context"={"groups":"country:list:short:read"}
* }
* }
* )
*
* @ORM\Entity(repositoryClass=CountryRepository::class)
*
*/
class Country extends AbstractEntity
/**
*
* @ORM\Column(
* name="name",
* type="string",
* length=128,
* nullable=false,
* options={"comment":"Country name (English)"}
* )
*
* @Assert\Unique(message="The name {{ value }} already in the table")
* @Assert\NotBlank(message="The country name should not be blank")
* @Assert\NotNull(message="The country name should not be null")
* @Assert\Length(
* min="2",
* minMessage="The Country name expected minimum length is {{ limit }}",
* max="128",
* maxMessage="The Country name expected maximum length is {{ limit }}"
* )
*
* @Groups({"country:read","country:write","country:list:short:read"})
*/
private $name;
您发送的是 JSON 数组而不是 JSON 对象。
只需删除前置和附加的方括号:
{\"name\":\"Aruba2\",\"iso2\":\"ZZ\",\"iso3\":\"ZZZ\",\"region\":\"Americas\",\"subregion\":\"Caribbean\",\"capital\":\"Oranjestad\",\"latitude\":12.5,\"longitude\":-69.96666666,\"isEc\":false,\"isEfta\":false}
编辑:
您还使用了 @Assert\Unique
注释。这是个错误。此注释必须是 used with collections. Given the error message and your property type, you want to use @Assert\UniqueEntity
`,这是一个 class 约束检查 属性 在 table.