-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Immediate server side validation for text field form inputs (#1672)
Co-authored-by: Cameron Dutro <camertron@gmail.com> Co-authored-by: neall <neall@users.noreply.github.com> Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
- Loading branch information
1 parent
c1845c6
commit 1a7dadd
Showing
26 changed files
with
318 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/view-components': patch | ||
--- | ||
|
||
Add auto_check_src option to forms framework text fields to run server-side validation on field change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-27 Bytes
(99%)
...eenshots/previews.test.ts-snapshots/primer/alpha/text_field_preview/focused.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
rails: cd demo; bin/rails s -p 4000 | ||
js: npx chokidar "app/components/**/*.ts" -i "app/components/**/*.d.ts" -c "npm run build:js" | ||
js: npx chokidar "app/components/**/*.ts" "lib/primer/forms/**/*.ts" -i "app/components/**/*.d.ts" -i "lib/primer/forms/**/*.d.ts" -c "npm run build:js" | ||
css: npx chokidar "app/components/**/*.pcss" -c "npm run build:css" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
# :nodoc: | ||
# :nocov: | ||
class ImmediateValidationForm < ApplicationForm | ||
form do |validation_form| | ||
validation_form.text_field( | ||
name: :has_error, | ||
label: "Will have error", | ||
caption: "Every time this checks with the server, it returns an error", | ||
auto_check_src: @view_context.example_check_error_path | ||
) | ||
|
||
validation_form.text_field( | ||
name: :no_error, | ||
label: "Will not error", | ||
caption: "Will not have an error when it checks the server", | ||
auto_check_src: @view_context.example_check_ok_path, | ||
validation_message: "This message will go away once you type something" | ||
) | ||
|
||
validation_form.text_field( | ||
name: :random_error, | ||
label: "Random error", | ||
caption: "Server checks will randomly respond with errors", | ||
auto_check_src: @view_context.example_check_random_path | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# For auto-check previews | ||
# :nocov: | ||
class AutoCheckController < ApplicationController | ||
skip_before_action :verify_authenticity_token | ||
|
||
def error | ||
render status: :unprocessable_entity, plain: "Error! Error!" | ||
end | ||
|
||
def ok | ||
head :ok | ||
end | ||
|
||
def random | ||
if rand > 0.5 | ||
head :ok | ||
else | ||
render status: :unprocessable_entity, plain: "Random error!" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import '@github/auto-check-element' | ||
import {controller, target} from '@github/catalyst' | ||
|
||
@controller | ||
class PrimerTextFieldElement extends HTMLElement { | ||
@target inputElement: HTMLInputElement | ||
@target validationElement: HTMLElement | ||
@target validationMessageElement: HTMLElement | ||
|
||
#abortController: AbortController | null | ||
|
||
connectedCallback(): void { | ||
this.#abortController?.abort() | ||
const {signal} = (this.#abortController = new AbortController()) | ||
|
||
this.inputElement.addEventListener( | ||
'auto-check-success', | ||
() => { this.clearError() }, | ||
{signal} | ||
) | ||
|
||
this.inputElement.addEventListener( | ||
'auto-check-error', | ||
(event: any) => { | ||
event.detail.response.text().then( | ||
(error_message: string) => { this.setError(error_message) } | ||
) | ||
}, | ||
{signal} | ||
) | ||
} | ||
|
||
disconnectedCallback() { | ||
this.#abortController?.abort() | ||
} | ||
|
||
clearError(): void { | ||
this.inputElement.removeAttribute('invalid') | ||
this.validationElement.hidden = true | ||
this.validationMessageElement.innerText = '' | ||
} | ||
|
||
setError(message: string): void { | ||
this.validationMessageElement.innerText = message | ||
this.validationElement.hidden = false | ||
this.inputElement.setAttribute('invalid', 'true') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
<%= render(FormControl.new(input: @input)) do %> | ||
<% if @input.leading_visual || @input.show_clear_button? %> | ||
<%= content_tag(:div, **@field_wrap_arguments) do %> | ||
<%= render Primer::ConditionalWrapper.new(condition: @input.auto_check_src, tag: "primer-text-field") do %> | ||
<%= render(FormControl.new(input: @input)) do %> | ||
<%= render Primer::ConditionalWrapper.new(tag: :div, class: @input.field_wrap_classes, condition: @input.leading_visual || @input.show_clear_button?) do %> | ||
<% if @input.leading_visual %> | ||
<span class="FormControl-input-leadingVisualWrap"> | ||
<%= render(Primer::Beta::Octicon.new(**@input.leading_visual)) %> | ||
</span> | ||
<% end %> | ||
<%= builder.text_field(@input.name, **@input.input_arguments) %> | ||
<%= render Primer::ConditionalWrapper.new(condition: @input.auto_check_src, tag: "auto-check", csrf: auto_check_authenticity_token, src: @input.auto_check_src) do %> | ||
<%= builder.text_field(@input.name, **@input.input_arguments) %> | ||
<% end %> | ||
<% if @input.show_clear_button? %> | ||
<button id="<%= @input.clear_button_id %>" class="FormControl-input-trailingAction" aria-label="Clear"> | ||
<%= render(Primer::Beta::Octicon.new(icon: :"x-circle-fill")) %> | ||
</button> | ||
<% end %> | ||
<% end %> | ||
<% else %> | ||
<%= builder.text_field(@input.name, **@input.input_arguments) %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.