Ich befolgte das ZF2.4-Handbuch Kapitel 12 (Einführung unseres ersten "Blog" -Moduls), und ich habe ein Blog
-Modul erstellt. Ich habe Beitrag Form:Das ZF2-Formular wird nicht validiert, wenn Feldsätze verwendet werden.
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'
)
));
}
}
und die Post Fieldset:
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'
)
));
}
}
dies meine Aktion:
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
));
}
und die Post Eingangsfilter:
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;
}
}
und add.phtml Ansicht:
<?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(); ?>
Wenn ich das Formular absende, werden Formularfehler nicht angezeigt. Auch wenn ich gültige Daten eingeben sehe ich die Daten wie folgt Dump:
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"
}
}
die Daten nicht mit Feuchtigkeit in Post
Objekt, auch Dump-Daten zeigen zwei Titel und zwei Text und Fieldset Namen, ich verstehe nicht. und wenn ich $this->postForm->setInputFilter(new PostFilter());
entfernen die Daten in Post
Objekt hydratisieren.
Warum die Validierung nicht funktioniert und Formularfehler nicht angezeigt, und warum Daten nicht in Post
Objekt hydratisiert?
Das Problem ist nicht Eingangsfilter wie so in Deklarieren becuz i bereits haben testen, ohne 'FieldSet mit' und die Validierung funktioniert, und im Dokument gibt es ein Beispiel ähnlich dem folgenden: 'class TaskFilter erweitert InputFilter'. Das Problem ist, wenn ich 'Fieldset' verwende, becuz, wenn ich' Form 'ohne' FieldSet 'verwende, funktioniert alles einwandfrei –
aktualisiert meine Antwort – Hooli