[ANSWER] - File widget empty or filled with validator

Hello,

I have a form. In this one, I would like to :

  • upload a file if I want to put a file. So the file widget can be empty or filled.
  • If the widget is filled, I want to check the file name to allow only some file extensions (.pdf, .png,…). I already did it with a regex code and it works. But this validator dont let the possibility to have the field empty and obliged the user to put a file on it…

Can you tell me why it doesn’t works like I want? I already try to add a “if” loop on it and test the filename or filecontent to see if it is empty or equals to “” but nothing works.
Thanks for your help.

You can download the .proc to see my work and help me : http://dl.free.fr/getfile.pl?file=/uSjmCo7V

Cheers,
Sylvain

Hi
if you do a simple groovy validator that returns true, you will see that you can submit your form, the validator don’t throw

So, do a groovy validator that returns true if all conditions are respected, and false if not.
for example :

def var = "blue" def result

if(var ==“blue”){
result = true
} else {
result = false
}

return result

Then , improve your condition by using a groovy regex : http://groovy.codehaus.org/Regular+Expressions

Cheers

Hi,

try this,

Boolean isValidate = true;

// or file_field.hasContext()
if (file_field != null){
isValidate = your regex here;
}

return isValidate

I had to do something similar in the end my code is as follows:

does exactly what you want, accepts a null or specific extension…

  1. Create file widget
  2. goto Validator
  3. UNCHECK - Add the default validator for this widget
  4. Name - give it a name
  5. Validator type = Groovy Expression
  6. Error Msg = “Error not empty or valid type (pdf, png)”
  7. parameter = Groovy Code as follows
import org.bonitasoft.engine.api.ProcessRuntimeAPI; import java.util.logging.Logger;

int dI = 0;
boolean debug = false; //set true for debug info

ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
String processName = processRuntimeAPI.getProcessInstance(processInstanceId).getName();

//set the name of the routine
String thisTrace = " "+processName+ " fileNull: "

Logger logger= Logger.getLogger(“org.bonitasoft”);
if(debug){dI++; logger.severe(dI+thisTrace+“Trace Start”);}

// set default return
boolean fileValid = false;

// NOTE field_File1.hasContent() does not handle a null entry (bug filed)
if (field_File1 == null){
if(debug){dI++; logger.severe(dI+thisTrace+“file is null”);}
fileValid = true;
}
else if (field_File1.hasContent() && // just making sure field_File1.hasContent()
(
// test file extensions
field_File1.getFileName().toString().substring(field_File1.getFileName().toString().lastIndexOf(“.”), field_File1.getFileName().toString().length()).
equalsIgnoreCase(“.pdf”) ||
field_File1.getFileName().toString().substring(field_File1.getFileName().toString().lastIndexOf(“.”), field_File1.getFileName().toString().length()).
equalsIgnoreCase(“.png”)
)
)
{

if(debug){dI++; logger.severe(dI+thisTrace+"hasContent Print");}
if(debug){dI++; logger.severe(dI+thisTrace+"field_File1.getFileName().toString(): "+field_File1.getFileName().toString());}
if(debug){dI++; logger.severe(dI+thisTrace+"field_File1.getFileName().toString().substring: "+field_File1.getFileName().toString().substring(field_File1.getFileName().toString().lastIndexOf("."), field_File1.getFileName().toString().length()));}

if(debug){dI++; logger.severe(dI+thisTrace+"hasContent return PDF is valid");}
fileValid = true;

}
else {
if(debug){dI++; logger.severe(dI+thisTrace+“hasContent return extn is INVALID”);}
fileValid = false;
}

if(debug){dI++; logger.severe(dI+thisTrace+“Trace End”);}
return fileValid;

Hope it helps regards
Seán

no news? :cry:

I would ask this question “validating a variable with regex” in a different place like http://stackoverflow.com/.

The reason, it’s a REGEX expression issue and not directly related to the operation of Bonita. You may have to change the question to be less specific about Bonita like,

I have a variable that donates a file value. My regex is .{the regex code}. which works in that it only allows the file types I want, but I also want it to allow for null (empty) values. Can someone help?

You will get a much quicker answer there as well, usually within a couple of hours max, even during the weekend.

Good luck, hope this helps,
regards

PS I know nothing about regexs and use stack often

Ok thx. I will did it.
I was thinking that we can do it in the validator with a loop like: if…else…

Hello,

I tried on some regex forum but no good solution was given. I come back to the Bonita forum to see if anyone has a new answer.

Thank you everybody, the solutions of Frederic Krebs, Jarg and Sean McP work.

Best regards,