@@ -6,13 +6,13 @@ import { getCookie } from './lib.js';
66import { InitContext } from './context' ;
77
88
9- export function FieldIsRequired ( { replyTo} ) {
9+ export function FieldIsRequired ( { replyTo, message } ) {
1010 return (
1111 < span
1212 className = "form-text small invalid-feedback"
1313 { ...( ( replyTo > 0 ) && { style : { "fontSize" : "0.71rem" } } ) }
1414 >
15- { django . gettext ( "This field is required." ) }
15+ { django . gettext ( message ) }
1616 </ span >
1717 ) ;
1818}
@@ -67,6 +67,7 @@ export function PreviewComment({avatar, name, url, comment, replyTo}) {
6767
6868export function CommentForm ( { replyTo, onCommentCreated } ) {
6969 const {
70+ comment_max_length,
7071 default_followup,
7172 default_form,
7273 is_authenticated,
@@ -82,36 +83,59 @@ export function CommentForm({ replyTo, onCommentCreated }) {
8283 avatar : undefined , name : "" , email : "" , url : "" , comment : "" ,
8384 followup : default_followup ,
8485 errors : { name : false , email : false , comment : false } ,
85- alert : { message : "" , cssc : "" }
86+ alert : { message : "" , cssc : "" } ,
87+ comment_field_error : "" ,
8688 } ) ;
8789
90+ const default_error = "This field is required." ;
91+
92+ const get_comment_length_error_msg = ( ) => {
93+ return (
94+ `Ensure this value has at most ${ comment_max_length } `
95+ + `character (it has ${ lstate . comment . length } ).`
96+ ) ;
97+ }
98+
8899 const handle_input_change = ( event ) => {
89100 const target = event . target ;
90101 const value = target . type === 'checkbox' ? target . checked : target . value ;
91102 const iname = target . name ;
92103
93- setLstate ( { ...lstate , [ iname ] : value } ) ;
104+ let comment_field_error = "" ;
105+ if ( lstate . comment_field_error . length > 0 ) {
106+ comment_field_error = get_comment_length_error_msg ( ) ;
107+ }
108+ setLstate ( {
109+ ...lstate ,
110+ [ iname ] : value ,
111+ comment_field_error : comment_field_error
112+ } ) ;
94113 }
95114
96115 const is_valid_data = ( ) => {
97- let is_valid_name = true , is_valid_email = true ;
116+ let is_valid_name = true , is_valid_email = true , comment_field_error = "" ;
98117
99118 if ( ! is_authenticated || request_name )
100119 is_valid_name = ( / ^ \s * $ / . test ( lstate . name ) ) ? false : true ;
101120
102121 if ( ! is_authenticated || request_email_address )
103122 is_valid_email = ( / \S + @ \S + \. \S + / . test ( lstate . email ) ) ? true : false ;
104123
105- const is_valid_comment = ( / ^ \s * $ / . test ( lstate . comment ) ) ? false : true ;
124+ let is_valid_comment = ( / ^ \s * $ / . test ( lstate . comment ) ) ? false : true ;
125+ if ( lstate . comment . length >= comment_max_length ) {
126+ is_valid_comment = false ;
127+ comment_field_error = get_comment_length_error_msg ( ) ;
128+ }
106129
107130 setLstate ( {
108131 ...lstate ,
109132 errors : {
110133 ...lstate . errors ,
111134 name : ! is_valid_name ,
112135 email : ! is_valid_email ,
113- comment : ! is_valid_comment
114- }
136+ comment : ! is_valid_comment ,
137+ } ,
138+ comment_field_error : comment_field_error
115139 } ) ;
116140
117141 return is_valid_name && is_valid_email && is_valid_comment ;
@@ -253,12 +277,17 @@ export function CommentForm({ replyTo, onCommentCreated }) {
253277 < div className = { ( replyTo > 0 ) ? "col-12" : "col-10" } >
254278 < textarea
255279 required name = "comment" id = "id_comment"
256- value = { lstate . comment } maxLength = { 3000 }
280+ value = { lstate . comment }
257281 placeholder = { django . gettext ( "Your comment" ) }
258282 className = { get_input_css_classes ( "comment" ) }
259283 onChange = { handle_input_change }
260284 />
261- { lstate . errors . comment && < FieldIsRequired replyTo = { replyTo } /> }
285+ { lstate . errors . comment && (
286+ < FieldIsRequired
287+ replyTo = { replyTo }
288+ message = { lstate . comment_field_error || default_error }
289+ />
290+ ) }
262291 </ div >
263292 </ div >
264293 ) ;
@@ -285,7 +314,12 @@ export function CommentForm({ replyTo, onCommentCreated }) {
285314 onChange = { handle_input_change }
286315 className = { get_input_css_classes ( "name" ) }
287316 />
288- { lstate . errors . name && < FieldIsRequired replyTo = { replyTo } /> }
317+ { lstate . errors . name && (
318+ < FieldIsRequired
319+ replyTo = { replyTo }
320+ message = "This field is required."
321+ />
322+ ) }
289323 </ div >
290324 </ div >
291325 ) ;
0 commit comments