Setting field as Read-Only or Disabled

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
yinsw
User
Posts: 159
Location: Penang, Malaysia

Setting field as Read-Only or Disabled

Post by yinsw »

Is there any way to set the field as ReadOnly and still submit the value? As I know, if you set it as Disabled, the value won't be submitted when saving.

I tried add the following in my Add/Copy Page Startup Script and it also can't work:

$("#btn-action").on("click", function () {    
    $("#x_MYFIELD1").prop("disabled", false);
    $("#x_MYFIELD2").prop("disabled", false);
});

To further explain:

I added the following in Add/Copy Page->Page_Load event. It does show correctly and the field was disabled:

$this->MYFIELD1>DefaultValue = "test";
$this->MYFIELD2>DefaultValue = "test2";

$this->MYFIELD1->Disabled = true;
$this->MYFIELD2->Disabled = true;

When I submit, it detected the value as missing as I set it as required field. I want the value to be submitted when I click the "Add" button.


mobhar
User
Posts: 11905

Post by mobhar »

You should use Row_Rendered server event instead of Page_Load to display the default values.

Assume MYFIELD1 and MYFIELD2 are using Textbox controls, then put the following code in Row_Rendered server event.

if (CurrentPageID() == "add") {
    $this->MYFIELD1->EditValue = "test";
    $this->MYFIELD2->EditValue = "test2";
}

If necessary, to make sure the default values are submitted before saving a new record, then you may also put this code in Row_Inserting server event:

$rsnew["MYFIELD1"] = "test";
$rsnew["MYFIELD2"] = "test2";

faisal117a
User
Posts: 12

Post by faisal117a »

below is my code of Edit page

function Row_Updating($rsold, &$rsnew)
{
  $rsnew["work_type"] = $rsold["work_type"];
  return true;
}

and

function Row_Rendered()
{
    if ($this->work_status->CurrentValue == "Paid") {
        $this->work_type->ReadOnly = true;
    } else {
        $this->work_type->ReadOnly = false;
    }
}

When I press update, it shows error of missing value of work_type. I tried putting code in other event also by searching on forum but still same error

I just want to disabled/hide/Readonly work_type on edit page

I also used $rsnew["work_type"] = $rsold["work_type"]; in inserting event but same error of missing value on update.

Any solution please


mobhar
User
Posts: 11905

Post by mobhar »

faisal117a wrote:

I also used $rsnew["work_type"] = $rsold["work_type"]; in inserting event but same error of missing value on update.

In Row_Inserting server event, there are no value yet for your work_type field, thus it will raise an error.

You should define the value explicitly and not get from the old recordset in Row_Inserting server event, for example:

$rsnew["work_type"] = "something"; // assume it is a varchar or string field type

faisal117a
User
Posts: 12

Post by faisal117a »

Sir, I got the same error of missing

"Work" table specific events

function Row_Updating($rsold, &$rsnew)
{
  $rsnew["work_type"] = $rsold["work_type"];
  return true;
}

function Row_Inserting($rsold, &$rsnew)
{
    $rsnew["work_type"] = "Work Order";
    return true;
}

function Row_Rendered()
{
     if ($this->work_status->CurrentValue == "Paid") {
        $this->work_type->ReadOnly = true;
    } else {
        $this->work_type->ReadOnly = false;
    }

}

work_type field is a radio buttons of "Work Order" and "Estimate", when I open edit page it is readonly and one of two values "Work order" is selected but after hitting update button page refreshed and both radio buttons are unselected with error and if I see inpect code at this point

<input type="radio" class="form-check-input ew-custom-option is-invalid" data-table="work" data-field="x_work_type" name="x_work_type_421383" id="x_work_type_197884" disabled="1" data-readonly="1" value="Work Order" data-index="1">

Now I do not know why disabled="1" even I just used ReadOnly.

work_type is required field and I checked it also after disabling "Required" but same error. Also I tried to do thing with edit page events but same result. I also used $this->work_type->Disabled instead of $this->work_type->ReadOnly same result.

Kindly help me to sort it out. Also an alternative way like JS/JQuery with a code snippet and where to place that code will help me.

I am registered user of v2023.


mobhar
User
Posts: 11905

Post by mobhar »

What is the field type of work_type? Is it an Integer/Numeric field? Or is it a String/Varchar field?

In addition, did you use Lookup Table for that field, or did you use User Values for that field?


faisal117a
User
Posts: 12

Post by faisal117a »

Field type is Varchar and I used two user values "Work Order" and "Estimate". I did not used table lookup.


mobhar
User
Posts: 11905

Post by mobhar »

Is the Value and Label for your User Values are the same or difference?


faisal117a
User
Posts: 12

Post by faisal117a »

Yes, same


mobhar
User
Posts: 11905

Post by mobhar »

Okay. Now, what control that used by your work_status field? Does it use Textbox or Select (Combobox) control?


faisal117a
User
Posts: 12

Post by faisal117a »

work_status (varchar) used select (Combobox) control with two user values "Paid" and "Pending"


mobhar
User
Posts: 11905

Post by mobhar »

Okay. It seems the required information is pretty enough.

However, since I have been using v2024, let me answer your question by using PHPMaker v2024.

For Radio Button control, then you should not use ReadOnly property of the field. You may use Disabled property instead.

So, the code in your Row_Rendered server event should be like this:

function Row_Rendered()
{
    if (CurrentPageID() == "edit") {
        if ($this->work_status->CurrentValue == "Paid") {
            $this->work_type->Disabled = true;
        } else {
            $this->work_type->Disabled = false;
        }
    }
}

mobhar
User
Posts: 11905

Post by mobhar »

If you want to handle also for Add Page, for example, by default the value for Radio Button is "Work Order", and you want to disable the control, then the code in Row_Rendered server event should be like this:

function Row_Rendered()
{
    if (CurrentPageID() == "edit") {
        if ($this->work_status->CurrentValue == "Paid") {
            $this->work_type->Disabled = true;
        } else {
            $this->work_type->Disabled = false;
        }
    }

    if (CurrentPageID() == "add") {
        $this->work_type->CurrentValue = "Work Order";
        $this->work_type->Disabled = true;
    }
}

In addition, make sure also in Row_Inserting server event, your code like this:

// Row Inserting event
function Row_Inserting($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false

    $rsnew["work_type"] = "Work Order"; // don't forget this
    
    return true;
}

faisal117a
User
Posts: 12

Post by faisal117a »

Thanks for your help.

Before coming to this forum I already tried Disabled and Readonly and many other possibilities but result is same.

All I know something is either missing or it is the PHPMaker which create such uncertainty. After a huge time wondering I come to the forum. This is because of no great documentation with good use cases is there. They never tell you in docs that radio buttons and textbox capacity about readonly and disabled etc.

If you think about any other solution, please guide me.


mobhar
User
Posts: 11905

Post by mobhar »

I have tried that code above first by using PHPMaker 2024, and it works properly. There is no error at all.

Then, I've just tried it also by using PHPMaker 2023, a few minutes ago and it works properly, too.

In addition to the code above, I also have the code in Row_Updating server event as follows:

// Row Updating event
function Row_Updating($rsold, &$rsnew)
{
    // Enter your code here
    // To cancel, set return value to false

    if ($rsnew["work_status"] == "Paid")
		$rsnew["work_type"] = "Work Order";

    return true;
}

Everything is working properly as expected.


mobhar
User
Posts: 11905

Post by mobhar »

And one more thing, make sure also you have already updated the template to the latest version, just click on Tools -> Update Template, and then re-generate ALL the script files, and try again.


faisal117a
User
Posts: 12

Post by faisal117a »

-Re-install PHP Maker v2023
-updated the template
-Clear browser caches
-three events code

function Row_Inserting($rsold, &$rsnew)
{
   $rsnew["work_type"] = "Work Order";
    return true;
}

function Row_Rendered()
{

    if (CurrentPageID() == "edit") {
        if ($this->work_status->CurrentValue == "Paid") {
            $this->work_type->Disabled = true;
        } else {
            $this->work_type->Disabled = false;
        }
    }

}

function Row_Updating($rsold, &$rsnew)
{
    if ($rsnew["work_status"] == "Paid")
		$rsnew["work_type"] = "Work Order";

  return true;
}

Only the depressive thing that if every thing is working fine then why this one is not working.

error after pressing update button

Row_Rendered code works but at the end result is same.

Any JS/JQuery solution ?


mobhar
User
Posts: 11905

Post by mobhar »

I don't know the condition when the Edit form is loaded for the very first time.

I meant, did you see the Type field has been selected for one of the provided options? If not, then it means perhaps you have existing record which has blank value for the Type field.

For such case, then you need to edit the value directly from database side, and not from the web application UI above.

Another thing that you need to double check is, make sure the Type field is NOT setup as ReadOnly from Field setup.


faisal117a
User
Posts: 12

Post by faisal117a »

did you see the Type field has been selected for one of the provided options?

Yes, I told in previous discussion that when we open edit page first time then "Work order" option is selected and after pressing save button both radio buttons are unselected. I do know why.

Another thing that you need to double check is, make sure the Type field is NOT setup as ReadOnly from Field setup.

No definitely not. Field is not Readonly


mobhar
User
Posts: 11905

Post by mobhar »

Did you have another code in any other server events or any other client scripts that cause the option will be clear on form submit?


Post Reply