2015-07-13 65 views
2

我有一个表单中的下拉选择标签名称很长的列表。我想实现jquery自动完成但我当前的代码不起作用。正确的方式呈现json在html中

public function executeNew($r) { 
    $this->getResponse()->setContentType('application/json'); 
    $cityId = Doctrine_Core::getTable('City')->getCity(); 
    return $this->renderText(json_encode($cityId)); 
    $this->form = new VotersForm(); 
    if($r->isMethod('post')) { 
     $this->errors = array(); 
    $this->form->bind($r->getParameter('voters')); 
     if($this->form->isValid()) { 
      $values = $this->form->getValues(); 
      $firstname = $values['firstname']; 
      $middlename = $values['middlename']; 
      $lastname = $values['lastname']; 
      $birthday = $values['birthday']; 
      $email = $values['email']; 
      $address = $values['address']; 
      // $city = $values('city_id'); 
      $city = $cityId; 
      $cellphone = $values['mobile_no']; 
      $telephone = $values['telphone_no']; 
      $profession = $values['profession_id']; 
      $status = $values['civil_status']; 
      $comments = $values['comments']; 
      Doctrine_Core::getTable('Voters')->AddNewVoters($firstname,$middlename,$lastname,$birthday,$email,$address,$city,$cellphone,$telephone,$profession,$status, $comments,$date= date('Y-m-d, h:i:s')); 
      $this->getUser()->setFlash("good", "You are added successfully"); 
      $this->redirect('duterte/showtotals'); 
     } else { 
      $this->errors = 'Oops.Something is missing.Please check for required fields'; 
     } 
    } 
} 

的$城市场是在一个相关的模型(市)的许多relationship.Using上面的代码,我可以从相关模型“拉” json的,但我的问题是,我的代码不显示形式properly.Instead,它会显示这样的事情

{"Candon":"Candon","Vigan":"Vigan","Alilem":"Alilem","Banayoyo":"Banayoyo","Bantay":"Bantay","Burgos":"Burgos","Cabugao":"Cabugao","Caoayan":"Caoayan","Cervantes":"Cervantes","Galimuyod":"Galimuyod","Gregorio } 

如何正确显示的形式,自动完成下拉列表?

<?php if(isset($errors) && !empty($errors)):?> 
    <div class="alert alert-warning"><?php echo $errors ?></div> 
<?php endif ?> 
<div class="page-header"> 
    <h3>Register here</h3> 
</div> 
<form method="post" class="form-horizontal"> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['firstname']->renderRow(array('class'=>'form-control','placeholder' => 'firstname')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['middlename']->renderRow(array('class'=>'form-control','placeholder' => 'middlename')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['lastname']->renderRow(array('class'=>'form-control','placeholder' => 'lastname')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-2"> 
     <?php echo $form['birthday']->renderRow(array('class'=>'form-control','placeholder' => 'birthday')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['address']->renderRow(array('class'=>'form-control','placeholder' => 'address')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['city_id']->renderRow(array('class'=>'form-control required city_autocomplete')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['profession_id']->renderRow(array('class'=>'form-control')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['civil_status']->renderRow(array('class'=>'form-control')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-10"> 
     <?php echo $form['comments']->renderRow(array('class'=>'form-control','placeholder' => 'why Duterte')) ?> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10"> 
     <input type="submit" value="I Will Vote Duterte For President" class="btn btn-primary" /> 
     <input type="button" class="btn btn-danger" onclick="resetFunction()" value="Reset form" /> 
    </div> 
</div> 

回答

0

您将需要建立在你的形式下拉列表中选择控件试图表单元素上应用renderRow之前。这里有一个例子:

$cityArray = array(); 
foreach($cityId as $city) { 
    //create an array from the City models 
    $cityArray[$city->city_id] = $city->city_name; 
    //set the key to be the value in your dropdown select and the value to be the displayed label. 
} 
$form->setWidget('city_id', new sfWidgetFormChoice(array(
    'choices' => $cityArray, //your list of cities, in an associative array 
    'default' => '0' //choose a default key/value 
))); 

来源:Symfony Documentation