Symfony不验证表格

时间:2018-09-21 14:16:33

标签: php forms api symfony validation

我正在编写一个API。我需要验证表格。其他形式可以正确验证,但是此形式完全不能验证。

这是我的表单类型类:

public function buildForm(FormBUilderInterface $builder, array $options){
    $builder
        ->add('passwordConfirmation', RepeatedType::class, [
            'required' => true,
            'invalid_message' => 'Passwords must match!',
            'type' => PasswordType::class
        ]);
}

/**
 * @param OptionsResolver $resolver
 */
 public function configureOption(OptionsResolver $resolver){
    $resolver->setDefaults(array(
        'data_class' => RepeatPassword::class,
        'csrf_protection' => false
    ))
 }

public function getName() {
    return 'repeatPassword';
}

我的实体:

class RepeatedPassword{
/**
 * @var string
 * @Assert\Length(
 *    min = 8,
 *    minMessage = "Your password must be at least {{ limit }} characters long")
 * )
 * @Assert\NotBlank()
 */
private $passwordConfirmation;

/**
 * @return mixed
 */
public function getPasswordConfirmation() {
    return $this->passwordConfirmation;
}

/**
 * @param mixed $passwordConfirmation
 */
public function setPasswordConfirmation($passwordConfirmation): void{
    $this->passwordConfirmation = $passwordConfirmation;
}
}

我尝试验证的方法:

public function resetPassword(Request $request): View{
    $form = $this->createForm(RepeatPasswordType::class);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $this->userService->setPassword($this->getUser(), $form->getData()->getPasswordConfirmation());
        return View::create([], Response::HTTP_OK);
    }

    return View::create($form->getErrors(), Response::HTTP_BAD_REQUEST);
 }

我的config.yml文件:

 validation: { enabled: true, enable_annotations: true }
 serializer: { enable_annotations: true }

我发送的数据和状态为400的服务器响应:

3 个答案:

答案 0 :(得分:0)

也许有帮助: 在表单中添加以下内容

import numpy as np
import tensorflow as tf

current_sum = tf.Variable(0, name='current_sum')
current_div = tf.Variable(1, name='div')

current_random = tf.placeholder(dtype=tf.int32, shape=[], name='current_random')

mean = tf.div(current_sum, current_div)

model = tf.global_variables_initializer()

with tf.Session() as session:
    for i in range(100):
        num = np.random.random() * 100

        current_sum_assign_op = tf.assign(current_sum, value=current_sum + current_random)
        current_div_assign_op = tf.assign(current_div, value=current_div + 1)

        session.run(model)
        session.run(current_random, feed_dict={current_random: num})
        session.run(current_sum_assign_op, feed_dict={current_random: num})
        session.run(current_div_assign_op)
        session.run(mean)

        print('num =', num)
        print('mean = ')

        session.run(tf.Print(mean, [mean], 'Mean is'))
        session.run(tf.Print(current_div, [current_div], 'Div is'))
    session.run(model)

答案 1 :(得分:0)

我现在看到的是在minMessage声明的末尾还有一个额外的“,”。您是否尝试过删除它?

答案 2 :(得分:0)

$form->handleRequest的行为不同于$form->submit之前,我已经看到一个问题。我可以通过手动调用Submit来更有效地调试。

$form = $this->createForm(RepeatPasswordType::class);
if ($request->getMethod() === 'post') {
    $form->submit($request->request->get($form->getName()));
    if (!$form->isValid()) {
        // etc
    }
}