Skip to content

Commit

Permalink
FormBuilder Umbau
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanashi committed Oct 21, 2023
1 parent b38a345 commit e13d855
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 103 deletions.
40 changes: 1 addition & 39 deletions acptemplates/repositoryAdd.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,6 @@
</nav>
</header>

{include file='formError'}

{if $success|isset}
<p class="success">
{lang}packages.page.repositoryAdd.scuccess{/lang}
</p>
{/if}

<form method="post" action="{link application='packages' controller='RepositoryAdd'}{/link}">
<section class="section">
<dl{if $errorField == 'name'} class="formError"{/if}>
<dt><label for="name">{lang}packages.page.repositoryAdd.name{/lang}</label></dt>
<dd>
<input type="text" name="name" id="name" value="{$name}" required>
{if $errorField == 'name'}
<small class="innerError">
{if $errorType == 'tooShort'}
{lang}packages.page.repositoryAdd.name.error.tooShort{/lang}
{else if $errorType == 'noNumberOnStart'}
{lang}packages.page.repositoryAdd.name.error.noNumberOnStart{/lang}
{else if $errorType == 'wrongFormat'}
{lang}packages.page.repositoryAdd.name.error.wrongFormat{/lang}
{else if $errorType == 'nameTooLong'}
{lang}packages.page.repositoryAdd.name.error.nameTooLong{/lang}
{else if $errorType == 'alreadyUsed'}
{lang}packages.page.repositoryAdd.name.error.alreadyUsed{/lang}
{/if}
</small>
{/if}
<small>{lang}packages.page.repositoryAdd.name.description{/lang}</small>
</dd>
</dl>
</section>

<div class="formSubmit">
<input type="submit" value="{lang}wcf.global.button.submit{/lang}" accesskey="s">
{csrfToken}
</div>
</form>
{@$form->getHtml()}

{include file='footer'}
130 changes: 66 additions & 64 deletions files/lib/acp/form/RepositoryAddForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,77 @@

use packages\data\repository\RepositoryAction;
use packages\data\repository\RepositoryList;
use wcf\form\AbstractForm;
use wcf\system\exception\UserInputException;
use wcf\system\WCF;
use wcf\form\AbstractFormBuilderForm;
use wcf\system\form\builder\container\FormContainer;
use wcf\system\form\builder\field\TextFormField;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;

class RepositoryAddForm extends AbstractForm
class RepositoryAddForm extends AbstractFormBuilderForm
{
/**
* @inheritDoc
*/
public $objectActionClass = RepositoryAction::class;

/**
* @inheritDoc
*/
public $activeMenuItem = 'packages.acp.menu.link.package.repository.add';

protected $name;

public function readFormParameters()
{
parent::readFormParameters();

if (isset($_POST['name'])) {
$this->name = $_POST['name'];
}
}

public function validate()
{
parent::validate();

if (\strlen($this->name) < 2) {
throw new UserInputException('name', 'tooShort');
}
if (\preg_match('/^[0-9]+$/', \substr($this->name, 0, 1))) {
throw new UserInputException('name', 'noNumberOnStart');
}
if (!\preg_match('/^[a-z0-9]+$/', $this->name)) {
throw new UserInputException('name', 'wrongFormat');
}
if (\strlen($this->name) > 20) {
throw new UserInputException('name', 'nameTooLong');
}

$repositoryList = new RepositoryList();
$repositoryList->getConditionBuilder()->add('name = ?', [$this->name]);
$repositoryList->readObjects();
if (\count($repositoryList) > 0) {
throw new UserInputException('name', 'alreadyUsed');
}
}

public function save()
/**
* @inheritDoc
*/
protected function createForm()
{
parent::save();

$this->objectAction = new RepositoryAction([], 'create', ['data' => [
'name' => $this->name,
]]);
$this->objectAction->executeAction();

$this->saved();
}

public function saved()
{
parent::saved();

WCF::getTPL()->assign('success', true);
}

public function assignVariables()
{
parent::assignVariables();

WCF::getTPL()->assign([
'name' => $this->name,
parent::createForm();

$this->form->appendChildren([
FormContainer::create('data')
->appendChildren([
TextFormField::create('name')
->label('packages.page.repositoryAdd.name')
->description('packages.page.repositoryAdd.name.description')
->required()
->maximumLength(20)
->minimumLength(2)
->addValidator(
new FormFieldValidator('formatCheck', static function (TextFormField $formField) {
if (\preg_match('/^[0-9]+$/', \substr($formField->getValue(), 0, 1))) {
$formField->addValidationError(
new FormFieldValidationError(
'noNumberOnStart',
'packages.page.repositoryAdd.name.error.noNumberOnStart'
)
);

return;
}
if (!\preg_match('/^[a-z0-9]+$/', $formField->getValue())) {
$formField->addValidationError(
new FormFieldValidationError(
'wrongFormat',
'packages.page.repositoryAdd.name.error.wrongFormat'
)
);

return;
}

$repositoryList = new RepositoryList();
$repositoryList->getConditionBuilder()->add('name = ?', [$formField->getValue()]);
$repositoryList->readObjects();
if (\count($repositoryList) > 0) {
$formField->addValidationError(
new FormFieldValidationError(
'alreadyUsed',
'packages.page.repositoryAdd.name.error.alreadyUsed'
)
);
}
})
),
]),
]);
}
}

0 comments on commit e13d855

Please sign in to comment.