frontend mail rule validation

Display non-field validation errors, hide action param field if not needed
This commit is contained in:
Michael Shamoon 2022-11-28 15:51:39 -08:00
parent 7ace66d7fd
commit 35ca2195fe
3 changed files with 18 additions and 1 deletions

View File

@ -22,7 +22,7 @@
</div> </div>
<div class="col"> <div class="col">
<app-input-select i18n-title title="Action" [items]="actionOptions" formControlName="action" i18n-hint hint="Action is only performed when documents are consumed from the mail. Mails without attachments remain entirely untouched."></app-input-select> <app-input-select i18n-title title="Action" [items]="actionOptions" formControlName="action" i18n-hint hint="Action is only performed when documents are consumed from the mail. Mails without attachments remain entirely untouched."></app-input-select>
<app-input-text i18n-title title="Action parameter" formControlName="action_parameter" [error]="error?.action_parameter"></app-input-text> <app-input-text i18n-title title="Action parameter" *ngIf="showActionParamField" formControlName="action_parameter" [error]="error?.action_parameter"></app-input-text>
<app-input-select i18n-title title="Assign title from" [items]="metadataTitleOptions" formControlName="assign_title_from"></app-input-select> <app-input-select i18n-title title="Assign title from" [items]="metadataTitleOptions" formControlName="assign_title_from"></app-input-select>
<app-input-tags [allowCreate]="false" formControlName="assign_tags"></app-input-tags> <app-input-tags [allowCreate]="false" formControlName="assign_tags"></app-input-tags>
<app-input-select i18n-title title="Assign document type" [items]="documentTypes" [allowNull]="true" formControlName="assign_document_type"></app-input-select> <app-input-select i18n-title title="Assign document type" [items]="documentTypes" [allowNull]="true" formControlName="assign_document_type"></app-input-select>
@ -32,6 +32,7 @@
</div> </div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<span class="text-danger" *ngIf="error?.non_field_errors"><ng-container i18n>Error</ng-container>: {{error.non_field_errors}}</span>
<button type="button" class="btn btn-outline-secondary" (click)="cancel()" i18n [disabled]="networkActive">Cancel</button> <button type="button" class="btn btn-outline-secondary" (click)="cancel()" i18n [disabled]="networkActive">Cancel</button>
<button type="submit" class="btn btn-primary" i18n [disabled]="networkActive">Save</button> <button type="submit" class="btn btn-primary" i18n [disabled]="networkActive">Save</button>
</div> </div>

View File

@ -155,6 +155,13 @@ export class MailRuleEditDialogComponent extends EditDialogComponent<PaperlessMa
) )
} }
get showActionParamField(): boolean {
return (
this.objectForm?.get('action')?.value == MailAction.Move ||
this.objectForm?.get('action')?.value == MailAction.Tag
)
}
get attachmentTypeOptions() { get attachmentTypeOptions() {
return ATTACHMENT_TYPE_OPTIONS return ATTACHMENT_TYPE_OPTIONS
} }

View File

@ -785,3 +785,12 @@ class MailRuleSerializer(serializers.ModelSerializer):
if assign_tags: if assign_tags:
mail_rule.assign_tags.set(assign_tags) mail_rule.assign_tags.set(assign_tags)
return mail_rule return mail_rule
def validate(self, attrs):
if (
attrs["action"] == MailRule.MailAction.TAG
or attrs["action"] == MailRule.MailAction.MOVE
) and attrs["action_parameter"] is None:
raise serializers.ValidationError("An action parameter is required.")
return attrs