logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Name instead of the number, to show up for jquery autocomplete?

Symfony 1.4 and Doctrine.

I have a wine form with a foreign key relationship to the winery table, and I'm using sfWidgetFormJQueryAutocompleter so the user can start typing a winery name and the names of possibly wineries will be suggested. Everything works great except that when the page first loads, the user sees the numeric id of the winery instead of the name of the winery. Please see the attached screenshot and you'll see what I mean.

In the form class I have:

    sfProjectConfiguration::getActive()->loadHelpers('Url');
$url = url_for('winery/autocomplete');
$this->widgetSchema['winery_id'] = new sfWidgetFormJQueryAutocompleter(array(
'url' => $url,
));


And I have this action code, which works great:


   public function executeAutocomplete(sfWebRequest $request)
{
$this->getResponse()->setContentType('application/json');
$q = "%" . $request->getParameter('q') . "%";
$limit = $request->getParameter('limit');

// FIXME: use $limit
$dql = Doctrine_Query::create()
->from('Winery w')
->where('w.name LIKE ? ', array($q));
$this->rows = $dql->fetchArray();

$person = array();
foreach ($this->rows as $row) {
$persons[$row['id']] = (string) $row['name'];
}

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


But how do I get it so that the form shows the name of the winery, not the id, when the page first loads?

This question has been answered.

attachment image asker uploaded image

Lawrence Krubner | 03/11/11 at 10:20pm Edit


(2) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    03/14/11
    11:32am
    Marcos IbaƱez says:

    If you take a peek into sfWidgetFormJqueryAutocompleter.class.php you'll see that you have 2 options to specify the first value to be displayed by de form:

    1. Specify a callback that produces such value in the form, in your case it'll be something like:


    $this->widgetSchema['winery_id'] = new sfWidgetFormJQueryAutocompleter(array(
    'url' => $url,
    'value_callback' => 'thisFunctionShouldReturnTheNameOfTheWinery'
    ));


    2. And if you have personalized templates and you are rendering the form "manually" (calling the render, renderError and renderLabel methods of each field in the template) I think the best would be to pass the value you want to display initially to the render method:

    $form['winery_id']->render('winery_id', 'theNameOfTheFirstWineryToShow');


    If you choose the second way, DON'T specify the value_callback because that takes precedence over manually specified values passed to the render method.

  • avatar
    Last edited:
    03/12/11
    5:59pm
    Loban Rahman says:

    Are you sure it shouldn't be:

    $persons[$row['name']] = $row['id'];

    'Cause that's how I remember it off the top of my head.

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.