preaction / Yancy

The Best Web Framework Deserves the Best Content Management System
http://preaction.me/yancy/
Other
54 stars 21 forks source link

Custom form labels in field_for and form_for at Yancy::Plugin::Form #92

Closed pavelsr closed 4 years ago

pavelsr commented 4 years ago

E.g. I have a field which has following sql definition

`reg_address` VARCHAR(511) NOT NULL,

I want that in form corresponding <label>...</label> will be Registration address ( name attribute untouched )

For now I have following output of Yancy::Plugin::Form::input_for

%= app->yancy->form->input_for( $schema, 'reg_address' );
<div class="form-group">
    <label for="field-passports-reg_address">reg_address *</label>
    <input class="form-control" type="text" required="1" id="field-passports-reg_address" name="reg_address">
</div>

yancy->form->input %args have no way to customize <label> tag value

Currently there is only enum_labels param used for just boolean fields that generate output like

<div class="btn-group yes-no">
    <label class="btn btn-xs btn-outline-success">
        <input type="radio" name="is_admin" value="1"> Yes
</label>
    <label class="btn btn-xs btn-outline-danger">
        <input type="radio" name="is_admin" value="0" selected="selected"> No
</label>
</div>

Is there any undocumented way to customize label for other input types ?

If no, do you think that suggested feature would be useful ? If yes, I can try to make a PR.

preaction commented 4 years ago

What you want should be possible already through the schema configuration:

use Mojolicious::Lite;
plugin Yancy => {
    backend => '...',
    read_schema => 1, # this will fill in the rest of the schema
    schema => {
        passports => {
            properties => {
                reg_address => {
                    title => 'Registration address',
                    description => 'Put a description here, it will appear',
               },
        },
    },
};

The form plugins try to use the same information from the schema that the editor uses. All of the configuration here should work in the form plugin, too (though some of the more advanced fields are not yet implemented).

Also, it looks like the field_for() helper does take a %opt hash, it's just not documented ☹️. I'll fix the documentation. The %opt hash takes the same keys as the schema (so, title and description).