FOSrestBundle을 기반으로 일부 REST API를 만드는 방법에 대한 자습서를 진행하고 있지만 양식 유효성 검사와 관련하여 일부 문제가 있습니다. http://127.0.0.1:8000/usersSymfony 3 및 FOSrestBundle이 포함 된 유효성 확인 양식
을하지만 그것은 단지 빈 안 값에 대한 오류를 반환 :
는 그 엔드 포인트에서 POST 요청을 사용하여 내 데이터베이스에 새 사용자를 추가하려고합니다.
내 클래스 UserController이 방법이 있습니다
/**
* @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"user"})
* @Rest\Post("/users")
*/
public function postUsersAction(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user, ['validation_groups'=>['Default', 'New']]);
$form->submit($request->request->all());
if ($form->isValid()) {
$encoder = $this->get('security.password_encoder');
$encoded = $encoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($encoded);
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($user);
$em->flush();
return $user;
} else {
return $form;
}
}
내 validation.yml :
/**
* @ORM\Entity()
* @ORM\Table(name="users",
* uniqueConstraints={@ORM\UniqueConstraint(name="users_email_unique",columns={"email"})}
*)
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $firstname;
/**
* @ORM\Column(type="string")
*/
protected $lastname;
/**
* @ORM\Column(type="string")
*/
protected $email;
/**
* @ORM\Column(type="string")
*/
protected $password;
protected $plainPassword;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getFirstname()
{
return $this->firstname;
}
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
public function getLastname()
{
return $this->lastname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getPlainpassword()
{
return $this->plainPassword;
}
public function getRoles()
{
return [];
}
public function getSalt()
{
return null;
}
public function getUsername()
{
return $this->email;
}
public function eraseCredentials()
{
// Suppression des données sensibles
$this->plainPassword = null;
}
}
그리고 양식을 만들 부분 : 여기
AppBundle\Entity\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
properties:
firstname:
- NotBlank: ~
- Type: string
lastname:
- NotBlank: ~
- Type: string
email:
- NotBlank: ~
- Email: ~
plainPassword:
- NotBlank: { groups: [New, FullUpdate] }
- Type: string
- Length:
min: 4
max: 50
내 기업 사용자입니다 in UserType.php :
,
는 그 페이로드, 내 POST 요청을 보낼 우체부를 사용
{
"firstname": "foo",
"lastname": "bar",
"email": "[email protected]",
"password": "qwerty"
}
그러나 나는 400 오류가 나타납니다 내가 plainPassword를 교체 할 경우
{
"code": 400,
"message": "Validation Failed",
"errors": {
"children": {
"firstname": [],
"lastname": [],
"plainPassword": {
"errors": [
"This value should not be blank."
]
},
"email": []
}
}
}
EDIT 1
을 내 validation.yml에서 비밀번호로 오류가 반환됩니다.
{
"code": 400,
"message": "Validation Failed",
"errors": {
"errors": [
"This value should not be blank."
],
"children": {
"firstname": [],
"lastname": [],
"plainPassword": [],
"email": []
}
}
}
다른 파일을 보려면 필자에게 알려주십시오.
어쨌든 도와 주셔서 감사합니다.
해결 @ kunicmarko20가 고맙게도 저를 도와 그래서, 내가 여기에 올바른 하나, 내가 POST 요청을 보낼 페이로드의 실수를
: 무엇보다
{
"firstname": "foo",
"lastname": "bar",
"email": "[email protected]",
"plainPassword": "qwerty"
}
그리고 더 I 완전히 내 사용자 개체에서 세터를 넣어하는 것을 잊지, 여기에 우리가 간다 :
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($plainpassword)
{
$this->plainPassword = $plainpassword;
}
감사합니다,하지만 '$ 요청 -> 요청 -> 모든()' 'json_decode는 ($ 요청 ->의 getContent(), TRUE)' 그래서 난 여전히 내 POST 후 동일한 오류가 발생했습니다 것을 반환 동일 의뢰. – wilfryed
@wilfryed 컨트롤러 나 여러 장소에서 비밀번호 인코딩을하지 않으려면이를 수행 할 리스너를 추가 할 수 있습니다. [here] (https://github.com/kunicmarko20/guard-talk-example/blob/)를 확인하십시오. – kunicmarko20
글쎄, 그 오류 메시지는 400,하지만 500이라는 메시지는 반환하지 않습니다 :'\ "plainPassword \"속성에 대한 액세스 유형을 결정할 수 없습니다. " – wilfryed