Skip to content

Add form field macro #6393 #18423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
705 changes: 316 additions & 389 deletions warehouse/locale/messages.pot

Large diffs are not rendered by default.

63 changes: 20 additions & 43 deletions warehouse/templates/accounts/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,28 @@ <h1 class="page-title">{% trans title=title %}Log in to {{ title }}{% endtrans %
</ul>
{% endif %}
<div class="form-group">
<label for="username" class="form-group__label">
{% trans %}Username{% endtrans %}
{% if form.username.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ form.username(placeholder=gettext("Your username") ,
autocapitalize="off",
autocomplete="username",
spellcheck="false",
required="required",
class_="form-group__field",
aria_describedby="username-errors",
) }}
<div id="username-errors">
{% if form.username.errors %}
<ul class="form-errors" role="alert">
{% for error in form.username.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
</div>
{{ input_field(
instance="username",
user_friendly_name="Username",
form_instance=form.username,
placeholder="Your username",
autocomplete="username",
required="required",
autocapitalize="off",
spellcheck="false"
)}}
</div>
<div data-controller="password" class="form-group">
<div>
<label for="password" class="form-group__label">
{% trans %}Password{% endtrans %}
{% if form.password.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
</div>
{{ form.password(placeholder=gettext("Your password") ,
required="required",
class_="form-group__field",
autocomplete="current-password",
spellcheck="false", data_password_target="password",
aria_describedby="password-errors",
) }}
<div id="password-errors">
{% if form.password.errors %}
<ul class="form-errors" role="alert">
{% for error in form.password.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
</div>
{{ input_field(
instance="password",
user_friendly_name="Password",
form_instance=form.password,
placeholder="Your password",
autocomplete="current-password",
data_password_target="password",
required="required",
spellcheck="false"
)}}
<div class="form-group">
<div class="split-layout margin-top--large margin-bottom--large">
<div>
Expand Down
146 changes: 48 additions & 98 deletions warehouse/templates/accounts/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,48 +63,26 @@ <h1 class="page-title">{% trans title=title %}Create an account on {{ title }}{%
</ul>
{% endif %}
<div class="form-group">
<label for="full_name" class="form-group__label">
{% trans %}Name{% endtrans %}
{% if form.full_name.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ form.full_name(placeholder=gettext("Your name") ,
autocomplete="name",
autocapitalize="off",
spellcheck="false",
class_="form-group__field",
aria_describedby="name-errors",
) }}
<div id="name-errors">
{% if form.full_name.errors %}
<ul class="form-errors" role="alert">
{% for error in form.full_name.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
</div>
{{ input_field(
instance="name",
user_friendly_name="Name",
form_instance=form.full_name,
placeholder="Your name",
autocomplete="name",
autocapitalize="off",
spellcheck="false"
)}}
</div>
<div class="form-group">
<label for="email" class="form-group__label">
{% trans %}Email address{% endtrans %}
{% if form.email.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ form.email(placeholder=gettext("Your email address") ,
autocomplete="email",
spellcheck="false",
required="required",
class_="form-group__field",
aria_describedby="email-errors",
) }}
<div id="email-errors">
{% if form.email.errors %}
<ul class="form-errors" role="alert">
{% for error in form.email.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
</div>
{{ input_field(
instance="email",
user_friendly_name="Email address",
form_instance=form.email,
placeholder="Your email address",
autocomplete="email",
spellcheck="false",
required="required"
)}}
</div>
{# The following is a honeypot field and is hidden from the user #}
<div class="form-group confirm-form">
Expand All @@ -113,37 +91,23 @@ <h1 class="page-title">{% trans title=title %}Create an account on {{ title }}{%
aria_hidden='true',) }}
</div>
<div class="form-group">
<label for="username" class="form-group__label">
{% trans %}Username{% endtrans %}
{% if form.username.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ form.username(placeholder=gettext("Select a username") ,
autocapitalize="off",
autocomplete="username",
spellcheck="false",
required="required",
class_="form-group__field",
aria_describedby="username-errors",
) }}
<div id="username-errors">
{% if form.username.errors %}
<ul class="form-errors" role="alert">
{% for error in form.username.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
</div>
{{ input_field(
instance="username",
user_friendly_name="Username",
form_instance=form.username,
placeholder="Select a username",
autocomplete="username",
autocapitalize="off",
required="required"
)}}
</div>
<div>
<div class="form-group">
<div class="split-layout">
<label for="new_password" class="form-group__label">
{% trans %}Password{% endtrans %}
{% if form.new_password.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ input_field_label(
user_friendly_name="Password",
form_instance=form.new_password
)}}
<label for="show-password">
<input data-action="change->password#togglePasswords"
data-password-target="showPassword"
Expand All @@ -164,40 +128,26 @@ <h1 class="page-title">{% trans title=title %}Create an account on {{ title }}{%
data_action="input->password-match#checkPasswordsMatch input->password-strength-gauge#checkPasswordStrength input->password-breach#check",
aria_describedby="password-errors password-strength"
) }}
<div id="password-errors">
{% if form.new_password.errors %}
<ul class="form-errors" role="alert">
{% for error in form.new_password.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
</div>
{{ input_field_errors(
instance="password",
form_instance=form.new_password
)}}
{{ password_strength_gauge(data_password_strength_gauge_target="strengthGauge") }}
</div>
<div class="form-group">
<label for="password_confirm" class="form-group__label">
{% trans %}Confirm password{% endtrans %}
{% if form.password_confirm.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ form.password_confirm(placeholder=gettext("Confirm password") ,
required="required",
class_="form-group__field",
autocomplete="new-password",
spellcheck="false",
data_password_target="password",
data_password_match_target="passwordMatch",
data_action="input->password-match#checkPasswordsMatch",
aria_describedby="password-confirm-errors",
) }}
<div id="password-confirm-errors">
{% if form.password_confirm.errors %}
<ul class="form-errors" role="alert">
{% for error in form.password_confirm.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
<div class="form-group">
{{ input_field(
instance="password-confirm",
user_friendly_name="Confirm password",
form_instance=form.password_confirm,
placeholder="Confirm password",
autocomplete="new-password",
required="required",
spellcheck="false",
data_password_target="password",
data_password_match_target="passwordMatch",
data_action="input->password-match#checkPasswordsMatch",
)}}
</div>
</div>
</div>
<div class="form-group">
<ul class="form-errors">
Expand Down
49 changes: 17 additions & 32 deletions warehouse/templates/accounts/reset-password.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ <h1 class="page-title">{% trans %}Reset your password{% endtrans %}</h1>
{% endif %}
<div class="form-group">
<div class="split-layout">
<label for="password" class="form-group__label">
{% trans %}Password{% endtrans %}
{% if form.new_password.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ input_field_label(
user_friendly_name="Password",
form_instance=form.new_password
)}}
<label for="show-password">
<input data-action="change->password#togglePasswords"
data-password-target="showPassword"
Expand All @@ -50,35 +48,22 @@ <h1 class="page-title">{% trans %}Reset your password{% endtrans %}</h1>
data_action="input->password-match#checkPasswordsMatch input->password-strength-gauge#checkPasswordStrength",
aria_describedby="password-errors password-strength",
) }}
{% if form.new_password.errors %}
<ul id="password-errors" class="form-errors" role="alert">
{% for error in form.new_password.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
{{ field_errors(form_instance=form.new_password) }}
{{ password_strength_gauge(data_password_strength_gauge_target="strengthGauge") }}
</div>
<div class="form-group">
<label for="password_confirm" class="form-group__label">
{% trans %}Confirm password{% endtrans %}
{% if form.password_confirm.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{{ form.password_confirm(placeholder=gettext("Confirm password") ,
required="required",
autocomplete="new-password",
spellcheck="false",
class_="form-group__field",
data_password_target="password",
data_password_match_target="passwordMatch",
data_action="input->password-match#checkPasswordsMatch",
aria_describedby="confirm-password-errors",
) }}
{% if form.password_confirm.errors %}
<ul id="confirm-password-errors" class="form-errors" role="alert">
{% for error in form.password_confirm.errors %}<li>{{ error }}</li>{% endfor %}
</ul>
{% endif %}
{{ input_field(
instance="confirm-password",
user_friendly_name="Confirm password",
form_instance=form.password_confirm,
placeholder="Confirm password",
autocomplete="new-password",
spellcheck="false",
required="required",
data_password_target="password",
data_password_match_target="passwordMatch",
data_action="input->password-match#checkPasswordsMatch",
)}}
</div>
<div class="form-group">
<ul class="form-errors" role="alert">
Expand Down
30 changes: 30 additions & 0 deletions warehouse/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
{{ timestamp|format_date() }}
</time>
{%- endmacro %}
{% macro input_field_label(user_friendly_name="", form_instance="") -%}
<label for="{{form_instance.name}}" class="form-group__label">
{% trans form_name=user_friendly_name %}{{ form_name }}{% endtrans %}
{% if form_instance.flags.required %}
<span class="form-group__required">{% trans %}(required){% endtrans %}</span>
{% endif %}
</label>
{% endmacro %}
{% macro input_field_errors(instance, form_instance) -%}
<div id="{{instance}}-errors">
{% if form_instance.errors %}
<ul class="form-errors" role="alert">
{% for error in form_instance.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endmacro %}
{% macro input_field(instance, user_friendly_name, form_instance, placeholder="") -%}
{{ input_field_label(user_friendly_name, form_instance) }}
{% set form_field_attrs = {
"placeholder": gettext(placeholder),
"class": "form-group__field",
"aria-describedby": instance ~ "-errors"
} %}
{% set _ = form_field_attrs.update(kwargs) %}
{{ form_instance(**form_field_attrs) }}
{{ input_field_errors(instance, form_instance) }}
{%- endmacro %}
{% macro password_strength_gauge(data_password_strength_gauge_target=None) -%}
<div id="password-strength">
<p class="form-group__help-text">
Expand Down
Loading