Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice: Use same form for creation and update

Tags:

html

forms

php

I'm just curious and was wondering how you guys handle it if you want to use the same html form and as far as possible the same php code to create and update an item.

Example:
On one page you can create a database entry with name, email address and age.
On a different(?) page you see the form fields filled with your data and you can edit and save it.

I have my ways to accomplish this using pretty much the same code - but I'm hoping to learn something here. So how would you handle this task?

Thanks & Cheers, sprain

like image 447
sprain Avatar asked Aug 13 '10 07:08

sprain


3 Answers

Pretty easily - if an ID of an existing item (which the user is authorised to edit) is supplied in the query string, then it's an edit operation.

If no ID is supplied in the query string, it's a create operation.

The fields are pre-populated based on the existing values from the database if it's an edit operation, or based on default values or empty strings if it's a create operation.

like image 116
thomasrutter Avatar answered Sep 25 '22 00:09

thomasrutter


The way I see it is that reusing identical markup for form between create/edit works for some cases, but not for all. I find that forms -- though they may map to the same database table -- are really defined by their context. For example, if you had a 'users' table, you might have a 'create' form with username, email, password, but after that user exists you want them to retain their identity on their site, so the username field would not appear in an 'edit' context. I'm classically a PHP developer, but I have come to appreciate the approach that Django takes, where you create a model (table) that defines the basic validation for each field and you can create as many forms as you that build off of, or modify/extend from that definition. If you're writing from scratch, you'll probably find it practical to make your validation methods very portable and/or find ways to make your form fields context-sensitive.

like image 32
Chris Forrette Avatar answered Sep 23 '22 00:09

Chris Forrette


That's the way I always do it now. Are you using an MVC system at all? I use one controller with two different actions (urls = person/new + person/edit/xxxx_id).

the code is then something like:

function new()
    errors = []
    if (get)
        data = blank_record()
    elseif (post)
        data = posted_data
        if (create(data))
            redirect_to_listing()
        else
            errors = describe_errors
    show_form(data, errors)

function edit()
    errors = []

    if (get)
        data = get_from_db(id)
    elseif (post)
        data = posted_data
        if (save())
            redirect_to_listing()
        else
            errors = describe_errors
    show_form(data, errors)

Note that once it gets to the form there's always an object called data that the form can render, it may be blank, from the db, or posted data. Either way it should always be the same format.

The reason I split new and edit is that I find that often enough they are actually quite different in their behaviours and the load and save steps.

like image 35
Aidan Kane Avatar answered Sep 26 '22 00:09

Aidan Kane