Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Symfony2表单验证从文件加载的数据?_Php_Forms_Validation_Symfony_Doctrine Orm - Fatal编程技术网

Php 如何使用Symfony2表单验证从文件加载的数据?

Php 如何使用Symfony2表单验证从文件加载的数据?,php,forms,validation,symfony,doctrine-orm,Php,Forms,Validation,Symfony,Doctrine Orm,从中,以下是验证表单的方法: use Symfony\Component\HttpFoundation\Request; public function createAction(Request $request) { $em = $this->getDoctrine()->getManager(); $form = $this->createForm(new RegistrationType(), new Registration());

从中,以下是验证表单的方法:

use Symfony\Component\HttpFoundation\Request;

public function createAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();    
    $form = $this->createForm(new RegistrationType(), new Registration());    
    $form->handleRequest($request);

    if ($form->isValid()) {
        $registration = $form->getData();    
        $em->persist($registration->getUser());
        $em->flush();    
        return $this->redirect(...);
    }    
    return $this->render(
        'AcmeAccountBundle:Account:register.html.twig',
        array('form' => $form->createView())
    );
}
底线是我们有一个实体
注册
和一个相应的表单
注册类型
。当我们想要验证
$request
中的数据时,我们使用
$form->handleRequest($request)

我想使用相同的方法,但是当我的实体是从一个文件加载而不是通过表单发布时

例如,我有一个文件,其中包含我想要创建帐户的100个用户的列表,但是数据需要以与表单验证相同的方式进行验证,并且可以从表单类中提取错误数组


有没有一种方法可以以类似的方式使用Symfony2自动执行此操作?

嗨,在最近的一个项目中,我们做到了这一点。但是我们读取了整个CSV文件并创建了一些数组来保存这些值。然后,我们使用文件中的所有值创建对象,并使用Symfony验证器验证每个对象。如果你想自动完成,那是不可能的,你必须自己完成

我敢肯定你的答案就在这里!您需要直接使用验证器。@vascowhite作为最后手段,我实际上可以像您提到的那样直接使用验证器。我只是希望看到一种重用form类来验证文件中数据的方法,因为大部分工作已经在那里完成:)也许我会尝试重写
handleRequest()
方法。