Skip to content

Commit

Permalink
Demonstrate form validation in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehor Komarov committed Feb 21, 2024
1 parent 15a9243 commit 9d2a2bf
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions examples/sqla/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
row_action,
)
from starlette_admin.contrib.sqla import ModelView
from starlette_admin.exceptions import ActionFailed, FormValidationError
from starlette_admin.exceptions import FormValidationError

from .models import Post, Tag, User
from .models import Post, User

AVAILABLE_USER_TYPES = [
("admin", "Admin"),
Expand Down Expand Up @@ -68,19 +68,24 @@ async def validate(self, request: Request, data: Dict[str, Any]) -> None:
raise FormValidationError(errors)
return await super().validate(request, data)

async def validate_add_tag(self, request: Request, data: Dict[str, Any]):
errors: Dict[str, str] = {}
if data["tag"] is None:
errors["tag"] = "You must specify a tag"
if len(errors) > 0:
raise FormValidationError(errors)

@action(
name="add_tag",
text="Add a tag for selected posts",
icon_class="fas fa-tag",
confirmation="Are you sure you want to add a tag to all of these posts?",
form_fields=[HasOne("tag", identity="tag")],
)
async def add_tag(self, request: Request, pks: List[Any]):
form = await request.form()
tag_pk = form["tag"]
tag = await TagView(Tag).find_by_pk(request, tag_pk)
if not tag:
raise ActionFailed("Tag is missing")
async def add_tag(self, request: Request, pks: List[Any], data: Dict):
await self.validate_add_tag(request, data)

tag = data["tag"]
posts = await self.find_by_pks(request, pks)
for post in posts:
post.tags.append(tag)
Expand All @@ -97,9 +102,8 @@ async def add_tag(self, request: Request, pks: List[Any]):
EnumField("status", choices=AVAILABLE_POST_STATUSES, select2=False)
],
)
async def approve(self, request: Request, pk: Any):
form = await request.form()
status = form["status"]
async def approve(self, request: Request, pk: Any, data: Dict):
status = data["status"]
post = await self.find_by_pk(request, pk)
post.status = status
request.state.session.commit()
Expand Down

0 comments on commit 9d2a2bf

Please sign in to comment.