Data-MuForm

 view release on metacpan or  search on metacpan

lib/Data/MuForm.pm  view on Meta::CPAN

sub setup {
    my ( $self, @args ) = @_;

    if ( @args == 1 ) {
        $self->params( $args[0] );
    }
    elsif ( @args > 1 ) {
        my $hashref = {@args};
        while ( my ( $key, $value ) = each %{$hashref} ) {
            warn "invalid attribute '$key' passed to setup_form"
                unless $self->can($key);
            $self->$key($value);
        }
    }
    # set_active
    $self->set_active;

    # customization hook
    $self->in_setup;

    # set the submitted flag
    $self->submitted(1) if ( $self->has_params && ! defined $self->submitted );

    # fill the 'value' attributes from model, init_values or fields
    $self->fill_values;

    # fill in the input attribute
    my $params = data_clone( $self->params );
    if ( $self->submitted ) {
        $self->fill_from_params($params, 1 );
        # if the params submitted don't match fields, it shouldn't count as 'submitted'
        if ( ! scalar keys %{$self->input} ) {
            $self->submitted(0);
        }
    }

}

sub fill_values {
    my $self = shift;

    # these fill the 'value' attributes
    if ( $self->model && $self->use_model_for_defaults ) {
      $self->fill_from_object_source('model');
      $self->fill_from_object($self->model);
    }
    elsif ( $self->init_values ) {
        $self->fill_from_object_source('init_values');
        $self->fill_from_object($self->init_values );
    }
    elsif ( !$self->submitted ) {
        # no initial object. empty form must be initialized
        $self->fill_from_fields;
    }
}

sub in_setup { }
sub after_setup { }
sub after_build_fields { }

sub update_model {
    my $self = shift;
}


sub munge_params {
    my ( $self, $params, $attr ) = @_;

    my $_fix_params = Data::MuForm::Params->new;
    my $new_params = $_fix_params->expand_hash($params);
    if ( $self->field_prefix ) {
        $new_params = $new_params->{ $self->field_prefix };
    }
    $new_params = {} if !defined $new_params;
    return $new_params;
}

sub set_active {
    my $self = shift;
    if( $self->has_active ) {
        foreach my $fname (@{$self->active}) {
            my $field = $self->field($fname);
            if ( $field ) {
                $field->_active(1);
            }
            else {
                warn "field $fname not found to set active";
            }
        }
        $self->clear_active;
    }
    if( $self->has_inactive ) {
        foreach my $fname (@{$self->inactive}) {
            my $field = $self->field($fname);
            if ( $field ) {
                $field->_active(0);
            }
            else {
                warn "field $fname not found to set inactive";
            }
        }
        $self->clear_inactive;
    }
}

#====================================================================
# Validation
#====================================================================

sub validate_form {
    my $self = shift;

    $self->fields_validate;
    $self->validate;       # hook
    $self->validate_model; # hook
    $self->fields_set_value;
    $self->submitted(undef);
    $self->ran_validation(1);
}

# hook for child forms



( run in 0.905 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )