[英]ZendFramework2 posting form
这是实际上应该将表单数据发送到数据库中的方法,但是我无法弄清楚为什么它不起作用。没有出现错误,但数据未插入表中。 如果您能帮助我,我将非常高兴!
//method to add data to the database
public function addAction() {
//add user info
$form = new UserForm();
$form->get('submit')->setValue('add new info');
$request = $this->getRequest();
if($request->isPost()){
$user = new User();
$form->setData($request->getPost());
if($form->isValid()){
$user->exchangeArray($form->getData());//method that gets the validated data
$this->getUserTable()->saveUser($user);
return $this->redirect()->toRoute('application',array(
'controller'=>'user',
'action'=>'index'
));
}
}
//pass it to the view
$values = array('form'=>$form);
$view = new ViewModel($values);
return $view;
}
视图add.phtml
$form = $this->form;
$form->setAttribute('action', $this->url(
'application', array(
'controller' => 'user',
'action' => 'add',
)
));
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('email'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
$user = new User();
//user class contains the user information that you can store in the db
$user->name = "Anime lover";
$user->email = "Anime mail";
$this->getUserTable()->saveUser($user);
$view = new ViewModel($values);
return $view; <- because of this return
--------------你永远不会在这里----------------------
//add user info etc....
$form = new UserForm();
也许是为了测试? 是吗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.