Welcome Guest, Not a member yet? Register   Sign In
Validation for Dynamically Added Fields
#1

[eluser]BrandonDurham[/eluser]
I've got a form where the user can dynamically add extra fields with javascript and I'd like to be able to validate those fields, if possible. Has this been discussed before? Any ideas?

If it helps they'll all have similar names, like track_1, track_2, etc.

Thanks!
#2

[eluser]xwero[/eluser]
you could do something like
Code:
$postkeys = array_keys($_POST);
foreach($postkeys as $key)
{
if(preg_match('/^track/',$key)){ $rules[$key] = 'required'; }
}
#3

[eluser]BrandonDurham[/eluser]
I'll give that a shot. Thank you!
#4

[eluser]Pygon[/eluser]
alternatively, you could pass the keys by array --

instead of track_1, track_2, you can use track[] -- this will return an array of values:

Example Only:
Code:
<form method="post" action="processForm.php">
<input type="text" name="track[]" />
<input type="text" name="track[]" />
<input type="text" name="track[]" />
<input type="submit" value="Submit" />
</form>

and

Code:
$aTrack = $this->input->post('track');
foreach ($aTrack as $value)
{
//Code here.
}
#5

[eluser]Paulo Eduardo[/eluser]
Pygon,

If i use a array like you say how i do to repopulate the form if some data as posted wrong?

`cuz i try something like that:
Code:
$this->validation->username[0]
and they didn`t work

if have you a solution for that i will be Grateful

Thanks a lot
Paulo Eduardo
#6

[eluser]Pygon[/eluser]
I can't give you exact code at the moment, since I'm at home, but I would suggest that you print_r your array to see how it is populated (and whether you've correctly accessed the array).
#7

[eluser]Symcrapico[/eluser]
[quote author="Paulo Eduardo" date="1194689333"]Pygon,

If i use a array like you say how i do to repopulate the form if some data as posted wrong?

`cuz i try something like that:
Code:
$this->validation->username[0]
and they didn`t work

if have you a solution for that i will be Grateful

Thanks a lot
Paulo Eduardo[/quote]

Anyone has a head up on this? Im facing the same problem and I have no idea on how to repopulate my form.

Here my simplified form:

Code:
<div id="formJoueur" class="formJoueur">
    <h2 class="txtinsc">Informations Joueurs <span id="no_joueur">1</span></h2>
    <label>Nom et prénom :</label>&lt;input name="nomJoueur[]" value="" maxlength="60" size="65" type="text"&gt;
    <br>
    <label>No. Chandail :</label>&lt;input name="noChandail[]" value="" maxlength="3" size="3" type="text"&gt;
    <br>
    <label>No. passeport :</label>&lt;input name="noPasseport[]" value="" maxlength="12" size="15" type="text"&gt;
    <br>
    <hr>
</div>
                
<div class="formJoueur">
    <h2 class="txtinsc">Informations Joueurs <span id="no_joueur">2</span></h2>
    <label>Nom et prénom :</label>&lt;input name="nomJoueur[]" value="" maxlength="60" size="65" type="text"&gt;
    <br>
    <label>No. Chandail :</label>&lt;input name="noChandail[]" value="" maxlength="3" size="3" type="text"&gt;
    <br>
    <label>No. passeport :</label>&lt;input name="noPasseport[]" value="" maxlength="12" size="15" type="text"&gt;
    <br>
    <hr>
</div>

As you can see, I have 3 dynamic fields that I need to validate and repopulate, nomJoueur[], noChandail[] and noPasseport[].

I cant wait to see a solution for that!

Thanks
#8

[eluser]Michael Ekoka[/eluser]
The native library doesn't support these features. I've tried to fix this for myself and posted my solution here.
#9

[eluser]eggshape[/eluser]
Hi:

View file:
Code:
// makes array from data retrieved via database and passed to view
&lt;?php if (!empty($coverage_level) and is_string($coverage_level)) $coverage_level = explode(',', $coverage_level);
else $coverage_level = array(); ?&gt;
// form elements
<fieldset>
<ul>
<li><label for="coverage_level">Media Coverage Level <span class="g"> * </span></label></li>
<li><span><em>Check all that apply</em></span></li>
<li>&lt;input tabindex="1" id="coverage_level[]" name="coverage_level[]" type="checkbox" value="none" &lt;?php if (in_array('none', $coverage_level)) echo 'checked="checked"'; else echo $this-&gt;validation->set_checkbox('coverage_level', 'none'); ?&gt; /> None</li>
<li>&lt;input tabindex="2" id="coverage_level[]" name="coverage_level[]" type="checkbox" value="national" &lt;?php if (in_array('national', $coverage_level)) echo 'checked="checked"'; else echo $this-&gt;validation->set_checkbox('coverage_level', 'national'); ?&gt; /> National</li>
<li>&lt;input tabindex="3" id="coverage_level[]" name="coverage_level[]" type="checkbox" value="regional" &lt;?php if (in_array('regional', $coverage_level)) echo 'checked="checked"'; else echo $this-&gt;validation->set_checkbox('coverage_level', 'regional'); ?&gt; ?&gt; /> Regional</li>
<li>&lt;input tabindex="4" id="coverage_level[]" name="coverage_level[]" type="checkbox" value="local" &lt;?php if (in_array('local', $coverage_level)) echo 'checked="checked"'; else echo $this-&gt;validation->set_checkbox('coverage_level', 'local'); ?&gt; /> Local</li>
&lt;?php if (isset($this->validation->coverage_level_error)) : ?&gt;
<li class="error">&lt;?php echo $this->validation->coverage_level_error; ?&gt;</li>
&lt;?php endif; ?&gt;
</ul>
</fieldset>

Controller:
Code:
public function validate()
{
        if (!empty($_POST['coverage_level']))
        {
            /* Can't have 'none' and any other selection;
               if user does select 'none' and something else, set the validation
               error and generate the view again (return NULL to exit the method and
               prevent submit().
            */
            if (in_array('none', $_POST['coverage_level']) and count($_POST['coverage_level']) > 1)
            {
                $this->validation->coverage_level_error = 'Planned Media Coverage is invalid';
                $this->index();
                return;
            }
            // flatten array for DB insertion; you can tell this DB is not completely atomic =)
            $_POST['coverage_level'] = implode(',', $_POST['coverage_level']);
        }
        $this->submit();
}

public function submit()
{ /* submit method contains code snippet: $this->validation->run() to handle the other fields and rules */}

That works for me. I use this strategy when I want to validate dynamic or *special* fields on the fly, depending on the choices the user makes or does not make. It basically involves running your own validate() method which sets the custom validation rules and then run the CI validate function on submit.

EDIT: I should be clear that the form's action is "controller/validate"...validate() is called on form submission and not submit(). And I use the default validation file for 1.5.4




Theme © iAndrew 2016 - Forum software by © MyBB