LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / Setting / TextSetting.pm
blobe3f17b9ddbcab89ff9abac4ec7c4110804a8b94e
1 package LJ::Setting::TextSetting;
2 use base 'LJ::Setting';
3 use strict;
4 use warnings;
5 use Carp qw(croak);
7 # if override to something non-undef, current_value and save_text work
8 # assuming a userprop
9 sub prop_name { undef }
11 sub current_value {
12 my ($class, $u) = @_;
13 if (my $propname = $class->prop_name) {
14 return $u->prop($propname);
16 croak;
19 sub get_arg {
20 my ($class, $args) = @_;
21 return LJ::Setting::get_arg($class, $args, "txt");
24 # zero means no limit.
25 sub max_bytes { 0 }
26 sub max_chars { 0 }
28 # display size:
29 sub text_size { 40 }
31 sub question { croak; }
33 sub as_html {
34 my ($class, $u, $errs, $post) = @_;
35 my $key = $class->pkgkey;
36 return "<label for=''>" . $class->question . "</label>" .
37 LJ::html_text({
38 name => "${key}txt",
39 id => "${key}txt",
40 class => "text",
41 value => $errs ? $class->get_arg($post, "txt") : $class->current_value($u),
42 size => $class->text_size,
43 maxlength => $class->max_chars || undef,
44 }) .
45 $class->errdiv($errs, "txt");
48 # each subclass can override if necessary
49 sub error_check { 1 }
51 sub save {
52 my ($class, $u, $args) = @_;
53 $class->error_check($u, $args);
55 my $txt = $args->{txt} || "";
56 return 1 if $txt eq $class->current_value($u);
57 unless (LJ::text_in($txt)) {
58 $class->errors(txt => "Invalid UTF-8");
60 if ($class->max_bytes || $class->max_chars) {
61 my $trimmed = LJ::text_trim($txt, $class->max_bytes, $class->max_chars);
62 $class->errors(txt => "Too long") if $trimmed ne $txt;
64 return $class->save_text($u, $txt);
67 sub save_text {
68 my ($class, $u, $txt) = @_;
69 if (my $propname = $class->prop_name) {
70 return $u->set_prop($propname, $txt);
72 croak;