How to Set the Value on a Submit Button using Reaction.

Overview

By default, the value of a submit button is apply. Let’s say one would like to change that. This tiny article explores the different ways to do so and what are the advantages and disadvantages of each. It is important to know upfront that we’re talking about a ViewPort Action Role Attribute. Specifically, we’re concerned with the apply_label attribute, and we’d like to override its default value of apply to that of Search.

Put the value in the Widget that corresponds to the Controller of the Action

implements fragment apply_button {
    arg 'label' => 'Search';
};

This will change the value of the submit button to Search

Disadvantages

  • Can’t reuse elsewhere
  • Seems a little black magicish to the this Reaction novice.

Advantages

  • Short and sweet

Put Directly in the push_viewport of the Controller

apply_label => 'Search'

Disadvantages/Advantages

  • Similarly to above but the value is set in the Controller instead of the Widget

Put the value in config

In __PACKAGE__->config or equivalent:

    search_apply_label => 'Search',

then one would use the value by passing it to push_viewport like so:

    apply_label => $self->{search_apply_label}

Disadvantage

  • No error checking for typo

If I pass

apply_label => $self->{serch_apply_label}

to push_viewport then I will get no error message just undef.

Advantage

  • Can reuse the value elsewhere.

Use Config and an Accessor

If one adds an accessor like so:

    has 'search_apply_label' => (
        isa => 'Str',
        is  => 'rw'
    );

in addition to the adding the config key/value like previously:

    search_apply_label => 'Search',

then the can access the config value when passing it to push_viewport as:

    apply_label => $self->search_apply_label

Advantage

  • Reuse
  • The accessor would catch the typo shown previously and report something like:

“Unable to find method…”

Disadvantage

More typing.

My tags:
 
Popular tags:
 
Powered by MojoMojo