如何为FOSUserbundle注册添加确认复选框

时间:2014-08-21 09:10:17

标签: symfony fosuserbundle

我正在使用FOSUserBundle,如何添加必须为用户创建帐户的复选框。

FOS是否已经满足了这一点,也许我只需要在配置文件中添加一行,或者我可能需要更改控制器。

我添加了一张图片链接,以便更好地解释我需要做什么

http://lescouverts.com/Images/errer-fos.png

这非常重要,因为用户必须在注册前接受条款和条件。

4 个答案:

答案 0 :(得分:2)

您可以添加简单的à字段,其中«mapped»选项为false,但不会将其添加到您的实体中。

$builder
    ->add('cgu', 'checkbox', array(
        'label'     => 'Je reconnais…',
        'required'  => true,
        'mapped' => false
    ))
;

答案 1 :(得分:1)

在您的表单中,您必须使用class:

         Symfony\Component\Validator\Constraints\True;
像这样:

        <?php
namespace Project\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\True;

use FOS\UserBundle\Form\Type\RegistrationFormType;

class MyUserRegistrationType extends AbstractType
{

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('user', new RegistrationFormType())
        ->add('cgu', 'checkbox', array(
            'label'     => 'Your label',
            'required'  => true
        ))
       ->add('t_and_c', 'checkbox', array(
                'property_path' => false,
                'constraints' => new True(array('message' => 'Your Confirmation Message','groups' => 'Registration')),
        ))
    ;
}

/**
 * @return string
 */
public function getName()
{
    return 'project_userbundle_user';
}
}

答案 2 :(得分:0)

我看到两个解决方案,第一个是遗产,从FOSUserBundle表单创建遗产。但您在验证用户实体时可能会遇到问题。 我建议您创建一个表单MyUserRegistrationType.php,并添加FOSUserBundle表单和复选框,如下所示:

<?php
namespace Project\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use FOS\UserBundle\Form\Type\RegistrationFormType;

class MyUserRegistrationType extends AbstractType
{

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('user', new RegistrationFormType())
        ->add('cgu', 'checkbox', array(
            'label'     => 'Your label',
            'required'  => true
        ))
    ;
}

/**
 * @return string
 */
public function getName()
{
    return 'project_userbundle_user';
}
}

答案 3 :(得分:0)

Ajeet的回答对于验证表单是正确的,但用户可以提交表单,然后会显示错误。如果您想要阻止用户完全提交表单并且您需要使用javascript或jQuery,还有另一种选择。我使用这个,实际上我隐藏了提交按钮,直到用户填写了所有需要的项目。下面是一个使用jQuery的简单脚本:

<script>
    $('#chkSelect').click(function(){ validatechecked(); });

    validatechecked(){
        var isChecked = $('#chkSelect').prop('checked');
        if(var){
            #submit.show();
        }
        else{
            alert("Please agree to the terms to proceed.")
            #submit.hide();
        }
    };
</script>

您需要使用提交按钮的ID和复选框调整代码。您可以为其添加其他选项以进行更多验证。

我在我的实体中使用它和注释来为用户提供更快的反馈并防止明显错误/不完整的提交。这是你需要添加到twig文件的额外内容,你可能应该将jQuery代码放在一个单独的js文件中。

相关问题