首页 文章

在表单处理程序中调用表单值

提问于
浏览
-3

这是我的问题 .

我正在学习Symfony,我创建了一个带有formType文件和formHandler的表单 . 我现在想在我的处理程序中使用表单的值,但我无法想出如何调用这些值,我可以使用哪种方法?我已经尝试了许多请求类的方法,但它不起作用 . 请问你能帮帮我吗?

这是我的经纪人 . 我的一些尝试仍在评论中,它很简单,我只是想做一个回声 .

class adminFormHandler
{
    protected $form;
    protected $request;
    protected $em;

    public function __construct(Form $form, Request $request, EntityManager $em)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bindRequest($this->request);

            //if( $this->form->isValid() )
            //{
                //echo $this->request->get('nom');
                //$param = $this->request->request->keys();
                //$param = $this->form->getData(nom);
                //echo $param;
                $myfield = $form->getAttribute('nom');
                echo $myfield->getData();
                //echo $param;//$this->form->get('nom')->getText();
                //$this->onSuccess($this->form->getData());

                return true;
            //}
        }

        return false;
    }

    public function onSuccess1(/*Students $students*/)
    {
        //$this->em->persist($students);
        //$this->em->flush();
        echo'on success 1';
    }

    public function onSuccess2()
    {
        //$this->em->persist($students);
        //$this->em->flush();
        echo'on success 2';
    }
}

1 回答

  • 2

    您可以使用:

    $data = $this->form->getData();
    $myfield = $data['nom'];
    

    要么

    $myfield = $this->form->get('nom')->getData();
    

相关问题