Skip to content

Instantly share code, notes, and snippets.

@masto
Created May 20, 2011 14:34
Show Gist options
  • Save masto/983029 to your computer and use it in GitHub Desktop.
Save masto/983029 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
package Foo;
use parent 'Rose::Object';
use Rose::Object::MakeMethods::Generic (
scalar => [qw/name budget bar/],
);
package Bar;
use parent 'Rose::Object';
use Rose::Object::MakeMethods::Generic (
scalar => [qw/color budget/],
);
package BarForm;
use parent 'Rose::HTML::Form';
sub build_form {
my $self = shift;
$self->add_fields(
color => { type => 'text' },
budget => { type => 'numeric' },
);
}
sub bar_from_form {
my $self = shift;
my $bar = $self->object_from_form(class => "Bar");
return $bar;
}
package FooForm;
sub build_form {
my $self = shift;
$self->add_fields(
name => { type => 'text' },
budget => { type => 'numeric' },
);
$self->add_form(bar => BarForm->new(empty_is_ok => 1));
}
sub foo_from_form {
my $self = shift;
my $foo = $self->object_from_form(class => "Foo");
my $bar = $self->form("bar")->bar_from_form;
$foo->bar($bar);
return $foo;
}
use parent 'Rose::HTML::Form';
package main;
my $foo_form = FooForm->new;
$foo_form->params(
name => "test",
budget => "100.00",
"bar.color" => "red",
);
$foo_form->init_fields;
my $foo = $foo_form->foo_from_form;
print "foo name: ", $foo->name, "\n";
print "foo budget: ", $foo->budget, "\n";
print "bar color: ", $foo->bar->color, "\n";
# This prints 100.00 - I don't want bar's budget to be set
print "bar budget: ", $foo->bar->budget, "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment