typo3 extbase: validate a form

我们两清 提交于 2019-12-21 20:39:00

问题


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).

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?


回答1:


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:

for an example file: 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
}



回答2:


yes you can use fluid validator, but i would like to suggest you to use ValidationEngine

<input type="email" name="email" id="email" data-validation-engine="validate[required,custom[email]]"
data-errormessage-value-missing="Email is required!" 
data-errormessage-custom-error="Let me give you a hint: someone@nowhere.com" 
data-errormessage="This is the fall-back error message."/>



回答3:


If you want to validate the mail address in a controller action you can use the EmailAddressValidator class.

Here is a reference: https://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_validation_1_1_validator_1_1_email_address_validator.html#a72063d2e922edda5321b1970297eb0c3

I think a better way is to validate the form on the client side. The user can directly see what is wrong.

For HTML5 form validation look here: https://www.sitepoint.com/client-side-form-validation-html5/

http://www.w3schools.com/html/html_form_attributes.asp



来源:https://stackoverflow.com/questions/35267734/typo3-extbase-validate-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!