Welcome Guest, Not a member yet? Register   Sign In
Can I write a script that will show me all the fields that are required
#1

[eluser]justmelat[/eluser]
We have a codeigniter app 1.7.2, client can dynamically create their web form. it's based on sections/fieldsets and fields within those sections. Via javascript we hide/show sections based on selection from a particular dropdown.

The issue is, CI's validation code is great for sections/fieldsets that are always in view. But required fields can't be in sections that are not being displayed because obviously they would not be displayed in order to modify.

Is there a way to use the form_validation class or an php script to view all the fields tagged as required? If I can get a script/array to hold all the required fields, perhaps we can use that and javascript to add another layer of validation when the form is submitted.

Example code below [just a quick web form so you will have a better idea of what I am trying to explain]

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;

&lt;head&gt;
&lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type" /&gt;
&lt;title&gt;Fruits&lt;/title&gt;

[removed]
function callOnLoad()
{
var projectType_Field_Id = 'theType';

var projectTypes = new Array (
{id : 'fs1', value:'Fruits'},
{id : 'fs2', value:'Cars'},
{id : 'fs3', value:'States'});
      
var project_type = document.getElementById(projectType_Field_Id);
refreshSections(project_type);

}

function refreshSections(selectedType)
{
var projectTypes = new Array (
{id : 'fs1', value:'Fruits'},
{id : 'fs2', value:'Cars'},
{id : 'fs3', value:'States'});

for (var i = 0; i < projectTypes.length; i++)
{
  if (projectTypes[i].value == selectedType)
  {
   document.getElementById(projectTypes[i].id).style.display = '';
  } else {
   document.getElementById(projectTypes[i].id).style.display = 'none';
  }
}
  
}
function projectTypeChange()
{

  var projectType_Field_Id = 'theType';
  
var projectTypes = new Array (
{id : 'fs1', value:'Fruits'},
{id : 'fs2', value:'Cars'},
{id : 'fs3', value:'States'});

var project_type = document.getElementById(projectType_Field_Id).value;
refreshSections(project_type);
}


[removed]
&lt;/head&gt;

&lt;body&gt;
&lt;form method="post" action=""&gt;
<table>
<tr><td>
<fieldset name="fs_geninfo1" id="fs_genInfo1">
    <legend>General Project Information</legend>
    
    <table>
     <tr>
      <td><label id="Label1">Date</label></td>
      <td>&lt;input name="Text1" type="text" /&gt;&lt;/td>
     </tr>
     <tr>
      <td><label id="Label1">Time</label></td>
      <td>&lt;input name="Text1" type="text" /&gt;&lt;/td>
     </tr>

     <tr>
      <td><label id="Label2">Approvers</label></td>
      <td>&lt;textarea cols="20" name="TextArea1" rows="2"&gt;&lt;/textarea></td>
     </tr>
     <tr>
<td>Project Type:</td><td><select name="Select1" id="theType">
    <option value="0" selected="selected">Select</option>
    <option value="Fruits">Fruits</option>
    <option value="Cars">Cars</option>
    <option value="States">States</option>
   </select>
</td></tr>
  </table>
    
   </fieldset>
   </td>
   </tr>

<tr><td>
<fieldset name="fs1" id="fs1">
    <legend>Fruits</legend>
    
    <table>
     <tr>
      <td><label id="Label1">Name</label></td>
      <td>&lt;input name="Text1" type="text" /&gt;&lt;/td>
     </tr>
     <tr>
      <td><label id="Label2">Description</label></td>
      <td>&lt;textarea cols="20" name="TextArea1" rows="2"&gt;&lt;/textarea></td>
     </tr>
    </table>
    
   </fieldset>
   </td>
   </tr>
   <tr>
   <td>
   <fieldset name="fs2" id="fs2">
    <legend >Cars</legend>
    <table>
     <tr>
      <td><label id="Label1">Name</label></td>
      <td>&lt;input name="Text1" type="text" /&gt;&lt;/td>
     </tr>
     <tr>
      <td><label id="Label2">Description</label></td>
      <td>&lt;textarea cols="20" name="TextArea1" rows="2"&gt;&lt;/textarea></td>
     </tr>
    </table>

   </fieldset>
   </td></tr>
   <tr><td>
   <fieldset name="fs3" id="fs3">
    <legend >States</legend>
    <table>
     <tr>
      <td><label id="Label1">Name</label></td>
      <td>&lt;input name="Text1" type="text" /&gt;&lt;/td>
     </tr>
     <tr>
      <td><label id="Label2">Description</label></td>
      <td>&lt;textarea cols="20" name="TextArea1" rows="2"&gt;&lt;/textarea></td>
     </tr>
    </table>

   </fieldset>
   </td></tr>
   <tr><td>


&lt;input name="Button1" type="button" value="Submit" /&gt;
</td></tr>
</table>
&lt;/form&gt;
&lt;/body&gt;

&lt;/html&gt;




Theme © iAndrew 2016 - Forum software by © MyBB