我目前陷入困境,似乎无法在隧道尽头找到光明。
我有一种简单的控制器功能:
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 ,其中约束以注释的形式声明。
问题在于,当我提交表单时,即使数据不正确且表单如此,它仍然会在数据库中持续修改。我对这个问题的理解是,表单本身不会保留任何数据,只会将数据与我设置的约束注释进行比较。我不想淹没这个问题,所以我没有显示我的所有代码,但如果您需要更多信息,请询问,我会编辑!
非常感谢。
编辑:有人问我的实体代码:我在这里发布,没有不相关的获取者和制定者。/**
* @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;
}
答案 0 :(得分:0)
我想我发现了这个问题。你这样做:
$dm->persist($this->getUser()->getOrganisation());
你应该这样做:
$dm->persist($org);
实际上,我认为from验证与变量$ org相关联,而不是对象的引用。试着告诉我们:)
答案 1 :(得分:0)
也许控制器中的持久操作与表单本身无关。您的表单基础data_class是' MetaCloud \ DataBundle \ Document \ Organization',您确定,
$org = $this->getUser()->getOrganisation();
返回类型为' MetaCloud \ DataBundle \ Document \ Organization'的对象为了澄清事情,请发布您的实体代码。
编辑:也尝试添加
$org = $form->getData();
在你持久保存数据之前。
答案 2 :(得分:0)
我发现了这个问题。事实上,Symfony验证器服务仅表示数据无效但不刷新数据库中的修改实体。我所要做的就是刷新与表单相关的对象,以使工作正常:
if ($form->isSubmitted())
{
$dm->refresh($org);
$this->get('session')->getFlashBag()->add('danger', 'msg.update_failed_see_errors');
}
如果有人遇到同样的问题,可以在这里找到有趣的消息来源:
Google groups discussion about the problem Ticket on the subject for symfony2