简体   繁体   English

我应该使用哪个Perl模块来生成验证CRUD Web表单?

[英]Which Perl module should I use to generate a validating CRUD webform?

Has anyone successfully used something like DBIx::Class::WebForm or CatalystX-CRUD to automagically build a self-validating webform from a database table? 有没有人成功使用DBIx :: Class :: WebFormCatalystX-CRUD之 类的东西来从数据库表中自动构建自验证Web表单?

I'm imagining a module that reads a database table schema, reads the constraints for each column, and generates some abstract representation of a webform, with fields for error messages, etc. I'm using Catalyst and Plack with a big existing codebase. 我正在想象一个模块,该模块可以读取数据库表架构,读取每一列的约束,并生成Webform的抽象表示形式,以及用于错误消息的字段等。我将Catalyst和Plack与现有的大型代码库一起使用。

I don't want to code up an HTML webform, nor any validation logic. 我既不想编写HTML Web表单,也不想编写任何验证逻辑。 I'm aiming to write as little code as possible, in the style of Ruby on Rails. 我的目标是以Ruby on Rails的样式编写尽可能少的代码。 Which Perl module is best for this? 哪个Perl模块最适合?

UPDATE: I've solved the webform side with HTML::FormFu , but it's still clunky mapping the form inputs onto the database, eg date_start and date_end both relate to the 'created' column, and comment should match using 'LIKE %foo%', etc. Where's the 'DBICFu'? 更新:我已经用HTML :: FormFu解决了Webform方面,但是将表单输入映射到数据库上仍然很笨拙,例如date_start和date_end都与'created'列相关,并且注释应该使用'LIKE%foo% ”等。“ DBICFu”在哪里?

UPDATE: This is for a web application, the webform should not look like a database table. 更新:这是针对Web应用程序的,Web表单不应看起来像数据库表。 I'm not looking for a database management tool. 我不是在寻找数据库管理工具。

I've used HTML::FormHandler to generate forms for me in this fashion. 我已经使用HTML :: FormHandler以这种方式为我生成表单。 It needs some tweaking, but it does 90% of the work for you. 它需要进行一些调整,但它可以为您完成90%的工作。 Separately DBIx::Class offers a similar tool. 另外,DBIx :: Class提供了类似的工具。

You can use use HTML::FormHandler::Moose and HTML::FormHandler::Model::DBIC and get some nice forms. 您可以使用HTML::FormHandler::MooseHTML::FormHandler::Model::DBIC获得一些不错的表单。

As a simple example: 作为一个简单的例子:

The form definition: 表单定义:

package MyStats::Form::Datetime ;

use HTML::FormHandler::Moose ;
extends 'HTML::FormHandler::Model::DBIC' ;

use Date::Calc qw(Today_and_Now) ;

has_field 'datetimeid' => ( label     => 'ID' ) ;
has_field 'datetime'   => ( type      => 'Text',
                            apply     => [ { transform => \&transform_dt } ] ,
                            deflation => \&deflation_dt ,
                            required  => 1 ) ;
has_field 'submit'     => ( type => 'Submit' ,
                            value => 'Speichern' ) ;
# These are the fields of the table datetime

sub transform_dt {
  my ( $dt ) = @_ ;

  my @d = ( $dt =~ m/(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{1,2}):(\d{1,2})/ ) ;
  return sprintf( '%04d-%02d-%02d %02d:%02d:00' , @d[2,1,0,3,4] ) ;
}

sub deflation_dt {
  my ( $dt ) = @_ ;

  my @d = ( $dt =~ m/(\d{4})-(\d{2})-(\d{2})\s+(\d{1,2}):(\d{1,2})/ ) ;
  if( ! @d ) {
    @d = Today_and_Now() ;
  }
  return sprintf( '%02d.%02d.%04d %02d:%02d:00' , @d[2,1,0,3,4] ) ;
}

1 ;

And the usage in a controller: 以及控制器中的用法:

package MyStats::Controller::Datetime ;
use Moose ;
use namespace::autoclean ;

BEGIN { extends 'Catalyst::Controller' ; }

use MyStats::Form::Datetime ;

has 'form' => ( isa     => 'MyStats::Form::Datetime' ,
                is      => 'rw' ,
                lazy    => 1 ,
                default => \&new_datetime_form ) ;

sub new_datetime_form {
  MyStats::Form::Datetime->new( css_class => 'datetimeform' ,
                                name => 'datetimeform' ) ;
}

...

sub add :Local :Args(0) {
  my ( $self , $ctx ) = @_ ;

  my $data = $ctx->model( 'MyStatsDB::Datetime' )->new_result( {} ) ;
  $ctx->stash( template => 'datetime/add.tt2' ,
               form     => $self->form ) ;
  $ctx->bread_crumb( { name => 'Datum/Zeit eingeben' ,
                       location => '/datetime/add' } ) ;

  $ctx->req->param( 'datetimeid' , undef ) if $ctx->req->param( 'datetimeid' ) ;
  return unless $self->form->process( item   => $data ,
                                      params => $ctx->req->params ) ;
  $ctx->flash( message => 'Neuer Datensatz ' . $data->datetimeid .
                          ' angelegt.' ,
               id_add  => $data->datetimeid ) ;
  $ctx->res->redirect( $ctx->uri_for( '/datetime' ) ) ;
}

...

__PACKAGE__->meta->make_immutable ;

1 ;

Works good. 效果不错。

There are a number of crud options on the Catalyst wiki . 在Catalyst Wiki上有许多选项

It sounds like AutoCrud would fit your needs. 听起来AutoCrud可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM