使单个表单字段 CakePHP 无效

Invalidate individual form field CakePHP

我创建了一个无模型表单:

protected function _buildSchema(Schema $schema)
{
  return $schema->addField('name', ['type' => 'string'])
                ->addField('line1', ['type' => 'string'])
                ->addField('city', ['type' => 'string'])
                ->addField('state', ['type' => 'string'])
                ->addField('country', ['type' => 'string'])
                ->addField('postal_code', ['type' => 'string'])
                ->addField('phone', ['type' => 'string'])
                ->addField('email', ['type' => 'string']);
}

/**
 * Form validation builder
 *
 * @param \Cake\Validation\Validator $validator to use against the form
 * @return \Cake\Validation\Validator
 */
protected function _buildValidator(Validator $validator)
{
  return $validator->notEmpty('name')
                   ->notEmpty('line1')
                   ->notEmpty('city')
                   ->notEmpty('state')
                   ->notEmpty('country')
                   ->notEmpty('postal_code')
                   ->notEmpty('email')
                   ->add('email', 'valid', ['rule' => 'email']);
}


protected function _execute(array $data)
{
    return true;
}

public function setErrors($errors)
{
    $this->_errors = $errors;
}

如文档中所述Invalidating Individual Form Fields from Controller 现在我想使控制器中的字段无效,但执行方法仍标记为有效:

$address = new AddressForm();

  if ($this->request->is('post')) {

$address->setErrors(["line1" => ["_custom" => 'Inavlid field']]);
      if ($address->execute($this->request->getData())) {
          $this->Flash->success('Valid address');
      } else {
          $this->Flash->error('There was a problem submitting your form.');
      }
    }

$this->set(compact('address'));

我没有正确使用 setErrors 方法?

只需将 setErrors() 行移到 execute() 调用下方,它将重置错误...