next up previous contents
Next: Multiple Values Up: Sample Code Previous: Embedded Logic   Contents


MICdo

Consider the following code snippets:

from employee_entry.fs:

...
<MICgroup name="employees" obj="$obj"
 numvar="$index" numdisplay="10" dummyok="true">

  <Micdefine name="eid" data="employee_id"
   type="text">
   get_employee_id($index);
  </MICdefine>

  <MICdefine name="name" data="text"
   type="text">
   get_employee_name($index);
  </MICdefine>

  <MICdefine name="ssn" data="ssn" type="text">
   get_employee_ssn($index);
  </MICdefine>

  <MICdefine name="salary" data="uscurrency"
   type="text">
   get_employee_salary($index);
  </MICdefine>
</MICgroup>
...
*******
from enter.html:
...
<form action="overridden" 
 MICfieldspace="employee_entry">

<MICdynamicdisplay>

<hr>

Name: <MICdisplay name="name">
SSN: <MICdisplay name="ssn"><br>
Salary: <MICdisplay name="salary><br>

</MICdynamicdisplay>

<MICrollback name="entry_submit"
 assocdynamic="employees">
 <vinstatiated>
</MICrollback>

<MICrollover name="entry_submit"
 assocdynamic="employees">
 <vinstantiated>
</MICrollover>

<br>

<MICsubmit name="entry_submit" value="Submit"
 goto="process">
 <vinstantiated>
</MICsubmit>
<input type="submit" name="entry_submit"
 value="Submit">

</form>
...
*******
from process.html:
...
<MICPerl>
my %new_employees;
</MICPerl>

<form action="none" MICfieldspace="employee_entry">

<MICdo name="eid" numvar="$index">
  $new_employees{$index}-&gt;{id}-&gt;{$field-&gt;value()};
</MICdo>

<MICdo name="name" numvar="$index">
  $new_employees{$index}-&gt;{name}-&gt;{$field-&gt;value()};
</MICdo>

<MICdo name="ssn" numvar="$index">
  $new_employees{$index}-&gt;{ssn}-&gt;{$field-&gt;value()};
</MICdo>

<MICdo name="salary" numvar="$index">
  $new_employees{$index}-&gt;{salary}-&gt;{$field-&gt;value()};
</MICdo>

</form>

<MIClist iterator="$index" list="keys %new_employees">
<MICPerl>
  my $new_id;

  if ($new_employees{$index}-&gt;{id}) {
    $new_id = new_employee_id();
	
    $new_employees{$index}-&gt;{id}-&gt;{$new_id};
    add_employee($new_employees{$index});
  }
  else {
    update_employee($new_employees{$index});
  }
</MICPerl>
</MIClist>
...
*******
The above code is meant to take new employee data and store it in a database as well as display and update existing employee data. The default code blocks will fill in the first n employees (where n is the number of existing employees). Assume that the default code block returns null when we go off the first n employees, leaving blank text boxes.

Let us assume that we have strict error checking on all the data so that we know when we get to the processing page, it is all good data. That is, assume there is a lot of error checking and reporting that is not shown. Part of this error checking is to ensure that if one field is filled out, then the other two are.

Note that because of the instantiation rules of MIC, to change this form from only adding new employees, to adding and updating means that we only need to add 6 lines in the fieldspace file and 6 in the process.html (all of which are shown). When we get to the process page, we blindly load up everything that is there, only noting the state of the eid field at the very end. This is okay, since none of the blank fields or unchanged fields for existing customers will be instantiated, so all our data on the processing page are interesting.

So, we then MICdo over the fields, building up a hash, which is then passed into the add_employee function, which takes the information found in the hash reference and adds it to database or the update_employee function.


next up previous contents
Next: Multiple Values Up: Sample Code Previous: Embedded Logic   Contents