frg*_*v10 5 zend-framework zend-form zend-validate
我得到了和Zend Form这样做:
class Products_AddForm extends Zend_Form {
public function init() {
$ProductImage1 = $mainform->addElement('file', 'Productimage1', array(
'validators' => array(
array('Count', false, '1' ),
array('Size', false, '10MB'),
array('Extension', false, 'jpg,jpeg,tif,eps'),
),
'required' => false,
'label' => 'Product Image1 (jpg/tif/eps)'
));
Run Code Online (Sandbox Code Playgroud)
然后是一个检查帖子数据的Controller:
public function addAction()
{
$form = $this->getAddForm();
if($this->getRequest()->isPost()){
$post = $this->getRequest()->getPost();
// check post data
if($form->isValid($post) )
{
}
else {
print_r($form->getErrors());
print_r($form->getErrorMessages());
print_r($form->getMessages());
}
Run Code Online (Sandbox Code Playgroud)
一个自定义视图控制器是这样的:
echo '<form method="post" action="'.$this->baseUrl('products/add').'" enctype="multipart/form-data">';
$image1 = $form->getElement('Productimage1');
$helper1 = $image1->helper;
echo '<br/>'.$this->translate('Productimage').' (jpg/tif):<br/>'.$bild1->getView()->$helper1(
$image1->getName(),
$image1->getValue(),
$image1->getAttribs(),
$image1->options
);
Run Code Online (Sandbox Code Playgroud)
当我在textfields中输入一些信息并发布它时,$ form-> isValid()变为false.当我从表单和自定义视图中删除文件部分时,它完美地工作.
Formelement是'required'false.另外控制器的错误部分 - > getErrors(),getMessages()和getErrorMessages()都没有返回错误?!
有谁知道这里发生了什么?
小智 2
试试这样:这是你的表格:
$image = $this->createElement('file','image');
$image->setLabel('Product Image1 (jpg/tif/eps) ')
->setRequired(false)
->setDestination(??)
->addValidator('Count',false,1)
->addValidator('Size',false,10MB)
->addValidator('Extension',false,'jpg,tif,eps');
$this->addElement($image);
Run Code Online (Sandbox Code Playgroud)
这是你的控制器:
$form = new Application_Form_YOURFORM(array('action' => '', 'method' => 'POST'));
if($this->_request->isPost() && $form->isValid($this->_request->getPost()))
{
if($form->image->isUploaded())
{
$form->image->receive();
$this->image = 'destination' . basename($form->image->getFileName());
}
$this->_helper->redirector('..');
}
else
{
$this->view->form = $form;
}
Run Code Online (Sandbox Code Playgroud)
您的看法:
<?php echo $this->form ?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2433 次 |
| 最近记录: |