CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/pypi-flask-wtf

tessl install tessl/pypi-flask-wtf@1.2.0

Form rendering, validation, and CSRF protection for Flask with WTForms.

Agent Success

Agent success rate when using this tile

72%

Improvement

Agent success rate improvement when using this tile compared to baseline

0.91x

Baseline

Agent success rate without this tile

79%

task.mdevals/scenario-6/

Multi-language Form System

Build a Flask application that provides form validation messages in multiple languages using proper internationalization support.

Requirements

Create a user registration form with the following fields:

  • Username (required, minimum 3 characters)
  • Email (required, valid email format)
  • Age (required, integer, minimum value 18)

The form should display validation error messages in the user's preferred language. Support at least two languages: English and Spanish.

When validation fails, error messages should be translated according to the selected locale. For example:

  • "This field is required" should appear as "Este campo es obligatorio" in Spanish
  • "Field must be at least 3 characters long" should appear as "El campo debe tener al menos 3 caracteres" in Spanish

The application should:

  • Configure internationalization support to enable automatic translation of form validation messages
  • Accept a locale parameter (e.g., via query string ?locale=es) to determine which language to use
  • Return validation errors in the appropriate language based on the locale
  • Handle locale switching per-request

Test Cases

  • Given a form submission with an empty username field and locale "en", the validation error contains "This field is required" in English @test
  • Given a form submission with an empty username field and locale "es", the validation error contains the Spanish translation of "This field is required" @test
  • Given a form submission with username "ab" (too short) and locale "en", the validation error mentions the minimum length requirement in English @test
  • Given a form submission with an invalid email "notanemail" and locale "es", the validation error is shown in Spanish @test

Implementation

@generates

API

from flask import Flask, request, jsonify
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired, Email, Length, NumberRange

def create_app():
    """
    Create and configure the Flask application with i18n support.

    Returns:
        Flask: Configured Flask application
    """
    pass

class RegistrationForm(FlaskForm):
    """
    User registration form with validation.
    """
    username = StringField('Username', validators=[DataRequired(), Length(min=3)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    age = IntegerField('Age', validators=[DataRequired(), NumberRange(min=18)])

@app.route('/register', methods=['GET', 'POST'])
def register():
    """
    Handle user registration with localized validation messages.

    Accepts locale parameter to determine language (e.g., ?locale=es or form data).
    Returns JSON with success status and any validation errors in the selected language.

    Returns:
        JSON response with validation results
    """
    pass

Dependencies { .dependencies }

Flask-WTF { .dependency }

Provides form integration with Flask including internationalization support for validation messages.

Flask-Babel { .dependency }

Provides internationalization and localization support for Flask applications, enabling translation of text including form validation messages.

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/flask-wtf@1.2.x
tile.json