我有一个拥有自己的模型的表单包含这样的setter和getters:
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-> ???
P.S。我很抱歉我的英语。
答案 0 :(得分:0)
您可以在创建表单的对象实例上设置数据;不在表单字段本身。根据{{3}}:
$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();