简体   繁体   中英

Symfony 3 Form using RepeatedType with FOSRest

How can you submit a RepeatedType using FOSRest?

I have added the following formtype:

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('email', TextType::class);
    $builder->add('firstName', TextType::class);
    $builder->add('lastName', TextType::class);
    $builder->add('password', RepeatedType::class, [
        'type' => PasswordType::class,
        'invalid_message' => 'The password fields must match.',
        'required' => true
    ]);
}

And this is the data I submit:

array(1) {
["user"]=>
array(4) {
    ["email"]=>
    string(21) "Testuser@test.be"
    ["firstName"]=>
    string(4) "Test"
    ["lastName"]=>
    string(9) "User"
    ["password"]=>
    array(2) {
          ["first"]=>
          string(8) "password"
          ["second"]=>
          string(8) "password"
    }
  }
}

Everything works, except the repeatedType throws: This form should not contain extra fields.

What's the correct format to submit to a repeatedType? The docs aren't really clear on this ...

Thank you

try to change this:

$builder->add('password', RepeatedType::class, [
    'type' => PasswordType::class,
    'invalid_message' => 'The password fields must match.',
    'required' => true
]);

to this:

$builder->add('password', RepeatedType::class, [
    'type' => PasswordType::class,
    'first_options'  => array('label' => 'Password'),
    'second_options' => array('label' => 'Repeat Password')
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM