使单个表单字段CakePHP无效(Invalidate individual form field CakePHP)

编程入门 行业动态 更新时间:2024-10-22 21:25:01
使单个表单字段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...

更多推荐

本文发布于:2023-07-06 07:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   CakePHP   Invalidate   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!