Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

SmartyValidate - problems with form array validation

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
jhiza
Smarty Rookie


Joined: 04 Nov 2006
Posts: 6

PostPosted: Thu Aug 28, 2008 6:04 am    Post subject: SmartyValidate - problems with form array validation Reply with quote

I have a form I'm trying to submit that has the usual required data on it, of which I have the validation behaving correctly. There is additional data in the form that is array based. For instance it is an array of "time data". More or less I have broken out each component of (hours, minutes, seconds, etc) into their own form elements. I am trying to build custom validation that only validates a row of the array if anything is entered, otherwise it passes.

Example of form elements:
0[hh] 0[mm] 0[ss]
1[hh] 1[mm] 1[ss]
2[hh] 2[mm] 2]ss]
..etc

What I'm trying to accomplish is: If all of the 0 array elements are "empty" then the validation passes. If there is a single piece of data filled in ANY of array 1, but all of array 1 is not populated, that the validation fail.

The problems I'm running into are two fold. First, I cannot get the form parameters to display in smarty via their respective retrieval from the form submit (get/post)
Code:

<input type="text" name="{$smarty.section.laptimes.iteration}[laphh]" value="{$smarty.section.laptimes.iteration[laphh]}" class="colhead" style="width: 40px" maxlength="2" size="2">


Secondly, I have coded a custom validate method. Is it calling the method for *each* of the 3 array elements (hh,mm,ss)? I'm trying to pass the actually array in as the value, so that I can do my custom logic within my validate method to determine whether or not to return true or false, but it appears to "automatically" call it individually for EACH of the array elements for a given row (0 for example)

Code:
      SmartyValidate::register_form('myform');
      for ($i = 1; $i <= $laps; $i++){
         //$time is an empty TIME object, which is where the isLapValid method lives
         SmartyValidate::register_object('Time', $time);
         SmartyValidate::register_criteria('isTimeValid','Time->isLapValid');
      }


which currently is just a stub :
Code:

   public function isLapValid($value, $empty, &$params, &$formvars) {
      print_r($value);
      #print_r($empty);
      #print_r($params);
      #print_r($formvars);
      return true;
   }


I have thought about implementing the form array in two models:
0[hh] 0[mm] 0[ss]
1[hh] 1[mm] 1[ss]
2[hh] 2[mm] 2]ss]


OR

hh[0] mm[0] ss[0]
hh[1] mm[1] ss[1]
hh[2] mm[2] ss[2]

(I assumed this one would be more difficult for doing the form validation I was shooting for).

Like I said the issues are:
cannot retrieve the value from the form submit to display in smarty
cannot seem to get validation to work in my desired method.

Ideas? I can post more snippets if necessary.

TIA
Back to top
View user's profile Send private message
jhiza
Smarty Rookie


Joined: 04 Nov 2006
Posts: 6

PostPosted: Thu Aug 28, 2008 7:15 pm    Post subject: Reply with quote

ok, update. I found part of what I was missing, which is the @ within my register_validator.

new code that works with the numeric index based html input array:
Code:

         for ($i = 1; $i <= $track_data['setr_laps']; $i++){
            SmartyValidate::register_validator('lapValid', $i, '@isLapValid');
            #SmartyValidate::register_validator('lapValid', 'laphh', '@isLapValid');
            #SmartyValidate::register_validator('lapValid', 'lapmm', '@isLapValid');
            #SmartyValidate::register_validator('lapValid', 'lapss', '@isLapValid');
            #SmartyValidate::register_validator('lapValid', 'lapsss', '@isLapValid');
         }


however, It only appears to be attempting to validate the LAST element in the array:
Code:

      // validate after a POST
      SmartyValidate::connect($smarty);

      SmartyValidate::register_form('myform');

      for ($i = 1; $i <= $track_data['setr_laps']; $i++){
         print_r($i);
         SmartyValidate::register_object('Rank', $rank);
         SmartyValidate::register_criteria('isLapValid','Rank->isLapValid');
      }


also, any idea on why my form is not retaining values?
Code:

  {section name=laptimes start=0 loop=$track_data.setr_laps step=1}
        <input type="text" name="{$smarty.section.laptimes.iteration}[laphh]" value="{$smarty.section.laptimes.iteration[laphh]}" class="colhead" style="width: 40px" maxlength="2" size="2">
{/section}
Back to top
View user's profile Send private message
jhiza
Smarty Rookie


Joined: 04 Nov 2006
Posts: 6

PostPosted: Thu Aug 28, 2008 9:21 pm    Post subject: Reply with quote

ok, got my form values to carry over from the previous request:
Code:
        <input type="text" name="{$smarty.section.laptimes.iteration}[laphh]" value="{$smarty.post.$foo.laphh}" class="colhead" style="width: 40px" maxlength="2" size="2">


It appears I had to get it specifically from the smarty post reference.

The only piece I have missing is the fact that the validator appears to only be validating the *last* array row. i have 3 rows, and it appears to only be attempting to validate row #3 at this point, not 1 & 2.
Back to top
View user's profile Send private message
jhiza
Smarty Rookie


Joined: 04 Nov 2006
Posts: 6

PostPosted: Thu Aug 28, 2008 10:01 pm    Post subject: Reply with quote

it appears i didn't register each validator appropriately:

Code:

        for ($i = 1; $i <= $track_data['setr_laps']; $i++){
            SmartyValidate::register_validator('lapValid'.$i, $i, '@isLapValid');
         }


notice how i appended the $i onto the lapValid validation registration. That was the problem. Now I just have to figure out the easiest way not to reinvent the wheel with validation. i'm looking to do dependant validation, but wonder if there is a way i can reuse components of existing modules for validation.

basically, i'd like to know if i can re-use the following validations in conjunction with stringing them into dependancy checking:
Code:

      SmartyValidate::register_validator('overallss', 'overallss', 'notEmpty', false, false, 'trim');
      SmartyValidate::register_validator('overallssint', 'overallss', 'isInt', false, false, 'trim');
      SmartyValidate::register_validator('overallsslength', 'overallss:2:2', 'isLength', false, false, 'trim');
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP