Sonata Admin Bundle - configureFormFields中的不同字段选项

时间:2013-05-24 14:09:59

标签: php forms symfony mode symfony-sonata

是否有可能在Sonata Admin Bundle Form中添加不同的字段,具体取决于您是在创建新实体还是在configureFormFields中编辑现有实体?

2 个答案:

答案 0 :(得分:6)

我不确定这是不是最好的方法,但我已经完成了这个:

protected function configureFormFields(FormMapper $form)
{
    // Add fields common to add AND edit...

    if ($this->getSubject()->getId() > 0) {
        // Add fields only when editing an existing object
    }
}

如果您只想添加新对象的字段,那么显然您也可以添加else条件。

答案 1 :(得分:1)

在官方文档(Click here)中推荐巫婆是一种更好的方法

public void run()
        {
            readGamepiece();
            readLocation();
        }

public void readGamepiece()
    {

        byte[] buffer2 = new byte[1024];
        int bytes;

        while(true)
        {
            try
            {
                bytes = mInStream.read(buffer2);
 mHandler.obtainMessage(TwoPlayerActivityBluetooth.PLAYER_GAMEPIECE_READ, bytes, -1, buffer2).sendToTarget();
            }
            catch(IOException e)
            {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }

    }

    public void writeGamepiece(byte[] buffer2)
    {
        try
        {
            mOutStream.write(buffer2);
mHandler.obtainMessage(TwoPlayerActivityBluetooth.PLAYER_GAMEPIECE_WRITE, -1 , -1 , buffer2).sendToTarget();

        }
        catch(IOException e)
        {
            Log.e(TAG, "Exception during write", e);
        }
    }


    public void readLocation()
    {

        byte[] buffer = new byte[1024];
        int bytes;

        while(true)
        {
            try
            {
                bytes = mInStream.read(buffer);
                mHandler.obtainMessage(TwoPlayerActivityBluetooth.PLAYER_LOCATION_READ, bytes, -1, buffer).sendToTarget();
            }
            catch(IOException e)
            {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }


    public void writeLocation(byte[] buffer)
    {
        try
        {
            mOutStream.write(buffer);
            mHandler.obtainMessage(TwoPlayerActivityBluetooth.PLAYER_LOCATION_WRITE, -1 , -1 , buffer).sendToTarget();
        }
        catch(IOException e)
        {
            Log.e(TAG, "Exception during write", e);
        }
    }

或者您可以这样做:

$subject = $this->getSubject();

if ($subject->isNew()) {
    $formMapper->add('customField', TextType::class);
}