typo3 extbase:验证表单 [英] typo3 extbase: validate a form

查看:88
本文介绍了typo3 extbase:验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的订阅新闻通讯"表单:

I created a simple "subscribe to newsletter" form:

<f:form action="subscribe" method="post" name="newsletterform">
  <f:form.textfield id="name" name="name" required="true" />
  <f:form.textfield id="email" name="email" required="true"/>
  <button type="submit">Submit</button>
</f:form>

如您所见,这不是基于现有模型的表单,我没有兴趣将新闻订阅保存到数据库中(无论如何它们都将存储在其他地方).

as you can see, this is not a form that's based on an existing model, I have no interest in saving newslettersubscriptions to the database (they'll be stored somewhere else anyways).

现在在我的subscripeAction中,我想进行某种形式的验证.我想检查电子邮件是否真的是电子邮件地址,如果不是notEmpty等.是否可以使用typo3/extbase验证程序?如果是这样-怎么办?

Now in my subscripeAction I want to do some form validating. I want to check if the email is really an email address, if its notEmpty etc. Is there a way to use the typo3 / extbase Validators? If so - how?

推荐答案

您可以创建不是数据库模型的专用类,但可以扩展\TYPO3\CMS\Extbase\DomainObject\AbstractEntity并允许您通过Extbase映射该类:

You can create dedicated class that is not a database model, but extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity and allows you to map the class by Extbase:

例如文件:typo3conf/ext/yourext/Classes/Domain/Form/SubscribeForm.php

<?php

namespace Vendor\Extname\Domain\Form;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class SubscribeForm extends AbstractEntity {

    /**
     * @var string
     * @validate NotEmpty
     */
    protected $name;

    /**
     * @var string
     * @validate NotEmpty
     * @validate EmailAddress
     */
    protected $email;

    /** @return string */
    public function getName() {
        return $this->name;
    }

    /** @param string $name */
    public function setName($name) {
        $this->name = $name;
    }

    /** @return string */
    public function getEmail() {
        return $this->email;
    }

    /** @param string $email */
    public function setEmail($email) {
        $this->email = $email;
    }

}

使用此类,您可以像使用普通域模型一样工作,并且不会将其保存到任何内容-

With such class you can work as with common domain model and it will not be saved to anything - https://docs.typo3.org/typo3cms/ExtbaseFluidBook/9-CrosscuttingConcerns/2-validating-domain-objects.html

在您的控制器中,您只需执行以下两项操作即可:

in your controller you will just handle it with two actions:

/**
 * Displays the subscription form
 *
 * @param \Vendor\Extname\Domain\Form\SubscribeForm|NULL $subscribeForm
 * @dontvalidate $subscribeForm
 */
public function subscribeAction(\Vendor\Extname\Domain\Form\SubscribeForm $subscribeForm = NULL) {

}

/**
 * Handle the valid subscription form
 */
public function subscribeSaveAction(\Vendor\Extname\Domain\Form\SubscribeForm $subscribeForm) {
    // Handle the $subscribeForm
}

这篇关于typo3 extbase:验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆