0

我已经直接修改了 FOSUserForm(没有覆盖)以尝试在构建表单时分配默认值,我添加了

array('data'=>'default value') 

如:

namespace FOS\UserBundle\Form\Type;

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

class RegistrationFormType extends AbstractType
{
private $class;

/**
 * @param string $class The User class name
 */
public function __construct($class)
{
    $this->class = $class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username', null, array('data' => 'Default value','label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
        ->add('email', 'email', array('data' => 'Default value','label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
        ->add('plainPassword', 'repeated', array(
            'type' => 'password',
            'options' => array('translation_domain' => 'FOSUserBundle'),
            'first_options' => array('label' => 'form.password'),
            'second_options' => array('label' => 'form.password_confirmation'),
            'invalid_message' => 'fos_user.password.mismatch',
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => $this->class,
        'intention'  => 'registration',
    ));
}

public function getName()
{
    return 'fos_user_registration';
}

它不起作用,我一直收到表单验证消息“请输入用户名”,这是怎么回事?

4

1 回答 1

1

您是否尝试将用户名设置为隐藏字段而不是“null”?或者从表单中完全删除用户名并在实体持久保存在数据库中之前手动设置它。我不推荐第二个选项,除非您在将用户名持久保存到数据库之前执行了检查以确保用户名是唯一的。

编辑:

出于好奇,您为什么要直接修改供应商类?

编辑2:

用户名字段的默认验证可能不应该被删除(如果你这样做,请确保它仍然是数据库中的唯一字段(http://symfony.com/doc/2.0/reference/constraints/UniqueEntity.html) .

如果要设置默认用户名值,而不是在表单中设置 data 选项,请创建一个覆盖默认 createUser 方法的自定义 RegistrationFormHandler:

<?php

namespace Acme\UserBundle\Form\Handler;

use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;

class RegistrationFormHandler extends BaseHandler
{
    /**
     * @return UserInterface
     */
    protected function createUser()
    {
        $user = $this->userManager->createUser();
        $user->setUsername('customusername');

        return $user;
    }
}

可以在此处找到有关覆盖此处理程序的说明(朝向底部): https ://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md

这很简单,只需一分钟即可实现。此外,当您通过 composer 更新您的供应商时,创建自定义表单处理程序还将防止您在供应商类中所做的任何更改丢失。

编辑3:

DependencyInjection\EtienneUserExtension.php:

<?php

namespace Etienne\UserBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;

//if using xml
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

//if using yaml
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;

class EtienneUserExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        //if using XML
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');

        //or if using yaml services
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }

    public function getAlias()
    {
        return 'etienne_user';
    }
}

服务.xml

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">


<services>
    <service id="acme_user.form.handler.registration" class="Acme\UserBundle\Form\RegHandler" scope="request" public="false">
        <argument type="service" id="fos_user.registration.form" />
        <argument type="service" id="request" />
        <argument type="service" id="fos_user.user_manager" />
        <argument type="service" id="fos_user.mailer" />
        <argument type="service" id="fos_user.util.token_generator" />
    </service>
</services>

</container>
于 2012-12-13T17:25:34.393 回答