As the first example, the pre-WoltLab Suite Core 5.2 versions of the forms to add and edit persons from the first part of the tutorial series will be updated to the new form builder API.
This form is the perfect first examples as it is very simple with only two text fields whose only restriction is that they have to be filled out and that their values may not be longer than 255 characters each.
As a reminder, here are the two relevant PHP files and the relevant template file:
<?phpnamespacewcf\acp\form;usewcf\data\person\PersonAction;usewcf\form\AbstractForm;usewcf\system\exception\UserInputException;usewcf\system\WCF;usewcf\util\StringUtil;/** * Shows the form to create a new person. * * @author Matthias Schmidt * @copyright 2001-2019 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Acp\Form */classPersonAddFormextendsAbstractForm{/** * @inheritDoc */public$activeMenuItem='wcf.acp.menu.link.person.add';/** * first name of the person * @var string */public$firstName='';/** * last name of the person * @var string */public$lastName='';/** * @inheritDoc */public$neededPermissions=['admin.content.canManagePeople'];/** * @inheritDoc */publicfunctionassignVariables(){parent::assignVariables();WCF::getTPL()->assign(['action'=>'add','firstName'=>$this->firstName,'lastName'=>$this->lastName]);}/** * @inheritDoc */publicfunctionreadFormParameters(){parent::readFormParameters();if(isset($_POST['firstName']))$this->firstName=StringUtil::trim($_POST['firstName']);if(isset($_POST['lastName']))$this->lastName=StringUtil::trim($_POST['lastName']);}/** * @inheritDoc */publicfunctionsave(){parent::save();$this->objectAction=newPersonAction([],'create',['data'=>array_merge($this->additionalFields,['firstName'=>$this->firstName,'lastName'=>$this->lastName])]);$this->objectAction->executeAction();$this->saved();// reset values$this->firstName='';$this->lastName='';// show success messageWCF::getTPL()->assign('success',true);}/** * @inheritDoc */publicfunctionvalidate(){parent::validate();// validate first nameif(empty($this->firstName)){thrownewUserInputException('firstName');}if(mb_strlen($this->firstName)>255){thrownewUserInputException('firstName','tooLong');}// validate last nameif(empty($this->lastName)){thrownewUserInputException('lastName');}if(mb_strlen($this->lastName)>255){thrownewUserInputException('lastName','tooLong');}}}
<?phpnamespacewcf\acp\form;usewcf\data\person\Person;usewcf\data\person\PersonAction;usewcf\form\AbstractForm;usewcf\system\exception\IllegalLinkException;usewcf\system\WCF;/** * Shows the form to edit an existing person. * * @author Matthias Schmidt * @copyright 2001-2019 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Acp\Form */classPersonEditFormextendsPersonAddForm{/** * @inheritDoc */public$activeMenuItem='wcf.acp.menu.link.person';/** * edited person object * @var Person */public$person=null;/** * id of the edited person * @var integer */public$personID=0;/** * @inheritDoc */publicfunctionassignVariables(){parent::assignVariables();WCF::getTPL()->assign(['action'=>'edit','person'=>$this->person]);}/** * @inheritDoc */publicfunctionreadData(){parent::readData();if(empty($_POST)){$this->firstName=$this->person->firstName;$this->lastName=$this->person->lastName;}}/** * @inheritDoc */publicfunctionreadParameters(){parent::readParameters();if(isset($_REQUEST['id']))$this->personID=intval($_REQUEST['id']);$this->person=newPerson($this->personID);if(!$this->person->personID){thrownewIllegalLinkException();}}/** * @inheritDoc */publicfunctionsave(){AbstractForm::save();$this->objectAction=newPersonAction([$this->person],'update',['data'=>array_merge($this->additionalFields,['firstName'=>$this->firstName,'lastName'=>$this->lastName])]);$this->objectAction->executeAction();$this->saved();// show success messageWCF::getTPL()->assign('success',true);}}
<?phpnamespacewcf\acp\form;usewcf\data\person\PersonAction;usewcf\form\AbstractFormBuilderForm;usewcf\system\form\builder\container\FormContainer;usewcf\system\form\builder\field\TextFormField;/** * Shows the form to create a new person. * * @author Matthias Schmidt * @copyright 2001-2019 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Acp\Form */classPersonAddFormextendsAbstractFormBuilderForm{/** * @inheritDoc */public$activeMenuItem='wcf.acp.menu.link.person.add';/** * @inheritDoc */public$formAction='create';/** * @inheritDoc */public$neededPermissions=['admin.content.canManagePeople'];/** * @inheritDoc */public$objectActionClass=PersonAction::class;/** * @inheritDoc */protectedfunctioncreateForm(){parent::createForm();$dataContainer=FormContainer::create('data')->appendChildren([TextFormField::create('firstName')->label('wcf.person.firstName')->required()->maximumLength(255),TextFormField::create('lastName')->label('wcf.person.lastName')->required()->maximumLength(255)]);$this->form->appendChild($dataContainer);}}
But, as you can see, the number of lines almost decreased by half.
All changes are due to extending AbstractFormBuilderForm:
$formAction is added and set to create as the form is used to create a new person.
In the edit form, $formAction has not to be set explicitly as it is done automatically if a $formObject is set.
$objectActionClass is set to PersonAction::class and is the class name of the used AbstractForm::$objectAction object to create and update the Person object.
AbstractFormBuilderForm::createForm() is overridden and the form contents are added:
a form container representing the div.section element from the old version and the two form fields with the same ids and labels as before.
The contents of the old validate() method is put into two method calls:
required() to ensure that the form is filled out and maximumLength(255) to ensure that the names are not longer than 255 characters.