Welcome Guest, Not a member yet? Register   Sign In
form validation for a set of fields
#1

[eluser]tirso[/eluser]
hi to all

I hav a set of fields to validate. here is the scenario

let say I have fields shown below, since name are the same I put a number and add at the end.

job_title_1
company_name_1
responsibilities_1

job_title_2
company_name_2
responsibilities_2

job_title_3
company_name_3
responsibilities_3

In my view, I use a loop to assign this number at the end of the field name then pass the variable "ctr = 3" in my controller

I use validation like this

Code:
$validation_errors = array();
for ($i = 1; $i <= $ctr; $i++)
{
    $this->form_validation->set_rules('career_objectives_' . $i, 'Career objectives title', 'trim|required');                                                                                    
    $this->form_validation->set_rules('job_title_' . $i, 'Job title / position', 'trim|required');
    $this->form_validation->set_rules('company_name_' . $i, 'Company', 'trim|required');    
    $this->form_validation->set_rules('from_date_employment_' . $i, 'From date of employment', 'trim|required');            
    $this->form_validation->set_rules('to_date_employment_' . $i, 'To date of employment', 'trim|required');    
    $this->form_validation->set_rules('responsibilities_' . $i, 'Responsibilities type', 'trim|required');
    $this->form_validation->set_error_delimiters('<div class="valid-err">', '</div>');                    
    
    if ($this->form_validation->run() == FALSE)
    {
        $validation_errors[$i] = validation_errors();
    }                
}


I decided to put the validation_errors in an array so that I could get this in my view and place the string error in a separate location. My problem now is even the job_title_1 only has error all the job_title from 1 to 3 produced an error. I think validation_errors() was not clear to the next loop.

Any help would greatly appreciated

Thanks

Tirso
#2

[eluser]saidai jagan[/eluser]
U can use like this
Code:
for($i=0; $i<$opponents; $i++ )
{
   $rules["name,$i"]    =     'trim|required|';
   $rules["age,$i"]    =     'trim|required|';
}
$this->get_instance()->validation->set_rules($rules);
for($i=0; $i<$opponents; $i++ )
{
   $fields["name,$i"]    = 'Name'.($i+1);
   $fields["age,$i"]    = 'Age'.($i+1);
}
$this->get_instance()->validation->set_fields($fields);
#3

[eluser]tirso[/eluser]
hi,

still the same output, Any solutions there.

Thanks
#4

[eluser]puzzlebox[/eluser]
why not do this:

Code:
$validation_errors = '';
for ($i = 1; $i <= $ctr; $i++)
{
    $this->form_validation->set_rules('career_objectives_' . $i, 'Career objectives title', 'trim|required');                                                                                    
    $this->form_validation->set_rules('job_title_' . $i, 'Job title / position', 'trim|required');
    $this->form_validation->set_rules('company_name_' . $i, 'Company', 'trim|required');    
    $this->form_validation->set_rules('from_date_employment_' . $i, 'From date of employment', 'trim|required');            
    $this->form_validation->set_rules('to_date_employment_' . $i, 'To date of employment', 'trim|required');    
    $this->form_validation->set_rules('responsibilities_' . $i, 'Responsibilities type', 'trim|required');
    $this->form_validation->set_error_delimiters('<div class="valid-err">', '</div>');                    
}    
    if ($this->form_validation->run() == FALSE)
    {
        $validation_errors = validation_errors();
    }

I mean, do you need to put the errors in an array?
#5

[eluser]tirso[/eluser]
hi puzzlebox,

I want to put the error in array because those set of fields in a different fieldset in my view

The ouput in my view should be like below. But I will use also (for statement) base on the $ctr as counter and add the number at the end of the fieldname.

Is this possible?

Thank you.

set1

Code:
<fieldset>
    <div style="padding:5px 0 10px 0">&lt;?php echo $validation_errors[0]; ?&gt;</div>            
    <ul class="reg-info">
        <li><span style="float:right">&lt;textarea name="career_objectives"&gt;&lt;?php echo set_value('career_objectives') ?&gt;&lt;/textarea&gt;&lt;/span><label style="vertical-align:top;float:right"><span class="required">* </span>Career Objectives:</label></li>
        <div style="clear:right"></div>            
        <li><label><span class="required">* </span>Job title / Position</label>&lt;input name="job_title" type="text" value="&lt;?php echo set_value('job_title') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>Company Name</label>&lt;input name="company_name" type="text" value="&lt;?php echo set_value('company_name') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>Date of Employment</label>&lt;input name="from_date_employment" type="text" id="date-pick-from" value="&lt;?php echo set_value('from_date_employment') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>To Date</label>&lt;input name="to_date_employment" type="text" id="date-pick-to" value="&lt;?php echo set_value('to_date_employment') ?&gt;"&gt;&lt;/li>                
        <li><span style="float:right">&lt;textarea name="responsibilities"&gt;&lt;?php echo set_value('responsibilities') ?&gt;&lt;/textarea&gt;&lt;/span><label style="vertical-align:top; float:right"><span class="required">* </span>Responsibilities / Achievements:</label></li>
        <div style="clear:right"></div>
     </ul>
</fieldset>

set2

Code:
<fieldset>
    <div style="padding:5px 0 10px 0">&lt;?php echo $validation_errors[1]; ?&gt;</div>            
    <ul class="reg-info">
        <li><span style="float:right">&lt;textarea name="career_objectives"&gt;&lt;?php echo set_value('career_objectives') ?&gt;&lt;/textarea&gt;&lt;/span><label style="vertical-align:top;float:right"><span class="required">* </span>Career Objectives:</label></li>
        <div style="clear:right"></div>            
        <li><label><span class="required">* </span>Job title / Position</label>&lt;input name="job_title" type="text" value="&lt;?php echo set_value('job_title') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>Company Name</label>&lt;input name="company_name" type="text" value="&lt;?php echo set_value('company_name') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>Date of Employment</label>&lt;input name="from_date_employment" type="text" id="date-pick-from" value="&lt;?php echo set_value('from_date_employment') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>To Date</label>&lt;input name="to_date_employment" type="text" id="date-pick-to" value="&lt;?php echo set_value('to_date_employment') ?&gt;"&gt;&lt;/li>                
        <li><span style="float:right">&lt;textarea name="responsibilities"&gt;&lt;?php echo set_value('responsibilities') ?&gt;&lt;/textarea&gt;&lt;/span><label style="vertical-align:top; float:right"><span class="required">* </span>Responsibilities / Achievements:</label></li>
        <div style="clear:right"></div>
     </ul>
</fieldset>

set3

Code:
<fieldset>
    <div style="padding:5px 0 10px 0">&lt;?php echo $validation_errors[2]; ?&gt;</div>            
    <ul class="reg-info">
        <li><span style="float:right">&lt;textarea name="career_objectives"&gt;&lt;?php echo set_value('career_objectives') ?&gt;&lt;/textarea&gt;&lt;/span><label style="vertical-align:top;float:right"><span class="required">* </span>Career Objectives:</label></li>
        <div style="clear:right"></div>            
        <li><label><span class="required">* </span>Job title / Position</label>&lt;input name="job_title" type="text" value="&lt;?php echo set_value('job_title') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>Company Name</label>&lt;input name="company_name" type="text" value="&lt;?php echo set_value('company_name') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>Date of Employment</label>&lt;input name="from_date_employment" type="text" id="date-pick-from" value="&lt;?php echo set_value('from_date_employment') ?&gt;"&gt;&lt;/li>
        <li><label><span class="required">* </span>To Date</label>&lt;input name="to_date_employment" type="text" id="date-pick-to" value="&lt;?php echo set_value('to_date_employment') ?&gt;"&gt;&lt;/li>                
        <li><span style="float:right">&lt;textarea name="responsibilities"&gt;&lt;?php echo set_value('responsibilities') ?&gt;&lt;/textarea&gt;&lt;/span><label style="vertical-align:top; float:right"><span class="required">* </span>Responsibilities / Achievements:</label></li>
        <div style="clear:right"></div>
     </ul>
</fieldset>




Theme © iAndrew 2016 - Forum software by © MyBB