Welcome Guest, Not a member yet? Register   Sign In
How to pass dynamical field names to validation object?
#1

[eluser]Skuja[/eluser]
I am making a form that has dynamicaly generated input fields. These fields are names in each language that has been created, it looks like something like this:
Code:
//$languages variable stores a query where i get data from db
<?foreach($languages->result() as $language):?>
<?$name = $language->code.'_name';?>
field name in &lt;?=$language->name?&gt; language: &lt;input type="text" name="&lt;?=$name?&gt;" &lt;?=$this-&gt;validation->$name;?&gt;><br>
&lt;?endforeach;?&gt;
ofcourse
Code:
&lt;?=$this->validation->$name;?&gt;
causes an error, so i'm stuck and can't figure out any way how to deal with this problem.
#2

[eluser]SitesByJoe[/eluser]
Here's what you need to do:

Create a variable, named "$fields" (or something like that) and then create a conditional check that either dumps your database->get() into the $fields variable, or your validation object into the $fields variable.

That way you can use the same variables in your form view.

Hopefully that makes sense. I'm just finally figuring the validation/dynamic data in forms myself.
#3

[eluser]Pygon[/eluser]
Your question is a bit confusing.

assuming that you correctly trying to call it:
Code:
&lt;?=$this->validation->{$name};?&gt;
would work.
#4

[eluser]Joey Marchy[/eluser]
Pygon's solution worked for me too. Example:

Code:
&lt;?php
for ($i=1; $i <= $how_many; $i++)
{
$answer = 'answer'.$i;
?&gt;
<dt><label for="answer&lt;?php echo $i; ?&gt;">Answer &lt;?php echo $i; ?&gt;</label></dt>
<dd>
&lt;input type="text" name="answer&lt;?php echo $i; ?&gt;"  value="&lt;?= $this-&gt;validation->{$answer}; ?&gt;" />
</dd>
&lt;?php  }     ?&gt;

This also works
Code:
&lt;?= $this->validation->$answer; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB