Chandra
view release on metacpan or search on metacpan
lib/Chandra/Form.pm view on Meta::CPAN
});
Add a dropdown select field. C<options> is an arrayref of hashrefs,
each with C<value> and C<label> keys. An option can be C<disabled>.
=head2 checkbox
$form->checkbox('agree', {
label => 'I agree to the terms',
checked => 1,
value => 'yes', # defaults to "1"
});
Add a checkbox. The label appears I<after> the checkbox. Serialized
as C<1>/C<0> in form data.
=head2 radio
$form->radio('priority', {
label => 'Priority',
options => [
{ value => 'low', label => 'Low' },
{ value => 'high', label => 'High' },
],
value => 'low',
});
Add a radio button group. C<options> and C<value> work like C<select()>.
=head2 number
$form->number('qty', { label => 'Quantity', min => 1, max => 99, step => 1 });
Add a numeric input. Accepts C<min>, C<max>, and C<step>.
=head2 range
$form->range('volume', { label => 'Volume', min => 0, max => 100, value => 50 });
Add a range slider. Same numeric options as C<number()>.
=head2 hidden
$form->hidden('token', { value => 'secret' });
Add a hidden field. No label or error placeholder is rendered.
=head2 submit
$form->submit('Save');
Set the submit button label. Defaults to C<"Submit">.
=head2 group
$form->group('Appearance' => sub {
$form->select('theme', { ... });
$form->number('font_size', { ... });
});
Wrap fields in a C<< <fieldset> >> with a C<< <legend> >>.
=head2 render
my $html = $form->render;
Render the form to an HTML string. Each field is wrapped in a
C<< <div class="chandra-field"> >> with a label and error placeholder.
=head2 bind_js
my $js = $form->bind_js;
Returns JavaScript that intercepts form submit and change/input events,
sending data to Perl via C<window.chandra.invoke()>.
=head2 attach
$form->attach($app);
Register this form with the global form registry and bind the bridge
events (C<_form_submit>, C<_form_change>, C<_form_input>,
C<_form_values>) on the given L<Chandra::App>. The binding JS is
automatically injected via C<dispatch_eval>.
Multiple forms can be attached to the same app; each submit/change
event is routed to the correct form by its C<id>.
=head2 detach
$form->detach;
Remove this form from the global registry. Future bridge events
for this form's id will be silently ignored.
=head2 set_values_js
my $js = $form->set_values_js({ username => 'bob', theme => 'light' });
Returns JavaScript that sets DOM field values from a hashref.
=head2 get_values_js
my $js = $form->get_values_js;
Returns JavaScript that reads current form values and sends them via bridge.
=head2 show_errors_js
my $js = $form->show_errors_js({ username => 'Required' });
Returns JavaScript that displays error messages next to fields.
=head2 clear_errors_js
my $js = $form->clear_errors_js;
Returns JavaScript that clears all error messages.
=head2 on_change
( run in 0.803 second using v1.01-cache-2.11-cpan-2398b32b56e )