2017-08-10 81 views
0

我試圖使用javascript裏面Symfony然後我有一個問題。該問題位於我的文件twig.html。在開始時我有這個代碼:使用Symfony發送一個值到輸入使用Symfony

{{ form_widget(form.name, {'attr': {'class': 'form-control'} }) }} 

然後我把它改成這樣才能使用動態驗證(這是在瀏覽器中產生相同的代碼,我只是添加的onkeyup動​​作)

<input type="text" id="person_name" 
    onkeyup="myFunction()" name="person[name]" required="required" 
    class="form-control" /> 

然後我很高興,因爲我的驗證規則有效,但是當我想要更新時,我發現了它。該字段名稱是空的(但在服務器中是好的)。所以我想獲得這個領域。在我的功能updateAction我傾倒可變處理形式之前然後是含有良好的元素。所以問題是form_widget和我剛剛做的很好。我願做這樣的事情,讓我現場

<input type="text" id="person_name" 
    onkeyup="myFunction()" name="person[name]" required="required" 
    class="form-control" value="if(form.name is not empty) form.name"/> 

謝謝。

+0

嘗試用'{{form_widget(form.name,{ 'ATTR':{「級':'form-control','onkeyup':'myFunction()'}})}}' – yceruto

+0

非常感謝 – bsm

回答

1

你是否將實體或文檔傳遞給了你的createForm函數? 類似的東西應該工作:

控制器

$em = $this->getDoctrine()->getManager(); 
$your_entity = $em->getRepository(**your_entity_path**)->findOneBy([**parameters**]); 
$edit = 1; 
if(!isset($your_entity)) { 
    $your_entity = new Your_entity(); 
    $edit = 0; 
} 
$form = $this->createForm(**your_entity_type_path**, $your_entity); 
$editForm->handleRequest($request); 

if ($form->isSubmitted() && $form->isValid()) { 
    if(!$edit) 
     $em->persist($your_entity); 
    $em->flush(); 

    //other code 
} 

我希望這不是太亂