Forums

Submitting empty fields

emmexx 10 Dec, 2012
I created a form that users can edit.
I just found out that I mismanaged the submission of empty fields. 😟
Case study:

[list]
  • the user enters a value in a field (field1)

  • the user saves the record

  • the user edits the record and clears the value of field1

  • the user saves the record
  • [/list]

    Since field1 is empty it is not posted and saved in the database.
    field1 has still the value entered when the user created the record.

    What is the best way to manage properly that?
    A custom server side validation action where I check if the field is set and if not I set it to a an empty value?

    
    if(!isset($form->data['field1']))
       $form->data['field1'] = '';


    Is there another CF way?

    Thank you
    maxx

    p.s. What I described above doesn't happen for any text box element. It happens for fields that are required depending on a drop down selected value.
    In my form there's some js code that manages that:

       if(e.target.options[e.target.selectedIndex].value=='other')
       {
          $(target).set('disabled',false);
          $(target).addClass("validate['required']");
          formCheck_myform.register($(target));
       } else {
          $(target).set('disabled',true);
          $(target).set('value','');
          $(target).removeClass("validate\\['required'\\]");
          formCheck_myform.dispose($(target));
        }


    (target is the id of field1)
    The dropdown element has a list of presets and an "other" value. If the user wants to enter a value other than the presets, he selects "other" and field1 gets enabled and becomes required.
    If the user selects a value other than "other", field1 is disabled and becomes not required.
    GreyHead 10 Dec, 2012
    Hi Maxx,

    Hard to know - clearing the value of an input is presumably a valid edit (or could be). I guess that you need to add validation of some kind - probably both client and server-side - to detect errors like this and require the user to fix them.

    The validation you have in your post won't do as the $form->data entry will be set even if the input is empty (the only exception is for checkboxes and radio buttons, unchecked checkboxes and radio buttons return nothing).

    Bob
    emmexx 10 Dec, 2012

    Hard to know - clearing the value of an input is presumably a valid edit (or could be). I guess that you need to add validation of some kind - probably both client and server-side - to detect errors like this and require the user to fix them.



    Sorry but there must me a misunderstanding.
    This is not a user error/problem.
    The problem is that if the user wants to edit (clear) a field, he can't because the empty field is not being submitted.

    The validation you have in your post won't do as the $form->data entry will be set even if the input is empty (the only exception is for checkboxes and radio buttons, unchecked checkboxes and radio buttons return nothing).



    Other misunderstanding.
    The problem here is that for a standard text field the form submits even an empty value. For my special field to which I apply some javascript, the form is not submitting if the field is empty.

    I suppose that applying the javascript code to the field to change the validation, changes something else and the field stop being submitted.
    But I don't understand if this is an error in my code or a CF bug.

    Thank you
    maxx
    GreyHead 12 Dec, 2012
    Hi Maxx

    I'm sorry if I missed the point on Monday - I'd just got off an over-night ferry home. But I'm still missing something important.

    My understanding is that if a text input is empty then it will still be returned in the $_POST array and then copied to the $form->data array as an empty value.

    The DB Save action will then save anything in the DB to the matching column in the database record so should remove any existing value.

    On the other hand, if there is no matching entry in the $form->data array then the column will not be updated and an earlier value will remain unchanged.

    Bob
    emmexx 13 Dec, 2012

    My understanding is that if a text input is empty then it will still be returned in the $_POST array and then copied to the $form->data array as an empty value.



    Thank you Bob, that is my understanding too. But...

    What is happening in the setup of my form is that some standard text boxes are submitted, my non-standard one's aren't.

    A non-standard text box is simply a text box enabled/disabled dinamically through javascript. Say you have a check box and if it is checked you have to enter some text in one extra field (the field becomes Required).
    If I edit the record, uncheck the check box, clear the value of the extra field, the extra field is not being submitted.
    Since the only difference between standard and non-standard text boxes is the javascript code applied, I suppose there's something wrong in my javascript code that disrupts the form, killing the non-standard text boxes.

    Bye
    maxx
    GreyHead 13 Dec, 2012
    Hi Maxx,

    If you disable a text box then that input is not returned in when the form is submitted. If you want it to submit then make it readonly - and set the value to '' if that's appropriate.

    Sorry for being slow on the uptake here.

    Bob
    emmexx 13 Dec, 2012

    If you disable a text box then that input is not returned in when the form is submitted. If you want it to submit then make it readonly - and set the value to '' if that's appropriate.



    That makes sense.
    I disabled the text boxex because disabled one's give a better visual feedback than readonly one's. I'll use some css instead.
    I had already solved the problem by checking the field on submit:

    if(!isset($form->data['myextrafield'])
       $form->data['myextrafield'] = '';


    But your suggestion is simpler and more standard.

    Sorry for being slow on the uptake here.



    Your help is always appreciated, it's my fault not to know by heart the html 4 manual. 🙂

    Bye
    maxx
    GreyHead 13 Dec, 2012
    Hi Maxx,

    The other alternative is to disable the input but to use a hidden 'ghost' input with the same name. As long as the ghost comes before the 'real' input it will only have any effect when the real input is disabled.

    Bob
    emmexx 13 Dec, 2012

    The other alternative is to disable the input but to use a hidden 'ghost' input with the same name. As long as the ghost comes before the 'real' input it will only have any effect when the real input is disabled.



    Yes, of course, a viable solution if you have only 1 or 2 of those text boxes.
    My form is already a mess with 40+ fields and adding also custom fields for 5-6 text boxes is just a source of unnecessary complications.

    Thank you
    maxx
    This topic is locked and no more replies can be posted.