Re: [Catalyst] transforming empty values in request to perl's undef

2017-03-03 Thread Rajesh Kumar Mallah


Thanks, It helps.


regds
Mallah.


> It's just a package and I have in make_schema_at.pl to apply the
> components
> that I want to use:
>
>   make_schema_at(
> "My::Schema::${class_name}",
> {
>   exclude   => qr/_\d+$/,
>   dump_directory=> "$Bin/../lib",
>   components=> [ '+My::TraitFor::Result::ForceNULL',
> '+My::TraitFor::Result::AuditLog' ],
>   schema_components => [ 'Schema::RestrictWithObject' ],
>   qualify_objects => 1, # prepend dpl/public in the
> __PACKAGE__->table(...) call
>   db_schema=> $schema,
> }
>   );
>
> Cheers
>
> On Fri, Mar 3, 2017 at 1:34 PM, Rajesh Kumar Mallah 
> wrote:
>
>>
>>
>> Thanks for that , it looks elegant .
>>
>> Could you please send some pointer to docs for Traits
>> approach.
>>
>> regds
>> mallah.
>>
>>
>>
>> > Not sure if that's the best way to do it or if it's a good approach,
>> but
>> I
>> > have a trait which is applied to all result classes.
>> >
>> > TraitFor::Result::ForceNULL;
>> > use strict;
>> > use warnings;
>> >
>> > my $null = undef;
>> >
>> > sub insert
>> > {
>> >   my $self = shift;
>> >
>> >   for my $col ($self->columns) {
>> > next unless defined $self->$col;
>> > next unless $self->$col eq "";
>> >
>> > my $info = $self->result_source->column_info($col);
>> > $self->$col($null) if $info->{is_nullable};
>> >   }
>> >   return $self->next::method(@_);
>> > }
>> >
>> > sub update
>> > {
>> >   my $self  = shift;
>> >   my %to_update = $self->get_dirty_columns;
>> >
>> >   return $self->next::method(@_) unless keys %to_update;
>> >
>> >   for my $col (keys %to_update) {
>> > next unless $to_update{$col} eq "";
>> > my $info = $self->result_source->column_info($col);
>> > $self->{_column_data}{$col} = $null if $info->{is_nullable};
>> >   }
>> >   return $self->next::method(@_);
>> > }
>> >
>> > 1;
>> >
>> > Best regards
>> >
>> > On Thu, Mar 2, 2017 at 5:14 PM, Rajesh Kumar Mallah
>> > >
>> > wrote:
>> >
>> >>
>> >> By empty i mean string with length 0 ie ''
>> >>
>> >> __CODE__
>> >>
>> >>
>> >> I am posting to rest endpoint Automagically generated via
>> >> Catalyst::Controller::DBIC::API  there is no explicit code hand
>> >> written by me.And that is why looking for a global filter.
>> >>
>> >> There are three great virtues of a programmer; Laziness, Impatience
>> and
>> >> Hubris -- Larry Wall.
>> >>
>> >>
>> >>
>> >> > Just curious: when you say "prefilled with nothing", you mean the
>> >> field
>> >> is
>> >> > empty, or the field is filled with one or more white spaces (like "
>> >> "),
>> >> or
>> >> > the field is a  with the first  having the
>> attribute
>> >> > "value" equals to ""? Or something else?
>> >> >
>> >> > gil
>> >> >
>> >> > On Thu, Mar 02, 2017 at 10:25:27AM -0500, Rajesh Kumar Mallah
>> wrote:
>> >> >>
>> >> >> Hi ,
>> >> >>
>> >> >> I want to transform request parameters in GET & POST requests
>> >> >> so that any thing which is empty (string) gets converted/replaced
>> >> >> my perl's undef (and DBIC's NULL).
>> >> >>
>> >> >> Why i need that:
>> >> >> 
>> >> >> I have edit form in my web app and some fields in the edit form
>> >> >> are prefilled with nothing. On submission the DB raises an
>> exception
>> >> >> that an empty string is being inserted to a integer field.
>> >> >>
>> >> >> Hence I feel like having a global filter that turns all blanks to
>> >> >> undef.
>> >> >>
>> >> >> The question is where to put this global filter.
>> >> >>
>> >> >> Regds
>> >> >> mallah.
>> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >> >> ___
>> >> >> List: Catalyst@lists.scsys.co.uk
>> >> >> Listinfo:
>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> >> >> Searchable archive:
>> >> >> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> >> >> Dev site: http://dev.catalyst.perl.org/
>> >> >
>> >> > ___
>> >> > List: Catalyst@lists.scsys.co.uk
>> >> > Listinfo:
>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> >> > Searchable archive:
>> >> > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> >> > Dev site: http://dev.catalyst.perl.org/
>> >> >
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> ___
>> >> List: Catalyst@lists.scsys.co.uk
>> >> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> >> Searchable archive: http://www.mail-archive.com/
>> >> catalyst@lists.scsys.co.uk/
>> >> Dev site: http://dev.catalyst.perl.org/
>> >>
>> > ___
>> > List: Catalyst@lists.scsys.co.uk
>> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> > Searchable archive:
>> > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> > Dev site: http://dev.catalyst.perl.org/
>> >
>>
>>
>>
>>
>>
>> ___
>> List: 

Re: [Catalyst] transforming empty values in request to perl's undef

2017-03-03 Thread Dimitar Petrov
It's just a package and I have in make_schema_at.pl to apply the components
that I want to use:

  make_schema_at(
"My::Schema::${class_name}",
{
  exclude   => qr/_\d+$/,
  dump_directory=> "$Bin/../lib",
  components=> [ '+My::TraitFor::Result::ForceNULL',
'+My::TraitFor::Result::AuditLog' ],
  schema_components => [ 'Schema::RestrictWithObject' ],
  qualify_objects => 1, # prepend dpl/public in the
__PACKAGE__->table(...) call
  db_schema=> $schema,
}
  );

Cheers

On Fri, Mar 3, 2017 at 1:34 PM, Rajesh Kumar Mallah 
wrote:

>
>
> Thanks for that , it looks elegant .
>
> Could you please send some pointer to docs for Traits
> approach.
>
> regds
> mallah.
>
>
>
> > Not sure if that's the best way to do it or if it's a good approach, but
> I
> > have a trait which is applied to all result classes.
> >
> > TraitFor::Result::ForceNULL;
> > use strict;
> > use warnings;
> >
> > my $null = undef;
> >
> > sub insert
> > {
> >   my $self = shift;
> >
> >   for my $col ($self->columns) {
> > next unless defined $self->$col;
> > next unless $self->$col eq "";
> >
> > my $info = $self->result_source->column_info($col);
> > $self->$col($null) if $info->{is_nullable};
> >   }
> >   return $self->next::method(@_);
> > }
> >
> > sub update
> > {
> >   my $self  = shift;
> >   my %to_update = $self->get_dirty_columns;
> >
> >   return $self->next::method(@_) unless keys %to_update;
> >
> >   for my $col (keys %to_update) {
> > next unless $to_update{$col} eq "";
> > my $info = $self->result_source->column_info($col);
> > $self->{_column_data}{$col} = $null if $info->{is_nullable};
> >   }
> >   return $self->next::method(@_);
> > }
> >
> > 1;
> >
> > Best regards
> >
> > On Thu, Mar 2, 2017 at 5:14 PM, Rajesh Kumar Mallah  >
> > wrote:
> >
> >>
> >> By empty i mean string with length 0 ie ''
> >>
> >> __CODE__
> >>
> >>
> >> I am posting to rest endpoint Automagically generated via
> >> Catalyst::Controller::DBIC::API  there is no explicit code hand
> >> written by me.And that is why looking for a global filter.
> >>
> >> There are three great virtues of a programmer; Laziness, Impatience and
> >> Hubris -- Larry Wall.
> >>
> >>
> >>
> >> > Just curious: when you say "prefilled with nothing", you mean the
> >> field
> >> is
> >> > empty, or the field is filled with one or more white spaces (like "
> >> "),
> >> or
> >> > the field is a  with the first  having the attribute
> >> > "value" equals to ""? Or something else?
> >> >
> >> > gil
> >> >
> >> > On Thu, Mar 02, 2017 at 10:25:27AM -0500, Rajesh Kumar Mallah wrote:
> >> >>
> >> >> Hi ,
> >> >>
> >> >> I want to transform request parameters in GET & POST requests
> >> >> so that any thing which is empty (string) gets converted/replaced
> >> >> my perl's undef (and DBIC's NULL).
> >> >>
> >> >> Why i need that:
> >> >> 
> >> >> I have edit form in my web app and some fields in the edit form
> >> >> are prefilled with nothing. On submission the DB raises an exception
> >> >> that an empty string is being inserted to a integer field.
> >> >>
> >> >> Hence I feel like having a global filter that turns all blanks to
> >> >> undef.
> >> >>
> >> >> The question is where to put this global filter.
> >> >>
> >> >> Regds
> >> >> mallah.
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> ___
> >> >> List: Catalyst@lists.scsys.co.uk
> >> >> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> >> >> Searchable archive:
> >> >> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> >> >> Dev site: http://dev.catalyst.perl.org/
> >> >
> >> > ___
> >> > List: Catalyst@lists.scsys.co.uk
> >> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> >> > Searchable archive:
> >> > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> >> > Dev site: http://dev.catalyst.perl.org/
> >> >
> >>
> >>
> >>
> >>
> >>
> >> ___
> >> List: Catalyst@lists.scsys.co.uk
> >> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> >> Searchable archive: http://www.mail-archive.com/
> >> catalyst@lists.scsys.co.uk/
> >> Dev site: http://dev.catalyst.perl.org/
> >>
> > ___
> > List: Catalyst@lists.scsys.co.uk
> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> > Searchable archive:
> > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> > Dev site: http://dev.catalyst.perl.org/
> >
>
>
>
>
>
> ___
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/
> catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>

Re: [Catalyst] transforming empty values in request to perl's undef

2017-03-03 Thread Rajesh Kumar Mallah


Thanks for that , it looks elegant .

Could you please send some pointer to docs for Traits
approach.

regds
mallah.



> Not sure if that's the best way to do it or if it's a good approach, but I
> have a trait which is applied to all result classes.
>
> TraitFor::Result::ForceNULL;
> use strict;
> use warnings;
>
> my $null = undef;
>
> sub insert
> {
>   my $self = shift;
>
>   for my $col ($self->columns) {
> next unless defined $self->$col;
> next unless $self->$col eq "";
>
> my $info = $self->result_source->column_info($col);
> $self->$col($null) if $info->{is_nullable};
>   }
>   return $self->next::method(@_);
> }
>
> sub update
> {
>   my $self  = shift;
>   my %to_update = $self->get_dirty_columns;
>
>   return $self->next::method(@_) unless keys %to_update;
>
>   for my $col (keys %to_update) {
> next unless $to_update{$col} eq "";
> my $info = $self->result_source->column_info($col);
> $self->{_column_data}{$col} = $null if $info->{is_nullable};
>   }
>   return $self->next::method(@_);
> }
>
> 1;
>
> Best regards
>
> On Thu, Mar 2, 2017 at 5:14 PM, Rajesh Kumar Mallah 
> wrote:
>
>>
>> By empty i mean string with length 0 ie ''
>>
>> __CODE__
>>
>>
>> I am posting to rest endpoint Automagically generated via
>> Catalyst::Controller::DBIC::API  there is no explicit code hand
>> written by me.And that is why looking for a global filter.
>>
>> There are three great virtues of a programmer; Laziness, Impatience and
>> Hubris -- Larry Wall.
>>
>>
>>
>> > Just curious: when you say "prefilled with nothing", you mean the
>> field
>> is
>> > empty, or the field is filled with one or more white spaces (like "
>> "),
>> or
>> > the field is a  with the first  having the attribute
>> > "value" equals to ""? Or something else?
>> >
>> > gil
>> >
>> > On Thu, Mar 02, 2017 at 10:25:27AM -0500, Rajesh Kumar Mallah wrote:
>> >>
>> >> Hi ,
>> >>
>> >> I want to transform request parameters in GET & POST requests
>> >> so that any thing which is empty (string) gets converted/replaced
>> >> my perl's undef (and DBIC's NULL).
>> >>
>> >> Why i need that:
>> >> 
>> >> I have edit form in my web app and some fields in the edit form
>> >> are prefilled with nothing. On submission the DB raises an exception
>> >> that an empty string is being inserted to a integer field.
>> >>
>> >> Hence I feel like having a global filter that turns all blanks to
>> >> undef.
>> >>
>> >> The question is where to put this global filter.
>> >>
>> >> Regds
>> >> mallah.
>> >>
>> >>
>> >>
>> >>
>> >> ___
>> >> List: Catalyst@lists.scsys.co.uk
>> >> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> >> Searchable archive:
>> >> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> >> Dev site: http://dev.catalyst.perl.org/
>> >
>> > ___
>> > List: Catalyst@lists.scsys.co.uk
>> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> > Searchable archive:
>> > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> > Dev site: http://dev.catalyst.perl.org/
>> >
>>
>>
>>
>>
>>
>> ___
>> List: Catalyst@lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive: http://www.mail-archive.com/
>> catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
>>
> ___
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>





___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] transforming empty values in request to perl's undef

2017-03-03 Thread Dimitar Petrov
Not sure if that's the best way to do it or if it's a good approach, but I
have a trait which is applied to all result classes.

TraitFor::Result::ForceNULL;
use strict;
use warnings;

my $null = undef;

sub insert
{
  my $self = shift;

  for my $col ($self->columns) {
next unless defined $self->$col;
next unless $self->$col eq "";

my $info = $self->result_source->column_info($col);
$self->$col($null) if $info->{is_nullable};
  }
  return $self->next::method(@_);
}

sub update
{
  my $self  = shift;
  my %to_update = $self->get_dirty_columns;

  return $self->next::method(@_) unless keys %to_update;

  for my $col (keys %to_update) {
next unless $to_update{$col} eq "";
my $info = $self->result_source->column_info($col);
$self->{_column_data}{$col} = $null if $info->{is_nullable};
  }
  return $self->next::method(@_);
}

1;

Best regards

On Thu, Mar 2, 2017 at 5:14 PM, Rajesh Kumar Mallah 
wrote:

>
> By empty i mean string with length 0 ie ''
>
> __CODE__
>
>
> I am posting to rest endpoint Automagically generated via
> Catalyst::Controller::DBIC::API  there is no explicit code hand
> written by me.And that is why looking for a global filter.
>
> There are three great virtues of a programmer; Laziness, Impatience and
> Hubris -- Larry Wall.
>
>
>
> > Just curious: when you say "prefilled with nothing", you mean the field
> is
> > empty, or the field is filled with one or more white spaces (like " "),
> or
> > the field is a  with the first  having the attribute
> > "value" equals to ""? Or something else?
> >
> > gil
> >
> > On Thu, Mar 02, 2017 at 10:25:27AM -0500, Rajesh Kumar Mallah wrote:
> >>
> >> Hi ,
> >>
> >> I want to transform request parameters in GET & POST requests
> >> so that any thing which is empty (string) gets converted/replaced
> >> my perl's undef (and DBIC's NULL).
> >>
> >> Why i need that:
> >> 
> >> I have edit form in my web app and some fields in the edit form
> >> are prefilled with nothing. On submission the DB raises an exception
> >> that an empty string is being inserted to a integer field.
> >>
> >> Hence I feel like having a global filter that turns all blanks to
> >> undef.
> >>
> >> The question is where to put this global filter.
> >>
> >> Regds
> >> mallah.
> >>
> >>
> >>
> >>
> >> ___
> >> List: Catalyst@lists.scsys.co.uk
> >> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> >> Searchable archive:
> >> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> >> Dev site: http://dev.catalyst.perl.org/
> >
> > ___
> > List: Catalyst@lists.scsys.co.uk
> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> > Searchable archive:
> > http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> > Dev site: http://dev.catalyst.perl.org/
> >
>
>
>
>
>
> ___
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/
> catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] transforming empty values in request to perl's undef

2017-03-02 Thread Rajesh Kumar Mallah

By empty i mean string with length 0 ie ''

__CODE__


I am posting to rest endpoint Automagically generated via
Catalyst::Controller::DBIC::API  there is no explicit code hand
written by me.And that is why looking for a global filter.

There are three great virtues of a programmer; Laziness, Impatience and
Hubris -- Larry Wall.



> Just curious: when you say "prefilled with nothing", you mean the field is
> empty, or the field is filled with one or more white spaces (like " "), or
> the field is a  with the first  having the attribute
> "value" equals to ""? Or something else?
>
> gil
>
> On Thu, Mar 02, 2017 at 10:25:27AM -0500, Rajesh Kumar Mallah wrote:
>>
>> Hi ,
>>
>> I want to transform request parameters in GET & POST requests
>> so that any thing which is empty (string) gets converted/replaced
>> my perl's undef (and DBIC's NULL).
>>
>> Why i need that:
>> 
>> I have edit form in my web app and some fields in the edit form
>> are prefilled with nothing. On submission the DB raises an exception
>> that an empty string is being inserted to a integer field.
>>
>> Hence I feel like having a global filter that turns all blanks to
>> undef.
>>
>> The question is where to put this global filter.
>>
>> Regds
>> mallah.
>>
>>
>>
>>
>> ___
>> List: Catalyst@lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
>
> ___
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>





___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] transforming empty values in request to perl's undef

2017-03-02 Thread Gil Magno
Hi

Are you by any chance using HTML::FormFu? If so, check the "ignore_if_empty" 
option.

https://metacpan.org/pod/HTML::FormFu::Model::DBIC#Config-options-for-fields

If not, could you please show the code with which you're "receiving" the params 
and inserting into the DB?

Just curious: when you say "prefilled with nothing", you mean the field is 
empty, or the field is filled with one or more white spaces (like " "), or the 
field is a  with the first  having the attribute "value" equals 
to ""? Or something else?

gil

On Thu, Mar 02, 2017 at 10:25:27AM -0500, Rajesh Kumar Mallah wrote:
> 
> Hi ,
> 
> I want to transform request parameters in GET & POST requests
> so that any thing which is empty (string) gets converted/replaced
> my perl's undef (and DBIC's NULL).
> 
> Why i need that:
> 
> I have edit form in my web app and some fields in the edit form
> are prefilled with nothing. On submission the DB raises an exception
> that an empty string is being inserted to a integer field.
> 
> Hence I feel like having a global filter that turns all blanks to
> undef.
> 
> The question is where to put this global filter.
> 
> Regds
> mallah.
> 
> 
> 
> 
> ___
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/

___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


[Catalyst] transforming empty values in request to perl's undef

2017-03-02 Thread Rajesh Kumar Mallah

Hi ,

I want to transform request parameters in GET & POST requests
so that any thing which is empty (string) gets converted/replaced
my perl's undef (and DBIC's NULL).

Why i need that:

I have edit form in my web app and some fields in the edit form
are prefilled with nothing. On submission the DB raises an exception
that an empty string is being inserted to a integer field.

Hence I feel like having a global filter that turns all blanks to
undef.

The question is where to put this global filter.

Regds
mallah.




___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/