使单个表单字段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; }正如在文档中使控制器中的单个表单字段失效所描述的,现在我想使控制器中的字段失效,但执行方法仍然标记为有效:
$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方法?
I created a modelless form:
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; }As described in the documentation Invalidating Individual Form Fields from Controller now I want to invalidate a field from the controller but the execute method still mark as valid:
$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'));I am using the setErrors method incorrectly?
最满意答案
只需在execute()调用下移动setErrors()行,它将重置错误...
just move the setErrors() line below the execute() call, it will reset the errors...
更多推荐
发布评论