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

[eluser]Unknown[/eluser]
Hi,

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:

Code:
... 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:

Code:
... 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.

Thanks
Paul
#2

[eluser]Unknown[/eluser]
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...

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

Thanks
Paul
#3

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

Code:
if ($existing_publication)
{
$data['publication'] =& $this->publication
}
else
{
$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.
#4

[eluser]Andrew Cairns[/eluser]
Sounds like you may be looking for the set_value() function within the form helper:
http://ellislab.com/codeigniter/user-gui...elper.html




Theme © iAndrew 2016 - Forum software by © MyBB