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

Parents
  • +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.

Reply
  • 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.

Children
  • 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.

  • 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.

  • 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.

  • 0
    Certified Lead Developer
    in reply to rp_balaji
    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.

    FYI, this won't work either - the document is only uploaded to a temporary and non-user-accessible system-protected folder up until the task is submitted, at which point the document is moved to its Target folder and becomes accessible to the user.  Therefore you wouldn't be able to swap in download links immediately for uploaded documents, no matter what else you do.  For more information, please see the file upload field documentation, particularly the first entry in the Notes section, which explains what i just described but probably more accurately.

  • Hi Mike, I would like to access fv!files metadata from saveInto too. Is there any plan to update this component in the near future?

  • 0
    Certified Lead Developer
    in reply to GABRIELTAY

    There are no plans that I know of for this - I believe it has not been included in 19.3, at least, though there's a chance it might be on the roadmap (nobody outside of Appian engineering would have any way of knowing one way or the other).  I've been toying with the idea of creating a "Feature Request" thread on Community to call this out specifically and publicly, so watch for that and like/upvote it if you see it anytime soon.