Welcome Guest, Not a member yet? Register   Sign In
How do I preload a form?
#1

[eluser]marlar[/eluser]
Hi,

Once again, the most simple task has put me down. Everything is so simple in plain PHP. I hope I will soon see the light :down:

When using the validation library I can get my form re-populated automatically by using code like:
Code:
<input name="name" value="<?=$this->validation->name?>">
and then in the controller:
Code:
$fields['name'] = "name";
$this->validation->set_fields($fields);

But what if I want to have the form initially loaded with some values? How do I do that?

Thanks,
Martin
#2

[eluser]Rick Jolly[/eluser]
You could prepopulate the validation class with default values in the controller. You'd do that if the page hadn't been posted yet.
Code:
$this->validation->name = $some_default;
#3

[eluser]marlar[/eluser]
Hi,

Thanks. I just figured that out a minute or so. Wish the manual had mentioned it!

I still have a problem with the checkboxes. How do I check these according to their values?

You are right that I talk about the page before it has been posted, like when you edit a user profile etc.

Martin
#4

[eluser]wiredesignz[/eluser]
... from the User Guide:

Code:
//in your view (untested)
<input type="checkbox" name="mycheck" value="1" <?= $this->validation->set_checkbox('mycheck', '1'); ?> />

//form continues...

to pre-load values into the validation object(after set_fields()):

Code:
//in your controller
$this->validation->mycheck = 1;
#5

[eluser]adamp1[/eluser]
Is that a 'legal' way to do it, set the values in the validation class? I normally have an if statement for each field and as you can imagine it gets quite long and messy.
#6

[eluser]wiredesignz[/eluser]
The validation class variables are public, so it has to be ok. (After set_fields() has loaded them of course)
#7

[eluser]marlar[/eluser]
[quote author="wiredesignz" date="1201411812"]
to pre-load values into the validation object(after set_fields()):

Code:
//in your controller
$this->validation->mycheck = 1;
[/quote]

That is what I tried, but it doesn't work. The validation library is too limited.

However, I have modified it so it works now - also for checkbox arrays!

Code:
function set_checkbox($field = '', $value = '')
    {
        if ($field == '' OR $value == '')
        {
            return '';
        }
        if(isset($_POST[$field])) {
            if ($_POST[$field] == $value || ( is_array($_POST[$field]) && in_array($value, $_POST[$field])) )
            {
                return ' checked="checked"';
            }
        }
        elseif(isset($this->$field)) {
            if($this->$field==$value || ( is_array($this->$field) && array_search($value, $this->$field)!==FALSE) ) {
                return ' checked="checked"';
            }
        }
    }

In the controller:

Code:
// for a single checkbox just use the checkbox' value:
$this->validation->mycheckbox = 1;
$this->validation->mycar = "big";

// for a checkbox group, use an array with the values that should be checked:
$this->validation->mycolors = array("red","blue","yellow");
In the view:
Code:
<input type="CHECKBOX" name="mycheckbox" value="1" <?=$this->validation->set_checkbox('mycheckbox', '1')?>>
<input type="CHECKBOX" name="mycar" value="big" <?=$this->validation->set_checkbox('mycar', 'big')?>>
<input type="CHECKBOX" name="mycolors[]" value="red" <?=$this->validation->set_checkbox('mycolors', 'red')?>>
<input type="CHECKBOX" name="mycolors[]" value="blue" <?=$this->validation->set_checkbox('mycolors', 'blue')?>>
<input type="CHECKBOX" name="mycolors[]" value="green" <?=$this->validation->set_checkbox('mycolors', 'green')?>>
<input type="CHECKBOX" name="mycolors[]" value="yellow" <?=$this->validation->set_checkbox('mycolors', 'yellow')?>>
#8

[eluser]Sondlen[/eluser]
Is this the best method for preloading the form? I'm working with this exact problem.
#9

[eluser]marlar[/eluser]
Don't know if it's the best method, but it works well with my hack.
#10

[eluser]veddermatic[/eluser]
I did something similar to what you did marlar, but for radio buttons:
Code:
function set_radio($field = '', $value = ''){
  if ($field == '' OR $value == ''){
    return '';
  }
  $compareVal = isset($_POST[$field]) ? $_POST[$field] : $this->$field;
  if($compareVal == $value){
    return ' checked="true"';
  }  
}
[EDIT] And hey, since it's right next to it, here's the SELECT one as well:
Code:
function set_select($field = '', $value = ''){
  if ($field == '' OR $value == ''){
    return '';
  }
  $compareVal = isset($_POST[$field]) ? $_POST[$field] : $this->$field;
  if($compareVal == $value){
    return ' selected="true"';
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB