How to save a value as part of a Field Validation?

Hi,

I have a requirement that I have a fileupload control where I need to set value as NULL whenever the validation fails. The validation I have added is to the fileupload field, so at present whenever I upload a wrong file there will be an error message displayed as I have added a validation specific to the field and manually I need to remove the file and upload the right one.

Instead my requirement is something like this,

a!fileUpload(

label" "File Upload",

value: local!fileId,

saveInto: local!fileId,

validations: {

if(fv!files.extension = "png",{"Error to display", a!save(local!fileId,null),"")  /* Is something like this possible? or how we can achieve this */

}

}

The above scenario/question is applicable for a textfeild or any other fields also where we can change the value of the field if the validation fails.

Regards,

Balaji.R

)

  Discussion posts and replies are publicly visible

  • Hi - what will you be using the null value for? Since you cannot submit the form until you've resolved the validation error I'm not sure what it is you're trying to achieve.

  • +1
    Certified Lead Developer

    Very simple.  You need to configure them in separate locations.  You can only set the a!save() functions in the saveInto block, but you can always set them conditionally by putting them in an if().  Validations can only go in the validations block, but they can also be defined with an if().  All you need to do is put the save in an if() with a set of conditions and put the validation in another if() with exactly the same conditions.  They'll both be in different parts of the code, but both be true at the same time.  Pseudo-code:

    a!interfaceComponent(

    label: "Something",

    ...

    value: local!someValue,

    saveInto:{

    a!save(local!someTarget, save!value),

    if(ri!conditions, a!save(local!fileId, null), {})

    },

    ...

    validations: if(ri!conditions, "Error to display", "")

    )

  • 0
    A Score Level 1
    in reply to Dave Lewis

    Hi David,

    Thanks for ur response, and my requirement something like below 

    a!fileUploadField(
             label: "Upload Excel",
             target: cons!UPLOAD_FOLDER_ID,
             maxSelections: 2,
             value: ri!documentIds_int,
             saveInto: {
                    ri!documentIds_int,
                    a!save(
                       local!fileIds,save!value
                    ),
                    if(fv!files.extension = "xlsx",
                      a!save(ri!documentIds_int,null),
                      null)
             }
    )

    I want to check the extension of the file and if it is wrong format then I should make the ri!documentIds_int as null. And in the above code am facing an error in "if" condition saying "Could not find variable 'fv!files'". Is there any way to access the fv!files variable in the saveInto.

  • 0
    Certified Lead Developer
    in reply to rp_balaji

    You wouldn't want to do this anyway.

    For one thing: if you clear out the value of ri!documentIds_int as soon as an invalid extension is uploaded, then the user will never be able to see any error message you put in the validations parameter - all they will see is that the file upload box will seem to just clear itself out as soon as they've uploaded a file, and you will get bug reports for this since they won't understand.

    But more importantly: once a document is uploaded to the File Upload field, unless its value is manually cleared out from within the file upload field itself, then that document will be saved to the filesystem upon submission of the form, regardless of whether some other control on the form set its value to Null before submission.  I strongly recommend against allowing this to happen.

    I do agree that the fv!files metadata should be accessible from the saveInto parameter of the File Upload Field (note: ), but in this case I believe you need to take another approach, like just using the straightforward validations.

  • Hi Mike,

    Thanks for ur response, My requirement is also something like you mentioned in Point No.1, I need to clear as soon as a file with wrong extension is uploaded but as you have mentioned how can I clear the value of ri!documentIds_int because I can check the files extension only in the validations part and in validations I cant use a!save to clear out the data.

  • Hi David,

    That is not possible, because the extension of the file cannot be accessed in the 'saveInto' part, if you would have seen my above code accessing the fv!file.extension inside a "saveInto" will throw an error.

  • I wonder if it came from Client.  Because of the validation message I came to know that I have uploaded an Incorrect Extension Document. So I can go ahead and remove the file and upload a valid file. If it came from Client you can explain them its better to have these validation error messages that prevent the form submission.

  • 0
    Certified Lead Developer
    in reply to rp_balaji

    Well my main point is, even if you could find a way to clear out the document upload field immediately after a bad extension is uploaded, it's still a really bad idea, for several reasons (which I listed at least some of in my previous reply).  It would be far more effective to throw a validation and force the user to clear out the value of the field manually - this is the only way to actually ensure that the document doesn't end up being uploaded to your Appian filesystem.

  • 0
    A Score Level 1
    in reply to chandu

    Hi Chandu,

    Yes this is the requirement but in a slight different way, we have the Uploader component in a grid so whenever user uploads a file the document_id will be saved in a variable. And the condition here in the grid is whenever user uploads and document_id generated we have to hide the uploader column and show the document_id column as a link. So as the uploader component is hidden though there is a validation it will not be shown to user, only the document_id link will be visible though it is a wrong file. And this is when I have the need of validating the extension before the document_id is getting saved in to the respective variable.

    So was curious in knowing if something like the above is possible.

  • Hi Mike,

    Yes this is the requirement but in a slight different way, we have the Uploader component in a grid so whenever user uploads a file the document_id will be saved in a variable. And the condition here in the grid is whenever user uploads and document_id generated we have to hide the uploader column and show the document_id column as a link. So as the uploader component is hidden though there is a validation it will not be shown to user, only the document_id link will be visible though it is a wrong file. And this is when I have the need of validating the extension before the document_id is getting saved in to the respective variable.

    So was curious in knowing if something like the above is possible.