Changeset 24 in text-formbuilder for trunk/lib/Text


Ignore:
Timestamp:
11/16/04 14:00:41 (20 years ago)
Author:
peter
Message:

allow for validating but not required fields

Location:
trunk/lib/Text
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Text/FormBuilder.pm

    r23 r24  
    8383            } 
    8484        } 
    85     } 
    86      
    87     # remove extraneous undefined values 
    88     for my $field (@{ $self->{form_spec}{fields} }) { 
    89         defined $$field{$_} or delete $$field{$_} foreach keys %{ $field }; 
    9085    } 
    9186     
     
    145140    } 
    146141     
    147  
     142    # remove extraneous undefined values 
     143    for my $field (@{ $self->{form_spec}{fields} }) { 
     144        defined $$field{$_} or delete $$field{$_} foreach keys %{ $field }; 
     145    } 
     146     
     147    # because this messes up things at the CGI::FormBuilder::field level 
     148    # it seems to be marking required based on the existance of a 'required' 
     149    # param, not whether it is true or defined 
     150    $$_{required} or delete $$_{required} foreach @{ $self->{form_spec}{fields} }; 
    148151 
    149152     
    150153    $self->{form} = CGI::FormBuilder->new( 
    151154        %DEFAULT_OPTIONS, 
     155        required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], 
    152156        title => $self->{form_spec}{title}, 
    153157        template => { 
     
    218222        %DEFAULT_OPTIONS, 
    219223        title => $self->{form_spec}{title}, 
     224        required => [ map { $$_{name} } grep { $$_{required} } @{ $self->{form_spec}{fields} } ], 
    220225        template => { 
    221226            type => 'Text', 
     
    327332        $OUT .= (grep { $$_{invalid} } @group_fields) ? qq[  <tr class="invalid">\n] : qq[  <tr>\n]; 
    328333         
    329          
    330334        #TODO: validated but not required fields 
    331335        # in a form spec: //EMAIL? 
    332336         
    333         #TODO: this doesn't seem to be working; all groups are getting marked as required         
    334337        $OUT .= '    <th class="label">'; 
    335338        $OUT .= (grep { $$_{required} } @group_fields) ? qq[<strong class="required">$$line[1]{label}:</strong>] : "$$line[1]{label}:"; 
     
    362365    td ul { list-style: none; padding-left: 0; margin-left: 0; } 
    363366    .sublabel { color: #999; } 
     367    .invalid { background: red; } 
    364368  </style> 
    365369</head> 
     
    586590=head2 Fields 
    587591 
    588 Form fields are each described on a single line. The simplest field is just a 
    589 name: 
     592First, a note about multiword strings in the fields. Anywhere where it says 
     593that you may use a multiword string, this means that you can do one of two 
     594things. For strings that consist solely of alphanumeric characters (i.e. 
     595C<\w+>) and spaces, the string will be recognized as is: 
     596 
     597    field_1|A longer label 
     598 
     599If you want to include non-alphanumerics (e.g. punctuation), you must  
     600single-quote the string: 
     601 
     602    field_2|'Dept./Org.' 
     603 
     604To include a literal single-quote in a single-quoted string, escape it with 
     605a backslash: 
     606 
     607    field_3|'\'Official\' title' 
     608 
     609Now, back to the basics. Form fields are each described on a single line. 
     610The simplest field is just a name (which cannot contain any whitespace): 
    590611 
    591612    color 
     
    597618    color|Favorite color 
    598619 
    599 Field names cannot contain whitespace, but the descriptive label can. 
     620The descriptive label can be a multiword string, as described above. 
    600621 
    601622To use a different input type: 
     
    629650    color|Favorite color:select{red,blue,green} 
    630651 
    631 Values are in a comma-separated list inside curly braces. Whitespace 
    632 between values is irrelevant, although there cannot be any whitespace 
    633 within a value. 
     652Values are in a comma-separated list of single words or multiword strings 
     653inside curly braces. Whitespace between values is irrelevant. 
    634654 
    635655To add more descriptive display text to a vlaue in a list, add a square-bracketed 
     
    637657 
    638658    ...:select{red[Scarlet],blue[Azure],green[Olive Drab]} 
    639  
    640 As you can see, spaces I<are> allowed within the display text for a value. 
    641659 
    642660If you have a list of options that is too long to fit comfortably on one line, 
     
    661679{ value2 => 'Description 2}, ... ) >> form. 
    662680 
    663 B<NOTE:> This feature of the language may go away unless I find a compelling 
     681I<B<NOTE:> This feature of the language may go away unless I find a compelling 
    664682reason for it in the next few versions. What I really wanted was lists that 
    665683were filled in at run-time (e.g. from a database), and that can be done easily 
    666 enough with the CGI::FormBuilder object directly. 
     684enough with the CGI::FormBuilder object directly.> 
    667685 
    668686You can also supply a default value to the field. To get a default value of 
     
    670688 
    671689    color|Favorite color:select=green{red,blue,green} 
     690 
     691Default values can also be either single words or multiword strings. 
    672692 
    673693To validate a field, include a validation type at the end of the field line: 
     
    687707    title//VALUE 
    688708 
     709By default, adding a validation type to a field makes that field required. To 
     710change this, add a C<?> to the end of the validation type: 
     711 
     712    contact//EMAIL? 
     713 
     714In this case, you would get a C<contact> field that was optional, but if it 
     715were filled in, would have to validate as an C<EMAIL>. 
     716 
    689717=head2 Comments 
    690718 
     
    702730with their own id and (optional) heading 
    703731 
    704 Optional validated fields; marked like C<//EMAIL?> 
    705  
    706732Better examples in the docs (maybe a standalone or two as well) 
    707733 
  • trunk/lib/Text/FormBuilder/grammar

    r23 r24  
    1414        $type, 
    1515        @options, 
     16        $required, 
    1617        $list_var, 
    1718        $size, 
     
    105106            list     => $list_var, 
    106107            validate => $item{'validate(?)'}[0], 
     108            required => $required || 0, 
    107109        }; 
    108110         
     
    121123        } 
    122124         
     125        #warn "field $item{name} is required" if $required; 
     126         
    123127        $type = undef; 
     128        $required = 0; 
    124129        $list_var = undef; 
    125130        $size = undef; 
     
    164169list_var: /@[A-Z_]+/ { $list_var = $item[1] } 
    165170 
    166 option: (simple_multiword | quoted_string) display_text(?) 
     171option: (value | simple_multiword | quoted_string) display_text(?) 
    167172    { push @options, { $item[1] => $item{'display_text(?)'}[0] } } 
    168173 
     
    171176display_text: '[' /[^\]]+/i ']'    { $item[2] } 
    172177 
    173 validate: '//' value 
     178validate: '//' (optional_pattern | required_pattern)    { $item[2] } 
     179 
     180optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] } 
     181 
     182required_pattern: /[A-Z_]+/ { $required = 1; $item[1] } 
    174183 
    175184comment: '#' /.*/ 
Note: See TracChangeset for help on using the changeset viewer.