保存数据的表单无效(Invalid form saving its data)

编程入门 行业动态 更新时间:2024-10-14 22:22:26
保存数据的表单无效(Invalid form saving its data)

我目前陷入了一个问题,我似乎无法找到隧道尽头的灯光。

我有一种简单的控制器功能:

public function settingsAction(Request $request) { $org = $this->getUser()->getOrganisation(); $form = $this->createForm(new OrgSettingsType(), $org); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $dm = $this->get('doctrine_mongodb')->getManager(); $dm->persist($this->getUser()->getOrganisation()); $dm->flush(); $this->get('session')->getFlashBag()->add('success', 'msg.changes_saved'); return $this->redirect($this->generateUrl('metacloud_account_organisation_settings')); } if ($form->isSubmitted()) { $this->get('session')->getFlashBag()->add('danger', 'msg.update_failed_see_errors'); } return $this->render('MetaCloudAccountBundle:Organisation:settings.html.twig', array('form' => $form->createView() )); }

表单并不是真的更复杂,但它是一个两级形式:

class OrgSettingsType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('groups', 'collection', array( 'type' => 'text', 'allow_add' => true, 'options' => array( 'label' => false ) )); $builder->add('cloudAccounts', 'cloud_account', array( )); $builder->add('settings', new OrganisationConfigType()); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'MetaCloud\DataBundle\Document\Organisation', 'cascade_validation' => true, 'validation_groups' => array('account_edit', 'Default') )); } public function getName() { return 'org_config'; } }

OrganisationConfigType表单非常简单,并且链接到文档OrganisationConfig ,其中约束以注释的形式声明。

问题是,当我提交表单时,即使数据不正确且表单如此,它仍然会在数据库中保留修改。 我对这个问题的理解是,表单本身不会保留任何数据,只会将数据与我设置的约束注释进行比较。 我不想淹没这个问题,所以我没有显示我的所有代码,但如果您需要更多信息,请询问,我会编辑!

非常感谢。

编辑:有人要求我的实体代码:我在这里发布,没有无关的getter和setter。

/** * @MongoDB\Document(collection="societe", repositoryClass="MetaCloud\DataBundle\Repository\OrganisationRepository") * @MongoDB\HasLifecycleCallbacks * @Unique("email") */ class Organisation { /** * @MongoDB\Id(strategy="AUTO", name="_id") */ protected $id; /** * @MongoDB\String(name="nom") * * @Assert\NotNull(message="error name") */ protected $name; /** * @MongoDB\String(name="siret", nullable=true) * * */ protected $number; /** * @MongoDB\String(name="nomRep") * * @Assert\NotNull( * message = "contact last name cant be null" * ) */ protected $contactLastName; /** * @MongoDB\String(name="prenomRep") * * @Assert\NotNull(message="error contact first name") */ protected $contactFirstName; /** * @MongoDB\String(name="titreRep", nullable=true) */ protected $contactTitle; /** * @MongoDB\String(name="civilite") * * @Assert\NotNull(message="civilite is null") */ protected $contactHonorific; /** * @MongoDB\String(name="email") * * @Assert\NotNull() * @Assert\Email() */ protected $email; /** * @MongoDB\String(name="emailadmin", nullable = true) * * @Assert\Email() */ protected $emailAdmin = ""; /** * @MongoDB\String(name="telephone") * * @Assert\NotNull */ protected $phone; /** * @MongoDB\Int(name="maxutilisateurs") * * @Assert\NotNull(groups={"account_edit"}) * @Assert\Range(min=1, groups={"account_edit"}) * @Assert\NotNull() */ protected $maxUsers; /** * @MongoDB\Int(name="restutilisateurs", nullable=true) */ protected $remainingUsers; /** * @MongoDB\EmbedOne(targetDocument="Contract", name="contract", nullable=true) */ protected $contract; /** * @MongoDB\String(name="datecreated") */ protected $createdAt; /** * @MongoDB\Date(name="datedeleted", nullable=true) */ protected $deletedAt; /** * @MongoDB\EmbedOne(targetDocument="Address", name="adresse") * * @Assert\NotNull() * @Assert\Valid() */ protected $address; /** * @MongoDB\Collection(name="groupes", nullable=true) * */ protected $groups = array(); /** * @MongoDB\EmbedOne(targetDocument="Administrator", name="administrateur") */ protected $administrator; /** * @MongoDB\ReferenceOne(targetDocument="User", name="mappedadmin") * * @Gedmo\ReferenceIntegrity("nullify") */ protected $mappedAdmin; /** * @MongoDB\EmbedOne(targetDocument="OrganisationConfig", name="configpublique", nullable=true) */ protected $settings; /** * @MongoDB\Collection(name="cloudstockages", nullable=true) * * @Assert\Count(min=1, groups={"account_edit"}) */ protected $cloudAccounts = array(); /** * @MongoDB\ReferenceMany(targetDocument="User", mappedBy="organisation", nullable=true) * * @Gedmo\ReferenceIntegrity("nullify") */ protected $users; /** * @MongoDB\ReferenceMany(targetDocument="SecurityCode", mappedBy="organisation", nullable=true) * * @Gedmo\ReferenceIntegrity("nullify") */ protected $invites; /** * @MongoDB\ReferenceMany(targetDocument="Organisation", mappedBy="integrator", name="organisations", nullable=true) */ protected $organisations; /** * @MongoDB\ReferenceOne(targetDocument="Organisation", inversedBy="organisations", name="integrator", simple="true") */ protected $integrator; /** * @MongoDB\Field(type="boolean", name="anintegrator") * */ protected $anIntegrator; /** * Determine if there are enough accounts to provide Confidentiality * * @Assert\True( * message = "org_not_enough_accounts_for_confidentiality", * groups={"account_edit"} * ) * * @return bool */ public function isEnoughCloudAccountsForConfidentiality() { if(null === $this->getSettings()) { return true; } if('NO' != $this->getSettings()->getIntegrity() && null !== $this->getSettings()->getIntegrity()) { // validate if getIntegrity is selected as it has a higher level validation rule return true; } if('NO' != $this->getSettings()->getConfidentiality() && null !== $this->getSettings()->getConfidentiality()) { return count($this->cloudAccounts) >= 1; } return true; } /** * Determine if there are enough accounts to provide Integrity * * @Assert\True( * message = "org_not_enough_accounts_for_integrity", * groups={"account_edit"} * ) * * @return bool */ public function isEnoughCloudAccountsForIntegrity() { if(null === $this->getSettings()) { return true; } if('NO' != $this->getSettings()->getIntegrity() && null !== $this->getSettings()->getIntegrity()) { return count($this->cloudAccounts) >= 2; } return true; }

I'm currently stuck on a problem and I can't seem to find the light at the end of the tunnel.

I have a kind of simple controller function:

public function settingsAction(Request $request) { $org = $this->getUser()->getOrganisation(); $form = $this->createForm(new OrgSettingsType(), $org); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $dm = $this->get('doctrine_mongodb')->getManager(); $dm->persist($this->getUser()->getOrganisation()); $dm->flush(); $this->get('session')->getFlashBag()->add('success', 'msg.changes_saved'); return $this->redirect($this->generateUrl('metacloud_account_organisation_settings')); } if ($form->isSubmitted()) { $this->get('session')->getFlashBag()->add('danger', 'msg.update_failed_see_errors'); } return $this->render('MetaCloudAccountBundle:Organisation:settings.html.twig', array('form' => $form->createView() )); }

The form isn't really more complex, but it's a two level form :

class OrgSettingsType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('groups', 'collection', array( 'type' => 'text', 'allow_add' => true, 'options' => array( 'label' => false ) )); $builder->add('cloudAccounts', 'cloud_account', array( )); $builder->add('settings', new OrganisationConfigType()); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'MetaCloud\DataBundle\Document\Organisation', 'cascade_validation' => true, 'validation_groups' => array('account_edit', 'Default') )); } public function getName() { return 'org_config'; } }

The OrganisationConfigType form is simple enough and is linked to a document OrganisationConfig where the constraints are declared in the form of annotations.

The problem is that when I submit the form, even though the data is incorrect and the form says so, it still persists the modification in the database. My understanding of the thing was that a form wouldn't persist any data by itself and would just compare data with the constraint annotations I have set up. I don't want to flood the question so I'm not showing all my code, but if you need any more information please ask and I will edit !

Thanks a lot.

EDIT: someone asked for my entity code: I post it here, without the irrelevant getters and setters.

/** * @MongoDB\Document(collection="societe", repositoryClass="MetaCloud\DataBundle\Repository\OrganisationRepository") * @MongoDB\HasLifecycleCallbacks * @Unique("email") */ class Organisation { /** * @MongoDB\Id(strategy="AUTO", name="_id") */ protected $id; /** * @MongoDB\String(name="nom") * * @Assert\NotNull(message="error name") */ protected $name; /** * @MongoDB\String(name="siret", nullable=true) * * */ protected $number; /** * @MongoDB\String(name="nomRep") * * @Assert\NotNull( * message = "contact last name cant be null" * ) */ protected $contactLastName; /** * @MongoDB\String(name="prenomRep") * * @Assert\NotNull(message="error contact first name") */ protected $contactFirstName; /** * @MongoDB\String(name="titreRep", nullable=true) */ protected $contactTitle; /** * @MongoDB\String(name="civilite") * * @Assert\NotNull(message="civilite is null") */ protected $contactHonorific; /** * @MongoDB\String(name="email") * * @Assert\NotNull() * @Assert\Email() */ protected $email; /** * @MongoDB\String(name="emailadmin", nullable = true) * * @Assert\Email() */ protected $emailAdmin = ""; /** * @MongoDB\String(name="telephone") * * @Assert\NotNull */ protected $phone; /** * @MongoDB\Int(name="maxutilisateurs") * * @Assert\NotNull(groups={"account_edit"}) * @Assert\Range(min=1, groups={"account_edit"}) * @Assert\NotNull() */ protected $maxUsers; /** * @MongoDB\Int(name="restutilisateurs", nullable=true) */ protected $remainingUsers; /** * @MongoDB\EmbedOne(targetDocument="Contract", name="contract", nullable=true) */ protected $contract; /** * @MongoDB\String(name="datecreated") */ protected $createdAt; /** * @MongoDB\Date(name="datedeleted", nullable=true) */ protected $deletedAt; /** * @MongoDB\EmbedOne(targetDocument="Address", name="adresse") * * @Assert\NotNull() * @Assert\Valid() */ protected $address; /** * @MongoDB\Collection(name="groupes", nullable=true) * */ protected $groups = array(); /** * @MongoDB\EmbedOne(targetDocument="Administrator", name="administrateur") */ protected $administrator; /** * @MongoDB\ReferenceOne(targetDocument="User", name="mappedadmin") * * @Gedmo\ReferenceIntegrity("nullify") */ protected $mappedAdmin; /** * @MongoDB\EmbedOne(targetDocument="OrganisationConfig", name="configpublique", nullable=true) */ protected $settings; /** * @MongoDB\Collection(name="cloudstockages", nullable=true) * * @Assert\Count(min=1, groups={"account_edit"}) */ protected $cloudAccounts = array(); /** * @MongoDB\ReferenceMany(targetDocument="User", mappedBy="organisation", nullable=true) * * @Gedmo\ReferenceIntegrity("nullify") */ protected $users; /** * @MongoDB\ReferenceMany(targetDocument="SecurityCode", mappedBy="organisation", nullable=true) * * @Gedmo\ReferenceIntegrity("nullify") */ protected $invites; /** * @MongoDB\ReferenceMany(targetDocument="Organisation", mappedBy="integrator", name="organisations", nullable=true) */ protected $organisations; /** * @MongoDB\ReferenceOne(targetDocument="Organisation", inversedBy="organisations", name="integrator", simple="true") */ protected $integrator; /** * @MongoDB\Field(type="boolean", name="anintegrator") * */ protected $anIntegrator; /** * Determine if there are enough accounts to provide Confidentiality * * @Assert\True( * message = "org_not_enough_accounts_for_confidentiality", * groups={"account_edit"} * ) * * @return bool */ public function isEnoughCloudAccountsForConfidentiality() { if(null === $this->getSettings()) { return true; } if('NO' != $this->getSettings()->getIntegrity() && null !== $this->getSettings()->getIntegrity()) { // validate if getIntegrity is selected as it has a higher level validation rule return true; } if('NO' != $this->getSettings()->getConfidentiality() && null !== $this->getSettings()->getConfidentiality()) { return count($this->cloudAccounts) >= 1; } return true; } /** * Determine if there are enough accounts to provide Integrity * * @Assert\True( * message = "org_not_enough_accounts_for_integrity", * groups={"account_edit"} * ) * * @return bool */ public function isEnoughCloudAccountsForIntegrity() { if(null === $this->getSettings()) { return true; } if('NO' != $this->getSettings()->getIntegrity() && null !== $this->getSettings()->getIntegrity()) { return count($this->cloudAccounts) >= 2; } return true; }

最满意答案

我想我发现了这个问题。 你做这个 :

$dm->persist($this->getUser()->getOrganisation());

你应该这样做:

$dm->persist($org);

实际上,我认为from验证与变量$ org相关联,而不是对象的引用。 试着告诉我们:)

I have found the problem. As a matter of fact, Symfony validator service only says the data is invalid but doesn't refresh the modified entity in database. All I had to do was to refresh the object associated to the form in order to make things work :

if ($form->isSubmitted()) { $dm->refresh($org); $this->get('session')->getFlashBag()->add('danger', 'msg.update_failed_see_errors'); }

If anyone has the same problem, interesting sources can be found here :

Google groups discussion about the problem Ticket on the subject for symfony2

更多推荐

本文发布于:2023-08-01 23:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   数据   Invalid   data   saving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!