remove die as it stops actual use cases
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
index 8374e91..49cd04f 100644 (file)
@@ -42,9 +42,9 @@ sub set_attribute {
 sub _parse_attribute_args {
   my $self = shift;
 
-  die "Long form arg (name => 'class', value => 'x') is no longer supported"
-    if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
-    
+  #die "Long form arg (name => 'class', value => 'x') is no longer supported"
+    #if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
+
   my $opts = ref($_[0]) eq 'HASH' ? $_[0] : {$_[0] => $_[1]};
   for (values %{$opts}) { $self->_zconfig->parser->html_escape($_); }
   return $opts;
@@ -366,6 +366,84 @@ sub repeat_content {
   $self->repeat($repeat_for, { %{$options||{}}, content => 1 })
 }
 
+sub extract_names {
+  my ($self, $to) = @_;
+  sub {
+    my ($evt) = @_;
+    push @$to, $evt->{'attrs'}->{'name'};
+    $evt;
+  }
+};
+
+sub validate_form {
+  my ($self,$to) = @_;
+  $self->collect({ 
+    filter => sub {
+      return 
+        $_->select('input')->validation_rules($to)
+        ->select('select')->validation_rules($to);
+    },
+    passthrough => 1,
+  });
+}
+
+sub fill_form {
+  my ($self,$val) = @_;
+  $self->collect({ 
+    filter => sub {
+      return 
+        $_->select('input')->val($val)
+        #->select('select')->val($val)
+        ;
+    },
+    passthrough => 1,
+  });
+}
+
+sub validation_rules {
+  my ($self, $to) = @_;
+  sub {
+    my ($evt) = @_;
+    $to->{$evt->{'attrs'}->{'name'}} 
+      = [split ' ', $evt->{'attrs'}->{'data-validate'}||""];
+    $evt;
+  }
+}
+
+sub val {
+  #if val is a hashref automatically match to name, otherwise fill as is.
+  my ($self, $val) = @_;
+  sub {
+    my ($evt) = @_;
+    my $attrs = $evt->{'attrs'};
+    my $nm = $attrs->{'name'};
+    my $tar = defined $val && ref $val eq 'HASH' ? $val->{$nm} : $val;
+    if(defined $tar) {
+      if($evt->{'name'} eq 'select') {
+        #if we are select do something more complicated
+        warn "Can't do selects yet";
+      } else {
+        $evt->{'raw'} = undef;
+        $evt->{'raw_attrs'} = undef;
+        push @{$evt->{'attr_names'}}, 'value' unless exists $attrs->{'value'};
+        $attrs->{'value'} = $tar;
+        #check if we are a checkbox
+        if(exists $attrs->{'type'} && $attrs->{'type'} eq 'checkbox') {
+          if($tar) {
+            push @{$evt->{'attr_names'}}, 'selected' unless exists $attrs->{'selected'};
+            $attrs->{'selected'} = $tar ? 'selected' : '';
+          } else {
+            delete $attrs->{'selected'};
+            $evt->{'attr_names'} = [ grep $_ ne 'selected', @{$evt->{'attr_names'}} ];
+          }
+        }
+      }
+    }
+    $evt;
+  }
+}
+
+
 1;
 
 =head1 NAME