Course Overview
Locked video preview

Paid video

Please purchase the course to watch.

Creating and managing articles

Validating new articles

Here is a summary of the video transcript:

  • Route and Controller Setup
    A new POST route is created for article creation under an admin prefix. The controller defines a payload struct matching form inputs and binds incoming request data to it using the framework’s request binding utilities.

  • Article Creation Logic
    The controller maps the validated payload to a model-layer article struct and calls a create function with the database connection. After successful creation, the user is redirected using an HTTP 303 status code.

  • Base Validation with go-playground/validator
    A validator instance is initialized and configured to validate struct fields using tags. Standard rules such as required, min, max, gt, and omitempty are applied to enforce constraints without hitting the database.

  • Custom Publish Validation Rule
    A custom struct-level validation ensures that when an article is marked as published, both excerpt and content must be non-empty. This rule is registered with the validator to prevent publishing incomplete articles.

  • Domain Error Handling
    A custom domain validation error is defined and combined with validation errors using error chaining. The controller inspects the error tree to distinguish validation failures from other types of errors.

  • UI Feedback Integration
    A reusable fieldProp structure is introduced to pass both field values and error messages to the view layer. Form components are updated to display validation errors dynamically beneath input fields.

  • Mapping Validation Errors to Form Fields
    A helper function converts validator errors into a map of field properties. It preserves user input, associates errors with specific fields, and generates user-friendly error messages for display.

  • End-to-End Validation Flow
    On form submission, validation errors are extracted and mapped back to the UI instead of returning a generic server error. Valid submissions result in correctly saved draft or published articles, ensuring consistent data integrity.