问题
I am using FOSRestBundle to manage the API in my app. So I got almost everything working, except PUT method, where some of the properties are NULL but they shouldn't since the entity (id 105) has them filled in.
Take a look at the putAction:
/**
* @ParamConverter("user", class="SoftwareBundle:User", options={"mapping": {"user": "guid"}})
*/
public function putAction(Request $request, $user)
{
$form = $this->createForm(new UserType(), $user, ['method' => 'PUT']);
$form->handleRequest($request);
var_dump($user->getId()); // 105
var_dump($user->getGuid()); // 12345
var_dump($user->getEmailAddress()); // NULL (should be johndoe@example.com
var_dump($user->getFirstName()); // NULL (should be John)
var_dump($user->getLastName()); // NULL (should be Doe)
var_dump($user->getSex()); // NULL (should be 0)
var_dump($user->getPassword()); // pass123
var_dump($user->getUserAgent()); // curl/7.30.0
var_dump($user->getIpAddress()); // 127.0.0.1
var_dump($user->getMember()->getPrivateNotes()); // example note
if($form->isValid())
{
$manager = $this->getDoctrine()->getManager();
$manager->flush();
return $this->view([
'user' => $user
]);
}
return $this->view($form, Codes::HTTP_BAD_REQUEST);
}
Question is, why FirstName, LastName, EmailAddress and Sex are NULL? They really shouldn't.
Here is how the form looks like:
class UserType extends AbstractFormType
{
public function buildForm(FormBuilderInterface $builder, array $option)
{
$builder
->add('firstName', 'text', [
'label' => 'word.first_name',
'required' => true
])
->add('lastName', 'text', [
'label' => 'word.last_name',
'required' => true
])
->add('emailAddress', 'email', [
'label' => 'word.email_address',
'required' => true,
'constraints' => [
new HasUsername($this->getOption('uuid'))
]
])
->add('sex', 'choice', [
'label' => 'word.sex',
'choices' => [
'0' => 'word.male',
'1' => 'word.female'
],
'required' => true,
'empty_value' => 'word.select',
'empty_data' => null
])
->add('locale', 'entity', [
'label' => 'word.locale',
'required' => false,
'property' => 'code',
'class' => 'SoftwareBundle:Locale',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('l')
->orderBy('l.code', 'ASC');
},
'placeholder' => 'word.select',
'empty_data' => null
])
->add('member', new MemberType(), [])
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'translation_domain' => 'general',
'data_class' => 'Software\Bundle\Entity\User',
'attr' => ['novalidate' => 'novalidate'],
'csrf_protection' => false
]);
}
public function getName()
{
return 'user';
}
Validation is done in a separate .yml file.
This is the JSON I am sending to put action:
{"user":{"member": {"privateNotes":"example note"}}}"
Any ideas?
回答1:
I've solved it in past with event listeners. The trick is to rewrite the value for defaults if submitted value is null
:
namespace Acme\Bundle\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
class UserType extends AbstractType
{
/**
* @param FormBuilder $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
->add('name');
$callback = function(FormEvent $event) {
if (null === $event->getData()) {
$event->setData($event->getForm()->getData());
}
};
$builder->get('email')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
$builder->get('name')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
}
// [ ... ]
}
来源:https://stackoverflow.com/questions/29190116/symfony-put-does-not-contain-all-entity-properties-mapped