My scenario is that I have a field in a table called Stock_Qty that when it is changed I need to update the Stock_Date field with the current date and then the user can replace the date if they don't want todays date to be stored. There are other fields in the record so I can't rely on the last modified date as the Stock_Qty may or may not have been changed when the record was last saved.

Ignore Last Updated as it really a Stock Available date

Ignore Last Updated as it really a Stock Available date

I have been looking for something in Javascript or an ability already baked into Joomla but have came up short. I suspect the answer is in Javascript but it is something I still have to learn a lot more about and I have looked through a lot of Joomla code in /libraries/cms/html but haven't found anything like what I want to do.

I have been looking for an example around the onchange= field of the <input> statement on the Form mainly because I have been doing some hacking on a Filter field recently and I can see that onchange= and onclick= are able to effect changes on another field.

I have considered storing the value for Stock_Qty and doing a before and after compare in the model before it is saved however it gets complex trying to then work out if the users has provided a new date or if it is the date retrieve from the table. I am trying to avoid more calls to the table to check each record when it is saved.

I haven't been able to think of any examples in Joomla that do something similar either.

Any assistance appreciated and I will reply, I monitor JSE daily.

For this question I am trying to find a solution for when Stock_Qty is changed via a simple edit Form, however I also need to handle a file import changing the Stock_Qty so I have been looking at code for changing the date at the time the record is written to the table as well, overriding updateObject in /libraries/joomla/database/driver.php, therefore I am open to suggestions outside the scope of just Input fields and JS if it is more elegant.

  • Might you be interested in automating this purely in mysql? You could set the default column value to the current date and omit that column when you INSERT, and as for UPDATEs, you could write a TRIGGER that updates the datestamp when your Stock_Qty is updated. Concept: dba.stackexchange.com/a/120081/157408 is that attractive or do you wish to keep all control in the code base? Nov 12, 2019 at 8:15
  • @mickmackusa I did come across MySql triggers in my reading today which are something I didn't know existed but I would prefer to keep it in the logic of the code at this stage. I found this, dba.stackexchange.com/questions/77121/… as a possible approach to the file import requirement that I mention in the last paragraph dealing with things at the time the records are inserted/updated.
    – Irata
    Nov 12, 2019 at 9:05
  • Are you creating a custom extension, then it is possible with the use of triggers of Joomla, as you can trigger an event on after the store operation is performed and use this event in your plugin to perform the requested task. if you need more details do let me know.
    – Pratyush
    Nov 14, 2019 at 6:45
  • @Pratyush It is for a custom extension and I am aware of triggering the events for before and after save and while you say 'after the store operation' I think I need to do something before the store occurs if I was to use such a trigger. Thinking of comparing what was retrieved with what is about to be stored and then acting on the fields I need to from there. I still think that some JS acting on the fields at the time they are altered will be more useful to the end user if they see the date field change in front of them and do the other checks before storing.
    – Irata
    Nov 14, 2019 at 10:06
  • @Irata yes you can send the old data and new data to the trigger and perform the operation there.
    – Pratyush
    Nov 14, 2019 at 11:15

1 Answer 1


*** Update - This solution works provided the date input field doess not have a type="calendar" in the Joomla forms .xml. I will raise a separate question to try and work out why and report back here.

My solution to change the date field on a change in the current stock when entered via the form involved learning enough JS to become dangerous. :)

My Javascript which I placed in /media/com_mycomponent/js/extras.js

    function addDate(target_field) 
        document.getElementById(target_field).value = currentDate();

function currentDate() 
    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();

    today = yyyy + '-' + mm + '-' + dd;
    return today;

And in my com_mycomponenet/models/forms/edit.xml

<field name="supplier_qty" default="0" type="number" onchange="addDate('jform_supplier_qty_date')" ....  > 

And how it works for anyone who has a similar need or wants to use it as a model to alter other fields as a form is filled in.

The onchange="addDate('jform_supplier_qty_date')" on the input field, supplier_qty, triggers the addDate function and passes it the field name where the change is to be applied. The first line gets the current value from the field to be changed and the second line replaces it with the date returned from the currentDate function.

The currentDate() routine is based on an answer that had 2,500+ votes in stackexchange

I am already thinking of making it more flexible so I can use it in other places on my forms and not just for dates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.