So I have this account creation form for a company. Here's what it looks like:

I am going to explain first how a feature in this system works.

After company creation, a table will be shown, listing all the companies I created. In the Action column of this table, there's an action button that once I click it, I will be logged in into a specific company I chose, acting as the user logged inside that company. Inside, there's more like the same layout and I can create a sub-company called "Operator". So there's a "Create Operator" button and once clicked, I will be redirected to the same form layout above (except that the "Company Name" label is changed to "Operator Name"). Alright, so I am using Yii2 php framework and basically, each input field is an attribute in a db table and in my model, there's a function called public function attributeLabels() where each of these attributes are assigned to a specific label. For example, first input field's db table attribute name is company_name so inside this function it is assigned as company_name => "Company Name" and this label is passed to the view which is this form. So whenever there are error messages, this label is used. Regardless if I am creating a company or operator, the error message is always "Company Name should not be blank". What I need to accomplish is when I create an operator, the error message should be "Operator Name should not be blank."

I hope you get my problem. So I tweaked some javascript/jQuery but I am still having problems.

// For changing Operator Name error message var operatorNameLabel = $('.field-company-company_name label').attr('for','company-company_name').text(); var companyNameErrorDiv = $(".field-company-company_name p"); var operatorNameMsg = 'Operator Name should not be blank.'; $('#submit-button').click(function() { // Change Operator Name error message if (operatorNameLabel == "Operator Name") { companyNameErrorDiv.html("<p class='help-block help-block-error'>Operator Name should not be blank</p>"); //companyNameErrorDiv.text(operatorNameMsg); } }); $('#content-offset').click(function () { if (operatorNameLabel == "Operator Name") { companyNameErrorDiv.html("<p class='help-block help-block-error'>Operator Name should not be blank</p>"); //companyNameErrorDiv.text(operatorNameMsg); } });

My problem in my current code is that :

when I leave the operator name input field blank then click into a blank space outside the input field, the error message is still "Company Name should not be blank." but then when I click back to the input field, that's the time the error message changes to "Operator Name should not be blank." What really happens is that when I click at the operator name input field, then single click outside or at any other input fields, the error message remains "Company Name should not be blank.", but when I do a second click, the the error message changes. There's a problem when I click back to the operator name input field for it keeps on getting back to the "Company Name should not be blank" error message, when clicked.

I hope you get my problem.

EDIT: Yii2 generated html looks like this: