If you know for sure you're doing an update, you can always call
$object->update. But IME it's common that a form does not include
every field in a table, so a safer bet is to first load() the object
based on the primary key valye in the form (if any) and then pass that
loaded object to *_from_form(). That way any existing fields are
preserved and any new values from the form are merged with them.
You can usually use the same *_from_form() routine for both add and
update forms with a little effort. (I usually have my "add" form
inherit from my "edit" form or vice versa.)
sub foo_from_form
{
my($self) = shift;
my $foo = My::DB::Foo->new;
if(my $id = $self->field_value('id'))
{
$foo->id($id);
$foo->load;
}
return $self->db_object_from_form($foo);
}
Some of this is mooted by calling $object->save(changes_only => 1)
instead of just plain save(), but I actually usually combine both
approaches, using a *_from_form() method like the above and then also
saving changes only.
Also note than any exceptions thrown in foo_from_form() must be caught
by the caller. It's my usual practice to wrap any form or db-related
tasks in the webapp method inside an eval block, and simply let all
those methods throw exceptions as needed.
-John