此時我正在Zend Framework中使用Form。 它必須成爲一個公司可以填寫候選人詳細信息的表格。Zend Framework:使用Zend Form元素上傳文件
我對Zend很新,所以這就是爲什麼我發佈我的問題。
這是形式的樣子(一些字段中刪除,使代碼更短)
<?php
class Application_Form_Validate_ContactMethodSelected extends Zend_Validate_Abstract {
const INVALID = 'invalid';
protected $_messageTemplates = array(
self::INVALID => 'Ten minste 1 vorm van contact invullen, telefoon, mobiel of e-mail'
);
public function isValid($value, $context = array())
{
// You need to use your element names, consider making these dynamic
$checkFields = array('telefoon','mobiel','mail');
// Check if all are empty
foreach ($checkFields as $field) {
if (isset($context[$field]) && !empty($context[$field])) {
if (!empty($value)) {
// This is the element with content... validate as true
return true;
}
// we are going to return false and no error
// to break validation chain on other empty values
// This is a quick hack, don't have time to invest in this
return false;
}
}
// All were empty, set your own error message
$this->_error(self::INVALID);
return false;
}
}
class Application_Form_Nieuwkandidaat extends Zend_Form {
public function init() {
$this->setMethod('post');
$DB = Zend_Db_Table::getDefaultAdapter();
$id = $this->createElement('hidden', 'id');
$voornaam = $this->createElement('text', 'voornaam');
$voornaam->setLabel('Voornaam:')
->setAttrib('size', 50)->addValidator('StringLength', false, array(2, 30))
->setRequired(true);
$telefoon = $this->createElement('text', 'telefoon');
$telefoon->setLabel('Telefoon:')
->setAttrib('size', 50)->setAllowEmpty(false)
->addValidator(new Application_Form_Validate_ContactMethodSelected(), true)
->addValidator('StringLength', false, array(10, 10));
$mobiel = $this->createElement('text', 'mobiel');
$mobiel->setLabel('Mobiel:')
->setAttrib('size', 50)->setAllowEmpty(false)
->addValidator(new Application_Form_Validate_ContactMethodSelected(), true)
->addValidator('StringLength', false, array(10, 10));
$mail = $this->createElement('text', 'mail');
$mail->setLabel('E-mail:')
->setAttrib('size', 50)->setAllowEmpty(false)
->addValidator(new Application_Form_Validate_ContactMethodSelected(), true)
->addValidator('StringLength', false, array(6, 40))->addValidator('EmailAddress', true);
$register = $this->createElement('submit', 'register');
$register->setLabel("Verstuur")
->setIgnore(true);
$reset = $this->createElement('reset', 'reset');
$reset->setLabel("Reset")
->setIgnore(true);
$this->addElements(array(
$voornaam,
$mobiel,
$telefoon,
$mail,
$id,
));
$this->addDisplayGroup(array(
'voornaam',
'mobiel',
'telefoon',
'mail',
'telefoon'
), 'contacts', array('legend' => 'Contact Informatie'));
$contacts = $this->getDisplayGroup('contacts');
$contacts->setDecorators(array(
'FormElements',
'Fieldset',
array('HtmlTag', array('tag' => 'div', 'style' => 'width:50%;;float:left;'))
));
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div', 'style' => 'width:98%')),
'Form'
));
$this->addElement($register);
}
}
我如何添加一個表單元素,我可以選擇一個文件並上傳它,所以它會被存儲(比方說,例如在/應用/ TMP)
如果非要將代碼從我的控制器的地方,請讓我知道提前以及
謝謝!
我很困惑,你問一個表單元素並向我們展示你的關於驗證類的代碼? –
-1我沒有收到回覆。 – JellyBelly