Php 保存其数据的表单无效

Php 保存其数据的表单无效,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我现在遇到了一个问题,我似乎无法在隧道的尽头找到光明 我有一种简单的控制器功能: public function settingsAction(Request $request) { $org = $this->getUser()->getOrganisation(); $form = $this->createForm(new OrgSettingsType(), $org); $form->handleRequest($request);

我现在遇到了一个问题,我似乎无法在隧道的尽头找到光明

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

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';
}
}
OrganizationConfigType表单非常简单,并链接到文档OrganizationConfig,其中约束以注释的形式声明

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

非常感谢

编辑:有人问我的实体代码:我把它发布在这里,没有无关的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;
}

我想我发现了问题所在。您可以这样做:

$dm->persist($this->getUser()->getOrganisation());
你应该这样做:

$dm->persist($org);

事实上,from验证链接到变量$org,而不是对象的引用,我认为。请尝试告诉我们:)

也许控制器中的持久化操作与表单本身无关。您的表单基础数据类是“元云\DataBundle\Document\organization”,您确定

$org=$this->getUser()->getorganization()

返回“MetaCloud\DataBundle\Document\Organization”类型的对象?为了澄清问题,请发布您的实体代码

编辑:同时尝试添加
$org=$form->getData()

在您保留数据之前。

我发现了问题。事实上,Symfony validator service only表示数据无效,但不会刷新数据库中修改的实体。我所要做的就是刷新与表单关联的对象,以使其正常工作:

if ($form->isSubmitted())
{
    $dm->refresh($org);
    $this->get('session')->getFlashBag()->add('danger', 'msg.update_failed_see_errors');
}
如果有人有同样的问题,可以在这里找到有趣的来源:


无效数据是什么意思?我看不到任何“特殊”验证。嵌入表单有约束。例如,如果其他字段不优于三个字段,则某些字段需要设置为false。出现错误时,错误会正确显示(表示我收到消息“msg.update\u failed\u see\u errors”,表示表单无效,但数据库中的数据仍在更改,因此您收到了一条闪光消息“msg.changes\u saved”,也是吗?不。但数据仍然保存在数据库中。这让我非常困惑。尝试过了,结果不是。正如我看到的问题,因为我甚至没有输入此条件代码(我的表单无效),我认为问题不在这个级别。不过感谢您的时间:)是的,确实,这很愚蠢,对不起!为了确保我们了解发生了什么:您得到了您的danger flashbag,整个对象被持久化到数据库中(不仅仅是子对象OrganizationConfig)?整个对象被保存,我只是检查了它。我确信GetOrganization()返回了正确类型的对象,表单将无法以其他方式构建,因为我没有此结构的其他文档(getter将返回错误)。我的实体代码很长,我会看看我能做些什么。