The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Yancy::Plugin::Form - Generate form HTML using various UI libraries

VERSION

version 1.013

SYNOPSIS

    use Mojolicious::Lite;
    plugin Yancy => {
        backend => 'pg://localhost/mysite',
        read_schema => 1,
    };
    app->yancy->plugin( 'Form::Bootstrap4' );
    app->routes->get( '/people/:id/edit' )->to(
        'yancy#set',
        collection => 'people',
        template => 'edit_people',
    );
    app->start;
    __DATA__
    @@ edit_people.html.ep
    %= $c->yancy->form->form_for( 'people' );

DESCRIPTION

The Form plugins generate forms from JSON schemas. Plugin and application developers can use the form plugin API to make forms, and then sites can load a specific form library plugin to match the style of the site.

NOTE: This API is EXPERIMENTAL and will be considered stable in Yancy version 2.0. Please report any issues you have or features you'd like to see. Minor things may change before version 2.0, so be sure to read the release changelog before upgrading.

Available Libraries

HELPERS

All form plugins add the same helpers with the same arguments so that applications can use the form plugin that matches their site's appearance. Yancy plugin and app developers should use form plugins to build forms so that users can easily customize the form's appearance.

yancy->form->input

    my $html = $c->yancy->form->input( %args );
    %= $c->yancy->form->plugin( %args );

Create a form input. Usually one of a <input>, <textarea, or <select> element, but can be more.

%args is a list of name/value pairs with the following keys:

type

The type of the input field to create. One of the JSON schema types. See "Generated Forms" in Yancy::Help::Config for details on the supported types.

format

For string types, the format the string should take. One of the supported JSON schema formats, along with some additional ones. See "Generated Forms" in Yancy::Help::Config for details on the supported formats.

pattern

A regex pattern to validate the field before submit.

required

If true, the field will be marked as required.

readonly

If true, the field will be marked as read-only.

disabled

If true, the field will be marked as disabled.

placeholder

The placeholder for <input> and <textarea> elements.

id

The ID for this field.

class

A string with additional classes to add to this field.

minlength

The minimum length of the text in this field. Used to validate the form.

maxlength

The maximum length of the text in this field. Used to validate the form.

minimum

The minimum value for the number in this field. Used to validate the form.

maximum

The maximum value for the number in this field. Used to validate the form.

Most of these properties are the same as the JSON schema field properties. See "Generated Forms" in Yancy::Help::Config for details on how Yancy translates JSON schema into forms.

yancy->form->field_for

    my $html = $c->yancy->form->field_for( $collection, $name );
    %= $c->yancy->form->field_for( $collection, $name );

Generate a field for the given $collection and property $name. The field will include a <label>, the appropriate input (<input>, <select>, or otherwise ), and any descriptive text.

yancy->form->form_for

    my $html = $c->yancy->form->form_for( $collection, %opt );
    %= $c->yancy->form->plugin( $collection, %opt );

Generate a form to edit an item from the given $collection. The form will include all the fields, a CSRF token, and a single button to submit the form.

method

The method attribute for the <form> tag. Defaults to POST.

action

The action URL for the <form> tag.

item

A hashref of values to fill in the form. Defaults to the value of the item in the stash (which is set by "set" in Yancy::Controller::Yancy.)

SEE ALSO

Yancy

AUTHOR

Doug Bell <preaction@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Doug Bell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.