我遵循了ZF2.4手冊第12章(介紹我們的第一個「博客」模塊),並創建了Blog
模塊。 我有帖子形式:ZF2窗體在使用現場集時不驗證
class PostForm extends Form{
public function __construct($name = null, $options = array()){
parent::__construct($name, $options);
$this->add(array(
'name' => 'post-fieldset',
'type' => 'Blog\Form\PostFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
)
);
$this->add(array(
'type' => 'submit',
'name' => 'submit',
'attributes' => array(
'value' => 'Insert new Post',
'class' => 'btn btn-primary'
)
));
}
}
和郵政字段集:
class PostFieldset extends Fieldset{
public function __construct($name = null, $options = array()){
parent::__construct($name, $options);
$this->setHydrator(new ClassMethods(false));
$this->setObject(new Post());
$this->add(array(
'type' => 'hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'text',
'name' => 'text',
'options' => array(
'label' => 'The Text'
),
'attributes' => array(
'class' => 'form-control'
)
));
$this->add(array(
'type' => 'text',
'name' => 'title',
'options' => array(
'label' => 'Blog Title'
),
'attributes' => array(
'class' => 'form-control'
)
));
}
}
這是我的行動:
public function addAction(){
$request = $this->getRequest();
if ($request->isPost()) {
$this->postForm->setInputFilter(new PostFilter());
$this->postForm->setData($request->getPost());
if ($this->postForm->isValid()) {
echo "The form is valid\n";
//Debug:: dump($this->postForm->getData()); die();
// save post...
}else{
echo "The form is not valid\n";
Debug:: dump($this->postForm->getData()); die();
}
}
return new ViewModel(array(
'form' => $this->postForm
));
}
和郵政輸入過濾:
class PostFilter extends InputFilter {
public function __construct(){
$title = new Input('title');
$title->setRequired(true);
$title->setValidatorChain($this->getTextTitleValidatorChain());
$title->setFilterChain($this->getStringTrimFilterChain());
$text = new Input('text');
$text->setRequired(true);
$text->setValidatorChain($this->getTextTitleValidatorChain());
$text->setFilterChain($this->getStringTrimFilterChain());
$this->add($title);
$this->add($text);
}
protected function getTextTitleValidatorChain(){
$notEmpty = new NotEmpty();
$stringLength = new StringLength();
$stringLength->setMin(5);
$stringLength->setMax(20);
$validatorChain = new ValidatorChain();
$validatorChain->attach($notEmpty);
$validatorChain->attach($stringLength);
return $validatorChain;
}
protected function getStringTrimFilterChain(){
$filterChain = new FilterChain();
$filterChain->attach(new StringTrim());
return $filterChain;
}
}
和add.phtml查看:
<?php
$form = $this->form;
$form->setAttribute('action', $this->url());
$form->prepare();
echo $this->form()->openTag($form);
?>
<div class="form-group" >
<?php echo $this->formRow($form->get('post-fieldset')->get('title')); ?>
</div>
<div class="form-group" >
<?php echo $this->formRow($form->get('post-fieldset')->get('text')); ?>
</div>
<?php echo $this->formSubmit($form->get('submit')); ?>
<?php echo $this->form()->closeTag(); ?>
如果我提交表單,表單錯誤不顯示。 另外,如果我輸入有效的數據,我看到的數據轉儲如下所示:
The form is not valid
array(4) {
["title"] => NULL
["text"] => NULL
["submit"] => string(15) "Insert new Post"
["post-fieldset"] => array(3) {
["id"] => NULL
["text"] => string(7) "my text"
["title"] => string(8) "my title"
}
}
未水進入Post
對象中的數據,也轉儲數據顯示了兩個冠軍和兩個文本字段集的名字,我不明白。 ,如果我刪除$this->postForm->setInputFilter(new PostFilter());
數據水合物到Post
對象。
爲什麼驗證無法正常工作,並且沒有顯示錯誤,以及爲什麼數據不會水化爲Post
對象?
的問題是不是在聲明輸入過濾像這樣,becuz我已經測試它,而無需使用'FieldSet中'和驗證正在工作,並且在doc中有一個類似於這個例子的例子:class TaskFilter擴展了InputFilter。問題是當我使用'Fieldset'時,如果我只使用'Form'而不使用FieldSet'一切正常, –
已更新我的答案 – Hooli