私は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'
)
));
}
}
とポストフィールドセット:
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
));
}
とポストInputFilter:
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;
}
}
と、これは私の行動である
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'
)
));
}
}
私はポストフォーム持っています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
オブジェクトに水和されていないデータを、また、データをダンプし、2つのタイトルと2つのテキストやフィールドセット名を示し、私は理解していません。 もし私が$this->postForm->setInputFilter(new PostFilter());
を取り除くと、データ水和物はPost
になります。
なぜ検証は機能しないのですか、フォームのエラーが表示されないのですか?また、データがPost
オブジェクトに水和していないのはなぜですか?
問題は、そのようInputFilter宣言ではありませんドキュメント内には、このクラスに似た例があります: 'class TaskFilter extends InputFilter'。問題は、 'Fieldset'を使用しているときです。私は' FieldSet 'を使って 'FieldSet'を使うだけです。すべてうまく動作します。 –
私の答えが更新されました。 – Hooli