Php 如何使用Symfony2中的setter设置表单字段值
我有一个表单,它有自己的模型,其中包含setter和getter,如下所示:Php 如何使用Symfony2中的setter设置表单字段值,php,symfony,Php,Symfony,我有一个表单,它有自己的模型,其中包含setter和getter,如下所示: class CommentAdd { protected $content; protected $yandexCaptcha; protected $isAnonymous; public function setContent($content) { $this->content = $content; } public func
class CommentAdd
{
protected $content;
protected $yandexCaptcha;
protected $isAnonymous;
public function setContent($content)
{
$this->content = $content;
}
public function getContent()
{
return $this->content;
}
public function setYandexCaptcha(YandexCaptcha $yandexCaptcha) {
$this->yandexCaptcha = $yandexCaptcha;
}
public function getYandexCaptcha() {
return $this->yandexCaptcha;
}
public function setIsAnonymous($isAnonymous) {
$this->isAnonymous = $isAnonymous;
}
public function getIsAnonymous() {
return $this->isAnonymous;
}
}
那么,通过调用setter来设置任何字段值的方法是什么呢?我知道使用getter$form->getData->getValue获取任何值的方法,但我不知道设置的方法
更新:
创建表单对象的目的是:
$commentAddForm = $this->createForm(new CommentAddType(), new CommentAdd(), [
'action' => $this->generateUrl('blog_comment_add', ['id' => $id]),
'is_authenticated' => $this->container->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')
]);
//That will return value by using the getter getContent from CommentAdd model
$commentAddForm->getData()->getContent();
//That will return value without using getter from the model
$commentAddForm->get('content')->getData();
//Now I want to know the way to set any data by using setter from the model
$commentAddForm-> ???
另外,我为我的英语感到抱歉。您在创建表单的对象实例上设置了数据;而不是表单字段本身。根据:
最简单的方法是创建表单类型。通读:我有一个表单类型,它有自己的模型。如果我使用$form->get'field->getData,它将返回数据,而不使用模型中的getter。但是我已经找到了使用getter的方法,我在帖子上展示了这一点,现在我想知道set的类似方法。
$task = new Task();
$task->setTask('Write a blog post');
$task->setDueDate(new \DateTime('tomorrow'));
$form = $this->createFormBuilder($task)
->add('task', 'text')
->add('dueDate', 'date')
->add('save', 'submit')
->getForm();