Welcome Guest, Not a member yet? Register   Sign In
Default values and the validation library
#1

[eluser]Vang[/eluser]
Unfortunately the validation library 'assumes' that all forms by default are empty. However most of the time you need default values mostly for checkboxes and radio buttons. It would be great to be able to define an array with default values.

Any plans of implementing that into the next CI ?
#2

[eluser]Randy Casburn[/eluser]
Hi Vang -- Either I've totally missed what it is you want to accomplish, or you're it's possible you're over complicating a very simple issue...

Quote:It would be great to be able to define an array with default values.

You can...

$_POST['MyDefaultValue'] = 'NoPlanOnDoingSomethingThatIsntNecessary';
$_POST['MyCheckBox'] = 'Checked';
$_POST['MyRadioButton'] = 'Buttoneded';

Now those will all have values when the validation class sees them. You can call them 'default' if you want ;-). Your task is to get them to the validation class before it is run.

Either way, there is a simple solution.

Randy
#3

[eluser]Colin Williams[/eluser]
Randy's right that it's a simple fix, although there really should be a set_values() method in the Validation library. (I think it's been done in the wiki. I've done it but I don't know if it's worthy of a release because it catered rather particularly to the given app.)

I have a config/forms.php settings file that looks something like this:

Code:
$config['forms']['register']['rules'] = array(
   'name' => 'required',
   'password' => 'required',
);
$config['forms']['register']['fields'] = array(
   'name' => 'Your Username',
   'password' => 'Set a Password',
);
$config['forms']['register']['values'] = array(
   'name' => 'Bob',
   'password' => '',
);

Later on, at the Controller...

Code:
$this->load->library('validation');
$form = $this->config->item('forms', 'register');
$this->validation->set_fields($form['fields']);
$this->validation->set_rules($form['rules']);

// yadda yadda yadda
$this->load->view('user/register', $form);

So, now in my view, I have $rules, $fields (good for labels) and, ahah!, $values. And this way, I'm never accessing $this->validation in my View (I avoid $this->anything in my views).

Ah, but how do the values get set if the user has input some? Like this:

Code:
foreach ($form['fields'] as $name => $value)
{
   if (is_array($form['values'][$name]) and !is_array($this->validation->$name))
   {
      // For empty multiple valued fields
      $form['values'][$name] = array();
   }
   else
   {
      $form['values'][$name] = $this->validation->$name;
   }
}

And this little loop is what should be done in $this->validation->set_values(), well, the other way around... maybe there should be get_values() too, since that is really what my loop is doing. set_values() would store default values in $this->validation->name
#4

[eluser]Vang[/eluser]
Quote:You can…

$_POST[’MyDefaultValue’] = ‘NoPlanOnDoingSomethingThatIsntNecessary’;
$_POST[’MyCheckBox’] = ‘Checked’;
$_POST[’MyRadioButton’] = ‘Buttoneded’;

@Randy: Very simple yet not the best solution. The $_POST array is what we get from a form so we shouldn't change its meaning to anything else. Imagine that someone else will have to make modifications to your code in 8 months from now. Seeing the $_POST there will confuse things. Also it would be nice for the the validation class to be able to see the difference between what is a default value and what is a value we got from the form.

@Colin: I was thinking something like
Code:
$defaults['name'] = 'bob';
$default['food'] = 'fish';
$this->validation->set_default($default);

This would allow the validation class to check first if there are any default values and then for anything else. This way you won't have to worry about anything else in your controller.
#5

[eluser]Colin Williams[/eluser]
Good point, Vang. Otherwise you have to inform coders that set_values() does not take precedence over $_POST values. Although, I'd probably make it set_default_values() or just default_values() to keep the API clear. Whatever you call it, it's probably no more than 10 lines of code to add to application/libraries/MY_Validation.php

If you end up doing it, share the code. I know a LOT of CodeIgniter's would appreciate it.

Randy's way is still probably the simplest/most-straight-forward, it just needs a check added to it:

Code:
if (empty($_POST))
{
   $_POST['sisters_hot_friend'] = "Vanessa";
   $_POST['milf_at_grocery_store_today'] = "Jenaveve";
}
#6

[eluser]Vang[/eluser]
Quote:If you end up doing it, share the code. I know a LOT of CodeIgniter’s would appreciate it.
Yes of course I will. I'm just under huge pressure at the moment to release the project i'm working on. I've got over this form issue with really ugly hacks at the moment. However i'll get to it end of the week.

I'd love to see the CI devs implementing such functionality to CI or tell us if they got a nice reason not to, or tell us what they think in general.
#7

[eluser]Colin Williams[/eluser]
Quote:I’d love to see the CI devs implementing such functionality to CI or tell us if they got a nice reason not to, or tell us what they think in general.

I'm sure you've caught on, but the EllisLab guys are also "under huge pressure at the moment," working night and day on releasing EE 2.0 (and thus, the next CI).

But I wouldn't wait on them to make the move. I've seen them implement suggestions from forum members into the codebase before, so just get it out there and who knows...

Besides, I think the alternative is something like this:

Code:
<input type="text" name="name" value="<?= ($name = $this->validation->name) ? $name : 'default value' ?>" ?>" />

Not so pretty, IMO (not to mention it breaks down when a default value is made empty by the user and it's hard to maintain)
#8

[eluser]Chicken's Egg[/eluser]
Default values can also be populated as showed below

Code:
<?php
// If form is not send
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
  $this->validation->iMeta_index  = $this->config->item('Cat_index', 'MY_conf_news');
  $this->validation->iMeta_follow = $this->config->item('Cat_follow', 'MY_conf_news');
}
?>

In this case I created checkboxes in my form (using the form helper) like:

Code:
<?php
      $form['form_txt']['label'][] = form_label($fields['iMeta_index'], 'Meta_index');
      $fieldinfo = array(
             'name'        => 'iMeta_index',
             'id'          => 'Meta_index',
             'value'       => '1',
             'checked'     => $this->validation->iMeta_index,
      );
      $form['form_txt']['field'][] = form_checkbox($fieldinfo);
      $form['form_txt']['label'][] = form_label($fields['iMeta_follow'], 'Meta_follow');
      $fieldinfo = array(
             'name'        => 'iMeta_follow',
             'id'          => 'Meta_follow',
             'value'       => '1',
             'checked'     => $this->validation->iMeta_follow,
      );
      $form['form_txt']['field'][] = form_checkbox($fieldinfo);
?>
#9

[eluser]Phil Sturgeon[/eluser]
My preffered way is to leave the validation library alone, and just set my own values. That can be existing values or post variables.

Code:
$rules['title'] = 'trim|required';
            $rules['description'] = 'trim|required';
            $this->validation->set_rules($rules);
            $this->validation->set_fields();
            
            $this->data->gallery = $this->galleries_m->getGallery($slug);
            
            foreach(array_keys($rules) as $field) {
                if(isset($_POST[$field]))
                    $this->data->gallery->$field = $this->validation->$field;
            }

Doing this same thing you can set blank values instead of exiting data, so you can use the same view file for adding and editing.




Theme © iAndrew 2016 - Forum software by © MyBB