Welcome Guest, Not a member yet? Register   Sign In
conditional validation / post array
#1

[eluser]Future Webs[/eluser]
In building forms there are often cases where a field would be required on insert but not on update.

for example imagine this table

Code:
id
file_field
title
date_added
date_updated

on insert we would require require ® and post these

Code:
file_field (R)
title (R)
date_added (R)

but on update we would only require ® and post these

Code:
file_field
title (R)
date_updated (R)

obviously date_added and date_updated would be set in the controller or model rather then in the form but I put them there to help describe the situation.

How do you guys go about this situation. do you create a new function within your controller to handle either the "insert" or "update" or do you use one function called "form" and use some condition statements that handles what is validated and then put into an array to pass to the database

Hopefully i explained this well enough. Im sure its a common situation and i may be trying to scrimp on not coding but at the same time im trying to avoid repeating any code where possible. The example above has the bare minimum to explain things but in real life my forms / tables are much bigger
#2

[eluser]bapobap[/eluser]
I've thought about this myself and I just end up creating the insert code, then copying it and modifying it for the update code. I thought I could try and be clever about it and somehow merge the two but sometimes things change and no matter how clever I can be about merging them, I'll need to separate them again as my code wasn't flexible enough.

Where you can probably save some time/code is by using the same view for both but on edit you just load up the form inputs with the DB values.
#3

[eluser]jtingato[/eluser]
I was trying to thinking about this logically.


Wouldn't it be easiest to include both fields "date_added" and "date_modified" and to write to both of them upon the addition of the entry i.e. date_added = date_modified. That way all you would need to validate is whether "date_added" data exists, and if so don't write it.

Just a thought.




Theme © iAndrew 2016 - Forum software by © MyBB