如何使用vue axios将表单发送到Symfony 4?
如何通过axios将数据从vue应用发送到Symfony?当我尝试发送数据Symfony请求时,没有看到它们 控制器代码:如何使用vue axios将表单发送到Symfony 4?,symfony,vue.js,vuejs2,axios,Symfony,Vue.js,Vuejs2,Axios,如何通过axios将数据从vue应用发送到Symfony?当我尝试发送数据Symfony请求时,没有看到它们 控制器代码: $form = $this->createForm(TeacherType::class); $form->submit($request->request->all()); $form->handleRequest($request); if($form->isSubmitted() &&am
$form = $this->createForm(TeacherType::class);
$form->submit($request->request->all());
$form->handleRequest($request);
if($form->isSubmitted() && !$form->isValid()) {
return new JsonResponse([
'code' => "400",
'data' => $this->getErrorMessages($form),
]);
}
return new JsonResponse(array(
'code' => 200,
));
axios:
sent() {
axios.post('http://localhost:8000/valid', {
firstname: this.firstname
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
表格:
好啊
我的回答总是:{'code':400},并提醒firstname不应为空(表单中为firstname设置了assert NotBlank)
我能做什么?尝试删除
$form->handleRequest($request)代码>,handleRequest
不使用api调用
更新
下面是验证实体的简单示例
实体:
use Symfony\Component\Validator\Constraints as Assert;
class SomeClass
{
/**
* @Assert\NotBlank
*/
private $content;
}
表单类型:
class SomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SomeClass::class,
]);
}
}
FormHandler:
class FormHandler
{
private $formFactory;
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
public function handleWithSubmit(
array $data,
string $type,
$entity,
array $options = []
) {
$form = $this->formFactory->create($type, $entity, $options);
$form->submit($data);
return $this->processSubmitted($form, $entity);
}
private function processSubmitted(FormInterface $form, $entity)
{
if (!$form->isValid()) {
return $form->getErrors();
}
if (!is_object($entity)) {
$entity = $form->getData();
}
return $entity;
}
}
在控制器中注入FormHandler
,并调用
$handled = $this->formHandler->handleWithSubmit($request->request->all(), SomeType::class, new SomeClass());
if (!$handled instanceof SomeClass) {
// return errors
}
这是因为HTML文档中的字段命名不正确
目前:
teacher[名字]
而是应该(或多或少):
teacher\u teacher[名字]
使用FormFactoryInterface
创建命名表单:
public function yourAction(Request $request, FormFactoryInterface $forms)
{
$form = $this->forms->createNamed('', TeacherType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) {
return new JsonResponse([
'code' => '400',
'data' => $this->getErrorMessages($form),
]);
}
return new JsonResponse(['code' => 200]);
}
当您创建一个没有名称的表单(例如$this->createForm(TeacherType::class)
)时,会自动为您生成一个名称:表单的名称,但不包含表单
,最后是类型
单词,因此,需要将教师部分置于字段名称之前。安装FOS Rest捆绑包,并确保已安装序列化程序。我会使用Symfony序列化程序
基本上,它将对HTTP请求正文进行解码,并接受$form->handleRequest($request)
所需的标题,然后将表单显示为已提交。它不起作用。所以我应该如何使用Symfony作为RESTOk生成验证表单我使用了$validator validate,但现在我不知道如何将$errors中的字符串解析为js对象(App\Entity\Teacher)。firstname:这个值不应该为空。(代码c1051bb4-d103-4f74-8988-acbcafc7fdc3)用验证实体的简单示例更新了答案。handleRequest不使用api调用
public function yourAction(Request $request, FormFactoryInterface $forms)
{
$form = $this->forms->createNamed('', TeacherType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) {
return new JsonResponse([
'code' => '400',
'data' => $this->getErrorMessages($form),
]);
}
return new JsonResponse(['code' => 200]);
}