refactor: [WIP] Improve form building.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2022-06-11 08:53:50 +02:00
parent 1bc977dfc8
commit 54bedda0b2
31 changed files with 63 additions and 49 deletions

View File

@@ -72,7 +72,7 @@ final class SaveCopyController extends AccountControllerBase
public function saveCopyAction(): ?bool
{
try {
$this->accountForm->validate(ActionsInterface::ACCOUNT_CREATE);
$this->accountForm->validateFor(ActionsInterface::ACCOUNT_CREATE);
$accountId = $this->accountService->create($this->accountForm->getItemData());

View File

@@ -73,7 +73,7 @@ final class SaveCreateController extends AccountControllerBase
public function saveCreateAction(): ?bool
{
try {
$this->accountForm->validate(ActionsInterface::ACCOUNT_CREATE);
$this->accountForm->validateFor(ActionsInterface::ACCOUNT_CREATE);
$accountId = $this->accountService->create($this->accountForm->getItemData());

View File

@@ -76,8 +76,7 @@ final class SaveEditController extends AccountControllerBase
public function saveEditAction(int $id): ?bool
{
try {
$this->accountForm->setItemId($id);
$this->accountForm->validate(ActionsInterface::ACCOUNT_EDIT);
$this->accountForm->validateFor(ActionsInterface::ACCOUNT_EDIT, $id);
$itemData = $this->accountForm->getItemData();

View File

@@ -72,8 +72,7 @@ final class SaveEditPassController extends AccountControllerBase
public function saveEditPassAction(int $id): bool
{
try {
$this->accountForm->setItemId($id);
$this->accountForm->validate(ActionsInterface::ACCOUNT_EDIT_PASS);
$this->accountForm->validateFor(ActionsInterface::ACCOUNT_EDIT_PASS, $id);
$this->accountService->editPassword($this->accountForm->getItemData());

View File

@@ -79,7 +79,7 @@ final class SaveBulkEditController extends ControllerBase
public function saveBulkEditAction(): bool
{
try {
$this->accountForm->validate(ActionsInterface::ACCOUNTMGR_BULK_EDIT);
$this->accountForm->validateFor(ActionsInterface::ACCOUNTMGR_BULK_EDIT, null);
$request = new AccountBulkRequest(
Util::itemsIdAdapter($this->request->analyzeString('itemsId')),

View File

@@ -51,7 +51,7 @@ final class SaveCreateController extends AuthTokenSaveBase
);
}
$this->form->validate(ActionsInterface::AUTHTOKEN_CREATE);
$this->form->validateFor(ActionsInterface::AUTHTOKEN_CREATE);
$id = $this->authTokenService->create($this->form->getItemData());

View File

@@ -56,8 +56,7 @@ final class SaveEditController extends AuthTokenSaveBase
);
}
$this->form->setItemId($id);
$this->form->validate(ActionsInterface::AUTHTOKEN_EDIT);
$this->form->validateFor(ActionsInterface::AUTHTOKEN_EDIT, $id);
if ($this->form->isRefresh()) {
$this->authTokenService->refreshAndUpdate($this->form->getItemData());

View File

@@ -332,7 +332,7 @@ final class CategoryController extends ControllerBase implements CrudControllerI
}
$form = new CategoryForm($this->dic);
$form->validate(ActionsInterface::CATEGORY_CREATE);
$form->validateFor(ActionsInterface::CATEGORY_CREATE, null);
$itemData = $form->getItemData();
@@ -393,7 +393,7 @@ final class CategoryController extends ControllerBase implements CrudControllerI
}
$form = new CategoryForm($this->dic, $id);
$form->validate(Acl::CATEGORY_EDIT);
$form->validateFor(Acl::CATEGORY_EDIT, null);
$itemData = $form->getItemData();

View File

@@ -330,7 +330,7 @@ final class ClientController extends ControllerBase implements CrudControllerInt
}
$form = new ClientForm($this->dic);
$form->validate(ActionsInterface::CLIENT_CREATE);
$form->validateFor(ActionsInterface::CLIENT_CREATE, null);
$itemData = $form->getItemData();
@@ -391,7 +391,7 @@ final class ClientController extends ControllerBase implements CrudControllerInt
}
$form = new ClientForm($this->dic, $id);
$form->validate(ActionsInterface::CLIENT_EDIT);
$form->validateFor(ActionsInterface::CLIENT_EDIT, null);
$this->clientService->update($form->getItemData());

View File

@@ -318,7 +318,7 @@ final class CustomFieldController extends ControllerBase implements CrudControll
}
$form = new CustomFieldDefForm($this->dic);
$form->validate(ActionsInterface::CUSTOMFIELD_CREATE);
$form->validateFor(ActionsInterface::CUSTOMFIELD_CREATE, null);
$itemData = $form->getItemData();
@@ -373,7 +373,7 @@ final class CustomFieldController extends ControllerBase implements CrudControll
}
$form = new CustomFieldDefForm($this->dic, $id);
$form->validate(ActionsInterface::CUSTOMFIELD_EDIT);
$form->validateFor(ActionsInterface::CUSTOMFIELD_EDIT, null);
$itemData = $form->getItemData();

View File

@@ -394,7 +394,7 @@ final class ItemPresetController extends ControllerBase implements CrudControlle
}
$form = new ItemsPresetForm($this->dic);
$form->validate(ActionsInterface::ITEMPRESET_CREATE);
$form->validateFor(ActionsInterface::ITEMPRESET_CREATE, null);
$itemData = $form->getItemData();
@@ -450,7 +450,7 @@ final class ItemPresetController extends ControllerBase implements CrudControlle
}
$form = new ItemsPresetForm($this->dic, $id);
$form->validate(Acl::ITEMPRESET_EDIT);
$form->validateFor(Acl::ITEMPRESET_EDIT, null);
$itemData = $form->getItemData();

View File

@@ -428,7 +428,7 @@ final class NotificationController extends ControllerBase implements CrudControl
}
$form = new NotificationForm($this->dic);
$form->validate(ActionsInterface::NOTIFICATION_CREATE);
$form->validateFor(ActionsInterface::NOTIFICATION_CREATE, null);
$this->notificationService->create($form->getItemData());
@@ -478,7 +478,7 @@ final class NotificationController extends ControllerBase implements CrudControl
}
$form = new NotificationForm($this->dic, $id);
$form->validate(ActionsInterface::NOTIFICATION_EDIT);
$form->validateFor(ActionsInterface::NOTIFICATION_EDIT, null);
$this->notificationService->update($form->getItemData());

View File

@@ -384,7 +384,7 @@ final class PublicLinkController extends ControllerBase implements CrudControlle
}
$form = new PublicLinkForm($this->dic);
$form->validate(ActionsInterface::PUBLICLINK_CREATE);
$form->validateFor(ActionsInterface::PUBLICLINK_CREATE, null);
$this->publicLinkService->create($form->getItemData());

View File

@@ -313,7 +313,7 @@ final class TagController extends ControllerBase implements CrudControllerInterf
}
$form = new TagForm($this->dic);
$form->validate(ActionsInterface::TAG_CREATE);
$form->validateFor(ActionsInterface::TAG_CREATE, null);
$this->tagService->create($form->getItemData());
@@ -361,7 +361,7 @@ final class TagController extends ControllerBase implements CrudControllerInterf
}
$form = new TagForm($this->dic, $id);
$form->validate(ActionsInterface::TAG_EDIT);
$form->validateFor(ActionsInterface::TAG_EDIT, null);
$this->tagService->update($form->getItemData());

View File

@@ -433,7 +433,7 @@ final class UserController extends ControllerBase implements CrudControllerInter
}
$form = new UserForm($this->dic);
$form->validate(Acl::USER_CREATE);
$form->validateFor(Acl::USER_CREATE, null);
$itemData = $form->getItemData();
@@ -523,7 +523,7 @@ final class UserController extends ControllerBase implements CrudControllerInter
}
$form = new UserForm($this->dic, $id);
$form->validate(ActionsInterface::USER_EDIT);
$form->validateFor(ActionsInterface::USER_EDIT, null);
$itemData = $form->getItemData();
@@ -587,7 +587,7 @@ final class UserController extends ControllerBase implements CrudControllerInter
}
$form = new UserForm($this->dic, $id);
$form->validate(ActionsInterface::USER_EDIT_PASS);
$form->validateFor(ActionsInterface::USER_EDIT_PASS, null);
$itemData = $form->getItemData();

View File

@@ -353,7 +353,7 @@ final class UserGroupController extends ControllerBase implements CrudController
}
$form = new UserGroupForm($this->dic);
$form->validate(ActionsInterface::GROUP_CREATE);
$form->validateFor(ActionsInterface::GROUP_CREATE, null);
$groupData = $form->getItemData();
@@ -414,7 +414,7 @@ final class UserGroupController extends ControllerBase implements CrudController
}
$form = new UserGroupForm($this->dic, $id);
$form->validate(ActionsInterface::GROUP_EDIT);
$form->validateFor(ActionsInterface::GROUP_EDIT, null);
$groupData = $form->getItemData();

View File

@@ -350,7 +350,7 @@ final class UserProfileController extends ControllerBase implements CrudControll
}
$form = new UserProfileForm($this->dic);
$form->validate(ActionsInterface::PROFILE_CREATE);
$form->validateFor(ActionsInterface::PROFILE_CREATE, null);
$profileData = $form->getItemData();
@@ -411,7 +411,7 @@ final class UserProfileController extends ControllerBase implements CrudControll
}
$form = new UserProfileForm($this->dic, $id);
$form->validate(ActionsInterface::PROFILE_EDIT);
$form->validateFor(ActionsInterface::PROFILE_EDIT, null);
$profileData = $form->getItemData();

View File

@@ -60,6 +60,7 @@ final class AccountForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return AccountForm|FormInterface
* @throws ValidationException
@@ -67,7 +68,7 @@ final class AccountForm extends FormBase implements FormInterface
* @throws NoSuchPropertyException
* @throws QueryException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::ACCOUNT_EDIT_PASS:

View File

@@ -37,18 +37,23 @@ use SP\Domain\Auth\Services\AuthTokenService;
final class AuthTokenForm extends FormBase implements FormInterface
{
protected ?AuthTokenData $authTokenData = null;
protected bool $refresh = false;
protected bool $refresh = false;
/**
* Validar el formulario
*
* @param int $action
* @param int $action
* @param int|null $id
*
* @return AuthTokenForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
if ($id !== null) {
$this->itemId = $id;
}
switch ($action) {
case ActionsInterface::AUTHTOKEN_CREATE:
case ActionsInterface::AUTHTOKEN_EDIT:

View File

@@ -41,11 +41,12 @@ final class CategoryForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return CategoryForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::CATEGORY_CREATE:

View File

@@ -41,11 +41,12 @@ final class ClientForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return ClientForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::CLIENT_CREATE:

View File

@@ -41,11 +41,12 @@ final class CustomFieldDefForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return CustomFieldDefForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::CUSTOMFIELD_CREATE:

View File

@@ -64,11 +64,11 @@ abstract class FormBase
}
/**
* @param int $itemId
* @return int|null
*/
public function setItemId(int $itemId): void
public function getItemId(): ?int
{
$this->itemId = $itemId;
return $this->itemId;
}
/**

View File

@@ -36,12 +36,13 @@ interface FormInterface
/**
* Validar el formulario
*
* @param int $action
* @param int $action
* @param int|null $id
*
* @return FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface;
public function validateFor(int $action, ?int $id = null): FormInterface;
/**
* @return mixed

View File

@@ -49,11 +49,12 @@ final class ItemsPresetForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return ItemsPresetForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::ITEMPRESET_CREATE:

View File

@@ -42,11 +42,12 @@ final class NotificationForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return NotificationForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::NOTIFICATION_CREATE:

View File

@@ -42,11 +42,12 @@ final class PublicLinkForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return PublicLinkForm
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::PUBLICLINK_CREATE:

View File

@@ -41,11 +41,12 @@ final class TagForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return TagForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::TAG_CREATE:

View File

@@ -42,11 +42,12 @@ final class UserForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return UserForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::USER_CREATE:

View File

@@ -41,11 +41,12 @@ final class UserGroupForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return UserGroupForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::GROUP_CREATE:

View File

@@ -42,11 +42,12 @@ final class UserProfileForm extends FormBase implements FormInterface
* Validar el formulario
*
* @param int $action
* @param int|null $id
*
* @return UserProfileForm|FormInterface
* @throws ValidationException
*/
public function validate(int $action): FormInterface
public function validateFor(int $action, ?int $id = null): FormInterface
{
switch ($action) {
case ActionsInterface::PROFILE_CREATE: