Reputation: 171
Hi everyone I come to you because I have a little problem with my ajax script I share my code and tell me if there is a problem thanks for advances, if you have any solution I thank you, the error made This is:
Argument 1 passed to App \ Repository \ ProductRepository :: findBySearch () must be of the string type, object given, called in C: \ wamp64 \ www \ Shop \ src \ Controller \ FrontController.php on line 201
my controller :
/**
* @Route("/recherche/", name="search", methods="POST")
* @param Request $request
* @return Response
*/
public function search(Request $request): Response{
$search = $this->createForm(SearchType::class);
$search->handleRequest($request);
if($request->isXmlHttpRequest()) {
$value = $search['name'];
$result = $this->getDoctrine()->getRepository(Product::class)->findBySearch($value);
return new JsonResponse($result);
}
return $this->render('inc/search.html.twig', [
'title' => 'Effectuer une recherche',
'search' => $search->createView()
]);
}
My repository in which the SQL query is performed :
/**
* @param $value
* @return string
* @throws \Exception
*/
public function findBySearch(string $value) {
$bool = 1;
$query = $this->createQueryBuilder('r')
->select('r')
->orwhere('r.name LIKE :chaine')
->orWhere('r.description LIKE :chaine')
->andWhere('r.isPublished = :bool')
->orderBy('r.createdAt', 'DESC')
->setParameter(':chaine', '%'.$value.'%')
->setParameter(':bool', $bool)
->getQuery();
try {
return $query->getResult();
}
catch (\Exception $e) {
throw new \Exception('Problème' . $e->getMessage());
}
}
this is my code that allows me to make ajax requests
<!-- Modal Search -->
<div class="modal-search-header flex-c-m trans-04 js-hide-modal-search">
<div class="container-search-header">
<button class="flex-c-m btn-hide-modal-search trans-04 js-hide-modal-search">
<img src="images/icons/icon-close2.png" alt="CLOSE">
</button>
<div id="resultat"></div>
{{ form_start(search, {'method': 'POST', 'attr': {'class': 'wrap-search-header flex-w p-l-15', 'id': 'form'}}) }}
<button class="flex-c-m trans-04">
<i class="zmdi zmdi-search"></i>
</button>
{{ form_widget(search.name) }}
{{ form_end(search) }}
</div>
</div>
<script>
$(document).ready(function(){
$("#form").keypress(function(){
$.ajax({
type:"POST",
data: $(this).serialize(),
url:"{{ path('search') }}",
success: function(data){
$("#search_name").html(data);
$.post( "{{ path('search') }}", function( data ) {
$( "#resultat" ).html( data );
});
},
error: function(){
$("#search_name").html('Une erreur est survenue.');
}
});
return false;
});
});
</script>
Upvotes: 1
Views: 1258
Reputation: 3115
Replace
$value = $search['name'];
with
$value = $search->get('name')->getNormData();
Since $search
is a FormInterface
object, you won't get the submitted value just through $search['name']
. You could call $search->getData()
to get the whole form data OR $search->get('YOUR_FIELD_NAME')->getNormData()
to get an specific value
Also, consider to wrap it with
if($search->isSubmitted()) {
if($request->isXmlHttpRequest()) {
// ...
}
}
Upvotes: 3