我正在尝试使用ValidatorInterface在控制器中验证使用ajax提交的表单。
但是我得到这个错误:
无法解析“ App \ SocialStudio \ AdminBundle \ Controller \ InfluencersController :: addinfluencerajax()”的参数$ validator,也许您忘记了将控制器注册为服务或错过了使用“ controller.service_arguments”对其进行标记的操作? / p>
这是我的控制者:
<?php
namespace App\SocialStudio\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Influencer;
use App\Form\Type\InfluencerType;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class InfluencersController extends AbstractController
{
public function Influencers(Request $request)
{
// Get all Inlfuencers
$influencers = $this->getDoctrine()
->getRepository(Influencer::class)
->findAll();
// create modal form
$influencer = new Influencer();
$addInfluencerForm = $this->createForm(InfluencerType::class,$influencer);
return $this->render('@Admin/influencers.html.twig',[
'menu' => 'influencers','influencers' => $influencers,'addInfluencerForm' => $addInfluencerForm->createView(),]);
}
public function addInfluencerAjax(Request $request,ValidatorInterface $validator)
{
... form handling happens in this function ...
}
}
这是我的ajax调用代码:
$('#submitAddInfluencerForm').click(function(e){
e.preventDefault();
addInfluencerForm = $('#addInfluencerForm').serialize();
$.ajax({
url:'{{ path('AddInfluencerAjax') }}',type: "GET",dataType: "json",data: {
"influencer": addInfluencerForm
},async: true,success: function (return_data)
{
...
}
});
});
services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
然后我将其放入我的framework.yaml
framework:
validation: { enable_annotations: true }
`