未添加到 table 的数据

Not added to data to table

我正在尝试验证表单。 我不明白为什么不进行表单验证。从逻辑上讲,它应该与符号的简单存在一起工作,但实际上,一个简单的测试:

if (! $form->isValid()) {
       echo 'not valid in check';
       return ['form' => $form];
    }

返回“echo 'not valid in check'” 问题是什么? 输入过滤器非常简单:

  public function getInputFilter()
{
    if ($this->inputFilter) {
        return $this->inputFilter;
    }

    $inputFilter = new InputFilter();

    $inputFilter->add([
        'name' => 'id',
        'required' => true,
        'filters' => [
            ['name' => ToInt::class],
        ],
    ]);

    $inputFilter->add([
        'name' => 'text',
        'required' => true,
        'filters' => [
            ['name' => StripTags::class],
            ['name' => StringTrim::class],
        ],
        'validators' => [
            [
                'name' => StringLength::class,
                'options' => [
                    'encoding' => 'UTF-8',
                    'min' => 1,
                    'max' => 1000,
                ],
            ],
        ],
    ]);

    $inputFilter->add([
        'name' => 'title',
        'required' => true,
        'filters' => [
            ['name' => StripTags::class],
            ['name' => StringTrim::class],
        ],
        'validators' => [
            [
                'name' => StringLength::class,
                'options' => [
                    'encoding' => 'UTF-8',
                    'min' => 1,
                    'max' => 100,
                ],
            ],
        ],
    ]);

    $this->inputFilter = $inputFilter;
    return $this->inputFilter;
}

表单?添加 post:

class PostForm extends Form
{
    public function __construct($name = null)
    {
        // We will ignore the name provided to the constructor
        parent::__construct('post');

        $this->add([
            'name' => 'id',
            'type' => 'hidden',
        ]);
        $this->add([
            'name' => 'title',
            'type' => 'text',
            'options' => [
                'label' => 'Title',
            ],
        ]);
        $this->add([
            'name' => 'text',
            'type' => 'textarea',
            'options' => [
                'label' => 'Text',
            ],
        ]);
        $this->add([
            'name' => 'submit',
            'type' => 'submit',
            'attributes' => [
                'value' => 'Go',
                'id'    => 'submitbutton',
            ],
        ]);

    }
}

如果需要,添加操作:

      public function addAction()
    {
        $form = new PostForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();

        if (! $request->isPost()) {
            return ['form' => $form];
        }
//        echo 'test';

        $post = new Post();
        $form->setInputFilter($post->getInputFilter());
       $form->setData($request->getPost());

    //    echo 'before valid';
       if (! $form->isValid()) {
           echo 'not valid in check';
           return ['form' => $form];
        }
        echo '\n';
       echo 'valid';
        $post->exchangeArray($form->getData());
        $this->table->savePost($post);

       return $this->redirect()->toRoute('post');


    }
public function addAction()
    {
     ---------------
       if (!$form->isValid()) {
           echo 'not valid in check';
           return ['form' => $form];
        }
   ---------

}

验证:

 public function getInputFilter()
    {
        if ($this->inputFilter) {
            return $this->inputFilter;
        }
    $inputFilter = new InputFilter();

    $inputFilter->add([
        'name' => 'id',
        'required' => true,
        'filters' => [
            ['name' => ToInt::class],
        ],
    ]);

    $inputFilter->add([
        'name' => 'text',
        'required' => true,
        'filters' => [
            ['name' => StripTags::class],
            ['name' => StringTrim::class],
        ],
        'validators' => [
            [
                'name' => StringLength::class,
                'options' => [
                    'encoding' => 'UTF-8',
                    'min' => 1,
                    'max' => 1000,
                ],
            ],
        ],
    ]);

    $inputFilter->add([
        'name' => 'title',
        'required' => true,
        'filters' => [
            ['name' => StripTags::class],
            ['name' => StringTrim::class],
        ],
        'validators' => [
            [
                'name' => StringLength::class,
                'options' => [
                    'encoding' => 'UTF-8',
                    'min' => 1,
                    'max' => 100,
                ],
            ],
        ],
    ]);

    $inputFilter->add([
        'name' => 'disc',
        'required' => true,
        'filters' => [
            ['name' => StripTags::class],
            ['name' => StringTrim::class],
        ],
        'validators' => [
            [
                'name' => StringLength::class,
                'options' => [
                    'encoding' => 'UTF-8',
                    'min' => 1,
                    'max' => 100,
                ],
            ],
        ],
    ]);

    $this->inputFilter = $inputFilter;
    return $this->inputFilter;
}