validation - typo3 extbase: validate a form -
i created simple "subscribe 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 can see, not form that's based on existing model, have no interest in saving newslettersubscriptions database (they'll stored somewhere else anyways).
now in subscripeaction want form validating. want check if email email address, if notempty etc. there way use typo3 / extbase validators? if - how?
you can create dedicated class not database model, extends \typo3\cms\extbase\domainobject\abstractentity
, allows map class extbase:
for 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 can work common domain model , not saved - https://docs.typo3.org/typo3cms/extbasefluidbook/9-crosscuttingconcerns/2-validating-domain-objects.html
in controller handle 2 actions:
/** * displays subscription form * * @param \vendor\extname\domain\form\subscribeform|null $subscribeform * @dontvalidate $subscribeform */ public function subscribeaction(\vendor\extname\domain\form\subscribeform $subscribeform = null) { } /** * handle valid subscription form */ public function subscribesaveaction(\vendor\extname\domain\form\subscribeform $subscribeform) { // handle $subscribeform }
Comments
Post a Comment