2011-08-11 73 views
1

我想使用zend_form驗證和過濾POST數據,並禁用某些表單字段元素, 但是當我使用$ form-> isValid($ post)時,過濾數據並使用$ form-> getValues()獲取過濾數據,它將返回所有元素值(包括我不想要的禁用元素值)。zend_form - 如何獲取除禁用元素以外的表單值

如:

<form method="post" action=""> 
<input type="text" disabled="disabled" name="account_id" value="123456"> 

<input type="text" name="name" value=""> 
<input type="text" name="email" value=""> 

<input type="text" disabled="disabled" name="created_date" value="2011-06-12"> 
<input type="text" disabled="disabled" name="created_by" value="admin"> 
<input type="submit"> 
</form> 

那麼有沒有什麼辦法可以擺脫禁用元素的值嗎? (因爲有很多領域和禁用的元素,所以我不想手動修剪它們)

謝謝!

+0

你有答案嗎? –

+0

你的表單怎麼能發佈禁用的元素值?它違反規範。 – Marecky

回答

4

這是某種黑客行爲。我們獲取所有元素並遍歷它。當我們看到一個元素被禁用時,我們可以跳過。

$somearray = array(); 
$elements = $form->getElements(); 
foreach ($elements as $key => $element) { 
    //echo $key; 
    if($element->disabled) { 
     continue; 
    } 
    $somearray[$key] = $element->getValue(); 
} 

希望這會有所幫助,或者你可以破解它;)。

2

看起來這不是一個錯誤,而是驗證表單的可接受工作流程。看到這一點:http://framework.zend.com/issues/browse/ZF-6909

它看起來像接受的解決方案/訣竅是使用的

$form->isValidPartial($this->getRequest()->getPost())

代替

$form->isValid($this->getRequest()->getPost())

isValidPartial只測試中存在的崗位表單字段。禁用的元素不應該最終發佈。

+0

他正在嘗試'$ form-> getValues()來獲取過濾的數據,它會返回所有元素值(包括我不想要的禁用元素值)''。所以'isValidPartial()'似乎不是那個。 –

+2

我很確定你是不正確的。 getValues()獲取由isValid或isValidPartial函數驗證/過濾的所有值。 isValidPartial只驗證帖子中的元素。禁用的元素將永遠不會成爲帖子的一部分。 http://www.w3.org/TR/html4/interact/forms.html#h-17.12.1「在這個例子中,INPUT元素是禁用的,因此它不能接收用戶輸入,也不會提交它的值表格。」 –

+0

是的你是對的,禁用的元素不會在後。這對我來說是新事物。謝謝你的提示。 –

相關問題