问题描述
我正在使用FOSUserBundle和FOSRestBundle在Symfony 2.3.*中开发RESTFul API,但在理解如何创建注册方法时遇到了麻烦.
I'm developing a RESTFul API in Symfony 2.3.* with FOSUserBundle and FOSRestBundle, and I'm having trouble understanding how to create a registration method.
我的控制器如下所示:
class UserRestController extends FOSRestController
{
//Other Methods ...
public function postUserAction()
{
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
$param = $paramFetcher->all();
$form = $this->createForm(new UserType(), $user);
$form->bind($param);
if ($form->isValid() == false)
return $this->view($form, 400);
$userManager->updateUser($user);
return $this->view('User Created', 201);
}
//...
}
还有我的UserType类:
And my UserType class :
class UserType extends BaseType
{
public function __construct($class = "User")
{
parent::__construct($class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'username')
->add('email', 'email')
->add('plainPassword', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password'
))
->add('lastname')
->add('firstname')
->add('job_position')
->add('phone')
->add('company_name')
->add('website')
->add('sector')
->add('address')
->add('city')
->add('zip_code')
->add('country')
->add('billing_infos_same_as_company')
->add('billing_address')
->add('billing_city')
->add('billing_zip')
->add('billing_country')
->add('putf')
->add('das');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Wipsea\UserBundle\Entity\User',
'csrf_protection' => false
));
}
public function getName()
{
return 'wipsea_user_registration';
}
}
当我测试它时,无论哪种形式都是无效的,也不会显示任何错误.当我尝试获得请求时:
When I test it, no matter what the form isn't valid and shows no error.And when I try to get the request :
验证失败"此表单不应包含其他字段."
"Validation Failed" "This form should not contain extra fields."
有没有一种方法可以正确验证表单?
Is there a way to properly validate the form ?
更新我的问题.
推荐答案
我将向您推荐本教程,分为3部分-您需要的所有内容:
I would recommend you this tutorial in 3 parts - there is everything you need:
- http://welcometothebundle.com/symfony2-rest-api -the-best-2013-way/
- http://welcometothebundle .com/web-api-rest-with-symfony2-最佳方法发布方法/
- http://welcometothebundle.com/symfony2-rest -api-the-best-way-part-3/
- http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/
- http://welcometothebundle.com/web-api-rest-with-symfony2-the-best-way-the-post-method/
- http://welcometothebundle.com/symfony2-rest-api-the-best-way-part-3/
如果要提供复杂的用户验证,则应创建UserType表单并将数据传递给该表单,而不是直接设置所有属性:
If you want to provide complex user validation you should create UserType form and pass data to this form instead of directly setting all properties:
public function postAction()
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
// propel version
$user->save();
$response = new Response();
$response->setStatusCode(201);
// set the `Location` header only when creating new resources
$response->headers->set('Location',
$this->generateUrl(
'acme_demo_user_get', array('id' => $user->getId()),
true // absolute
)
);
return $response;
}
// return form validation errors
return View::create($form, 400);
}
在本教程的第2部分中,您将获得有关创建表单,传递数据以及使用RestBundle进行验证的所有信息.
In part 2 of this tutorial you have all information about creating form, passing data and validating it with RestBundle.
关于将REST与Symfony2结合使用的最佳实践的信息也很多.
There is also a lot information about best practices using REST with Symfony2.
这篇关于FOSRestBundle如何验证注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!