在 Symfony2 中验证搜索表单后更新 AngularJS 范围

Updating AngularJS scope after validating search form in Symfony2

提问人:Dylan Gauthier 提问时间:1/28/2016 更新时间:2/12/2016 访问量:483

问:

嗨,各位开发人员,

我们必须使用 AngularJS 在 Symfony2 中重写软件应用程序,我们将 Symfony2 用于 MVC 目的,使用 AngularJS 用于有用的功能。

这是我们的问题,我们首先在我的 Symfony2 视图中使用 AngularJS 在表中显示以下代码的客户端:

var app = angular.module('appName', []);
app.controller('clientsCtrl', function ($scope, $http){
    $scope.loading = true;
    $http.post('{{ url(generatedScope) }}').then(function(response){
        $scope.clients = response.data;
        $scope.order = function(predicate){
            $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse :false;
            $scope.predicate = predicate;
        }
    },function(response){
        // TODO: handle the error somehow
    })
});

这是 Symfony2 控制器发送的 Twig 变量,具有以下功能:{{ url(generatedScope) }}

 /**
 * @Route("/clients", name="index")
 */
public function indexAction(Request $request)
{       
    $form = $this->createForm(SearchClients::class, null);
    $form->handleRequest($request);

    if($form->isValid()){
        //TODO: Get form params and update angularJS scope somehow
    }

    return $this->render('clients/list.html.twig', [
        'generatedScope' => 'getClients',
        'form' => $form->createView()
    ]);
}

getClients是打开视图时默认路由的名称(我们没有使用 Doctrine):clients/list.html.twig

 /**
 * @Route("/clients/list/json", name="getClients")
 */
public function getClientsAction()
{
    $clients = new Clients($this->get('database_connection'));
    $response = new JsonResponse($clients->getClients());
    return $response;
}

所以基本上我们的控制器发送的是 : ,它是我们客户端的 JSON 集合,然后我们以这种方式在表中显示客户端:generatedScope127.0.0.0:8000/clients/list/json

<tr ng-repeat="x in clients | filter : test | orderBy:predicate:reverse">
    <td>#{{ x.cli_id }}</td>
    <td>{{ x.cli_lastname }}</td>
    <td>{{ x.cli_firstname }}</td>
</tr>

我们有一个搜索表单,与显示客户的表格相同的页面,我们收集名字和姓氏,并调用一个操作来检索 json 响应以更新我们的角度范围,我们设法以这种方式检索响应:

 /**
 * @Route("/tiers/list/json/search", name="searchClients")
 */
public function searchClientsAction(){
    $request = new Request($_POST);
    $request = $request->query->get('search_clients'); //form name
    $clients = new clients($this->get('database_connection'));
    $response = new JsonResponse($clients->searchClients($request));
    return $response;
}

在验证表单后,我们尝试发送此路由视图:indexAction

if($form->isValid()){
    //TODO: Get form params and update angularJS scope somehow
    return $this->render('clients/list.html.twig', [
        'generatedScope' => 'searchClients',
        'form' => $form->createView()
    ]);
}

但正如你所看到的,它不起作用。

那么,我们如何在验证 Symfony2 表单后更新 angularJS 范围呢?

如果有人遇到过这个问题...很高兴阅读他的意见!

此致敬意

迪伦

javascript php angularjs json symfony

评论

0赞 DevDonkey 1/28/2016
在为什么要创建一个新实例中?只需键入 hint 操作,就会由 symfony 填充。除非我误解了你的代码,否则这可能是问题所在吗?searchClientsActionRequestRequest $request
1赞 kodvin 2/4/2016
从你的措辞来看,我不太确定你是否在提交你的表格。如果您同步发布表单,则 '{{ url(generatedScope) }}' 应该容器 “searchClients” 路由,但您没有专门传递 'search_clients“ 的数据属性,其中 'searchClientsAction' 需要查询。另一方面,如果您异步发布,即使发布请求包含所有必要的表单发布数据,它也应该返回 Twig 生成的 html(来自索引页面,但具有不同的 generatedScope 变量)
0赞 kodvin 2/4/2016
如果你能得到if($form->isValid())子句,你可以复制整个'searchClientsAction'函数内容(我不建议这样做)。在我们将angularjs添加到symfony后端后,我们停止使用表单,并开始使用后端作为rest api。然后,在按下搜索按钮后,您将调用带有“search_clients”到“searchClientsAction”路由的$http,并分配$scope.clients = response.data;
0赞 Dylan Gauthier 2/12/2016
@Kodvin是对的,请查看我自己对这个话题的回答。可以告诉我你对我的解决方案的看法,太棒了!谢谢大家:-)

答:

0赞 Dylan Gauthier 2/12/2016 #1

@Kodvin是对的,我想出了以下解决方案:

首先,我的 S2 控制器:

 /**
 * @Route("/tiers", name="index")
 */
public function indexAction()
{       
    return $this->render('tiers/list.html.twig', [
        'generatedScope' => 'searchTiers',
    ]);
}

generatedScope在我的控制器中定义了另一个获取数据的函数:searchTiers

/**
 * @Route("/tiers/list/json/search", name="searchTiers")
 */
public function searchTiersAction(Request $request){
    // TODO: A bit of refactoring needed there :-)
    $prenom = array('first_name' => $request->request->get('first_name'));
    $nom = array('last_name' => $request->request->get('last_name'));

    $params['first_name'] = $prenom['first_name'];
    $params['last_name'] = $nom['last_name'];

    $tiers = new Tiers($this->get('database_connection'));
    $response = new JsonResponse($tiers->searchTiers($params));

    return $response;
}

我的表单(摆脱了 Symfony 2 表单...但我认为我也可以用 S2 形式做到这一点):

<form method="post" ng-submit="submit()" id="search_tiers">
    <input ng-model="last_name" type="text" placeholder="Nom" name="last_name">
    <input ng-model="first_name" type="text" placeholder="Prénom" name="first_name">
    <input ng-model="submit" type="submit" value="Rechercher" name="submit_form">
</form>

将我的输入绑定到示波器ng-model

在我的 中单击提交按钮时调用 scope 函数submit()

app.controller('tiersCtrl', function ($scope, $http){
    $scope.submit = function() {
        var req = {
            method: 'POST',
            url: '{{ url(generatedScope)}}',
            headers: {
                'Content-Type': 'application/json'
            },
            data: { last_name: $scope.last_name, first_name: $scope.first_name }
        }

        $scope.loading = true;
        $http(req).then(function(response){
            $scope.tiers = response.data;
        },function(response){
            // TODO: handle the error somehow
        });
    }
});

{{ url(generatedScope }}是一个 Twig var,在我的 S2 控制器中调用我的 searchTiersAction() 以返回数据。

最后,显示数据:

<tr ng-repeat="x in tiers">
    <td>#{{ x.tie_id }}</td>
    <td>{{ x.tie_nom }}</td>
    <td>{{ x.tie_prenom }}</td>
    <td>{{ x.tie_adresse }}</td>
    <td>{{ x.tie_cp }}</td>
    <td>{{ x.com_nom }}</td>
    <td class="visible-none">{{ x.tpt_nom }}</td>
</tr>

我所缺少的只是我在提交表单时丢失了我的 POST 参数,Ajax 请求完全丢失了......这基本上是 Angular 的逻辑。

如果你们有任何其他解决方案,请告诉我们!

目前,我希望它有所帮助!:)

迪伦