开发者

Routing with parameter and a widget JQueryAutocompleter (symfony)

I have a form with a widget autocomplete, but when I add a parameter to the URL, my widget doesn't work.

I think it's a problem with routing because when I delete the parameter, the autocompletion works but I don't know how and where I have to define the new route. (I have tried in myModule/config/routing.yml but I think I don't understand how to use new routing).

Link to go on my form :

<a href="<?php echo url_for('refus/new?logement='.$logement->getId()) ?>">Nouveau refus</a>

Actions :

  public function executeAutocompleteNud(sfWebRequest $request) {
  //开发者_Python百科 Fonction générant une liste de locataire pour le champs autocomplete
    $this->getResponse()->setContentType('application/json');

    // Récupération de la chaine entrée par l'utilisateur
    $string = $request->getParameter('q');

    // Requète récupérant la liste des locataires dont le nom contient la chaine entrée
    $requete = Doctrine::getTable('locataire')->getDataWhereNUD($string);

    // Construction d'un tableau associatif à partir des résultats de la requète
    $resultats = array();
    foreach ($requete as $res):
        $resultats[$res->getNud()] = $res->getNud();
    endforeach;

    return $this->renderText(json_encode($resultats));
  }

  public function executeNew(sfWebRequest $request)
  {
    $log = $request->getParameter('logement');

    $refus = new Refus(); 
    $refus->set('logement', $log); 

    $this->form = new refusForm($refus);

    $this->setTemplate('new');
  }

Widget schema :

$this->widgetSchema['locataire'] = new sfWidgetFormJQueryAutocompleter(array('url' => 'autocompleteNud',
                                                                                     'config' => '{
                                                                                            scrollHeight: 300,
                                                                                            autoFill: true}'));


routing system was a mistery to my also but after reading a lot maybe i cuold help you. In apps > app_name > confing > routing.yml add a the TOP of the file. this route:

refus_new_route:
  url: /refus/new/:logment_id
  params: { module: refus , action: new}
autocompleteNud:
  url: /refus/autocompleteNud/:q
  params: { module: refus , action: autocompleteNud}

That tells symfony that when someone asks for something like: http://hosts/index.php/refus/new/10 it should look in the module refus an action called new and pass a parameter called logment_id .

After doing this you should clear cache.

Second think to do is to change this

<a href="<?php echo url_for('refus/new?logement='.$logement->getId()) ?>">Nouveau refus</a>

for this

<?php echo link_to('Nouveau refus', 'refus/new', array('logement' => $logement->getId())); ?>

Finally, modify your widget to usea the new route:

sfConfig::getInstance()->getConfiguratio()->loadHelpers('Url');
$this->widgetSchema['locataire'] = new sfWidgetFormJQueryAutocompleter(array('url' => url_for('@autocompleteNud'),
                                                                                     'config' => '{
                                                                                            scrollHeight: 300,
                                                                                            autoFill: true}'));

Hope this is useful!


I have resolved my problem using this way :

symfony : Form with one parameter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜