Symfony - 修改原型中的默认数据

时间:2016-10-26 23:50:13

标签: php symfony prototype

所以说我有一个Category和一个Product实体,其中Category有很多Product个实体。我的Category表单构建器如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class) 
            ->add('products', CollectionType::class, array(
            'entry_type' => ProductType::class,
            'allow_add' => true,
            'by_reference' => false,
            'allow_delete' => true,
            'prototype' => true,
            'prototype_name' => '__product_prot__'))
            ->add('save', SubmitType::class))
        ;
    }

我的Product表单构建器如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class) 
            ->add('dateAdded', DateType::class)
        ;
    }

我想知道的是如何设置dateAdded的值,以便每当添加新原型时,它会显示今天的日期?

有些人建议使用Entity __construct()函数来创建默认值,但这似乎不适用于原型。我也找到了placeholder选项,但我不确定如何使用它以便它始终具有今天的日期 - 即这似乎不起作用:

->add('addDate', DateType::class, array(
    'placeholder' => new \DateTime();
))

错误是:Attempted to call function "DateTime" from the global namespace

此外,我在CollectionType字段中找到prototype_data字段,但同样,我不确定如何指定仅将数据放在单个字段中,并使其动态化

有人可以告诉我使用的最佳方法 - 以及正确的语法吗?

修改

所以我的__construct()看起来像是:

public function __construct()
{
    $this->addDate = new \DateTime();
}

哪个适用于Category实体(如果我给它一个测试日期字段),但不适用于原型Product实体。当我将原型加载到表单中时,我只得到2011年1月1日的默认日期。原型看起来像:

{% block _category_products_entry_row %}
    <li>
        {{ form_row(form.name) }}
        {{ form_row(form.dateAdded) }}
    </li>
{% endblock %}

有趣的是,我还发现,如果我使用Product控制器中已创建的Category实体加载新表单,则出现dateAdded字段,原因如下:

{% for product in form.products %}
     {{ form_row(product) }}
{% endfor %}

今天的日期是默认值。这将告诉我,以与How to Embed a Collection of Forms教程相同的方式加载原型 - 导致问题。

1 个答案:

答案 0 :(得分:1)

要为表单字段设置默认值,可以使用“data”属性并使用FormEvent在更新时处理表单。结果如下:

class MusicItem implements Parcelable {
private String title;
private String album;
private String artist;
private long duration;
private Uri albumArtUri;
private Uri fileUri;

public MusicItem title(String title) {
    this.title = title;
    return this;
}

public MusicItem album(String album) {
    this.album = album;
    return this;
}

public MusicItem artist(String artist) {
    this.artist = artist;
    return this;
}

public MusicItem duration(long duration) {
    this.duration = duration;
    return this;
}

public MusicItem albumArtUri(Uri albumArtUri) {
    this.albumArtUri = albumArtUri;
    return this;
}

public MusicItem fileUri(Uri fileUri) {
    this.fileUri = fileUri;
    return this;
}

public String title() {
    return title;
}

public String album() {
    return album;
}

public String artist() {
    return artist;
}

public long duration() {
    return duration;
}

public Uri albumArtUri() {
    return albumArtUri;
}

public Uri fileUri() {
    return fileUri;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    MusicItem item = (MusicItem) o;

    if (duration != item.duration) return false;
    if (title != null ? !title.equals(item.title) : item.title != null) return false;
    if (album != null ? !album.equals(item.album) : item.album != null) return false;
    if (artist != null ? !artist.equals(item.artist) : item.artist != null) return false;
    if (albumArtUri != null ? !albumArtUri.equals(item.albumArtUri) : item.albumArtUri != null)
        return false;
    return fileUri != null ? fileUri.equals(item.fileUri) : item.fileUri == null;

}

@Override
public int hashCode() {
    int result = title != null ? title.hashCode() : 0;
    result = 31 * result + (album != null ? album.hashCode() : 0);
    result = 31 * result + (artist != null ? artist.hashCode() : 0);
    result = 31 * result + (int) (duration ^ (duration >>> 32));
    result = 31 * result + (albumArtUri != null ? albumArtUri.hashCode() : 0);
    result = 31 * result + (fileUri != null ? fileUri.hashCode() : 0);
    return result;
}

@Override
public String toString() {
    return "MusicItem{" +
            "title='" + title + '\'' +
            ", album='" + album + '\'' +
            ", artist='" + artist + '\'' +
            ", duration=" + duration +
            ", albumArtUri=" + albumArtUri +
            ", fileUri=" + fileUri +
            '}';
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.title);
    dest.writeString(this.album);
    dest.writeString(this.artist);
    dest.writeLong(this.duration);
    dest.writeParcelable(this.albumArtUri, 0);
    dest.writeParcelable(this.fileUri, 0);
}

public MusicItem() {
}

protected MusicItem(Parcel in) {
    this.title = in.readString();
    this.album = in.readString();
    this.artist = in.readString();
    this.duration = in.readLong();
    this.albumArtUri = in.readParcelable(Uri.class.getClassLoader());
    this.fileUri = in.readParcelable(Uri.class.getClassLoader());
}

public static final Creator<MusicItem> CREATOR = new Creator<MusicItem>() {
    public MusicItem createFromParcel(Parcel source) {
        return new MusicItem(source);
    }

    public MusicItem[] newArray(int size) {
        return new MusicItem[size];
    }
};
}

在PRE_SET_DATA上,您可以使用您提取的数据覆盖dateAdded字段的默认值。

有关详情,请访问http://symfony.com/doc/current/reference/forms/types/date.html#datahttps://symfony.com/doc/current/form/events.html

希望有所帮助