initial value of a field

Hi everybody, I have a simple query, but have been able to twist around the solution with trial and error, but I think is not reliable.

I have a form on which all fields are read-only. Now, when I click on ADD I get a form with all new fields (none are disabled).
What I want:
I would like to make a field take a value like this: {year} = date(‘Y’, strtotime( date(‘d-m-Y’))).
As soon as I click on ADD this field must be initialized to the value… in which event or any other method to go about this?

Thanks again for fast reply.

I was able to put the code in onBeforeInsert … but then it does not appear on the form while editing. onLoad is not Ok, as this will over-write the database vallue.

If you already have a stored value in db why do you want to print another value date(‘y’) ?

For new record

In onload add this


if(strlen({val_from_db})<4)
  {val_from_db}=date('Y', strtotime("now"));

Hello Clauu,

Your suggestion was smart! Thank you, it worked wonderfully.

Why coding, when you can (and should!) use Form Settings / Edit Fields / DB value (Insert)?

You mean i can put the formula in “defined value”?

Yes, try it …