Welcome Guest, Not a member yet? Register   Sign In
Help keeping my views DRY when using validation library.


I am doing some form validation using the validation libs.

I have a bunch of the usual CRUD views.

To keep things DRY I share the same form for Adding and Updating.

When using the validation lib I have to populate the form values using:

... value="<?php echo( $this->validation->title) ?>"/>

and so on...

When Updating an object these values are populated from some other data source so the above method does not work. I.e. The initial values come from a model object passed to the view and not the validation object.

How would one do this so I can share the same form and have validation... I could examine the validation object prior to populating each field, like:

... value="<?php echo( $this->validation->title?$this->validation->title:$title) ?>"/>

but I can't help think there must be a better way.

Any suggestions would be good.


I think I have thought how to do it...

Rather than comparing the validation value in the view I simply create a blank model object of the right type and then populate the values passed from the form prior to putting it back to the view...

$this->publication->title = $this->input->post('title');
$date['publication'] = $this->publication;
$this->load->view('admin/publication/edit', $data);


That's a good way to do it - or you can also use a bit of variable referencing. In your controller:

if ($existing_publication)
$data['publication'] =& $this->publication
$data['publication'] =& $this->validation;

Now in your code you can reference $publication->title and it will either be referencing the existing publication, or the values returned from the form validation system.

So you can then make your view agnostic as to where its info is coming from.

[eluser]Andrew Cairns[/eluser]
Sounds like you may be looking for the set_value() function within the form helper:

Theme © iAndrew 2016 - Forum software by © MyBB