This topic is locked
[SOLVED]

 Custom field Required

3/28/2017 2:02:42 PM
PHPRunner General questions
T
thamestrader author

Title should really be 'How to set a custom field as required'.
I have two custom input fields on an Edit Page, whether they are visible depends on the value of the accom_type field (a radio button), if they are visible then they need to be required fields and they are initially populated with the current values extracted from a database field by the before-display event. There is code in the before-record-updated event to copy the data from the two custom fields back to the database field.
The HTML from the visual editor in HTML mode, which has been added manually.
<TR class="{$fielddispclass_st reet1}" id="cstrow1">

<TD class="rnr-label" stylewidth: 110px;" rowspan="1" colspan="1">

Flat No.

</TD>

<TD class="rnr-control style3" style="width: 91px;" rowspan="1" colspan="1">

<INPUT id="cstflat" style="width: 66px; height: 16px;" type="text" size="3">

</TD>

<TD class="rnr-control style3 runner-cc" style="width: 133px;" rowspan="1" colspan="1">

<P align="right">

Building Name

</P>

</TD>

<TD class="rnr-control style3 runner-cc" style="width: 242px;" rowspan="1" colspan="1">

<INPUT id="cstbldg" style="width: 221px; height: 16px;" type="text" size="23">

</TD>

</TR>
Annotated code

// accom_type field from edit page

var ctrl_accom = Runner.getControl(pageid, 'accom_type');
//Proxy values set up by before display event code

var bldg = proxy.pxy_bldg;

var flat = proxy.pxy_flat;
$(document).ready(function(){
// cstflat is first custom field - here its is automatically blanked out when it gets focus - working OK.

$('#cstflat').focus(function(e) {

$('#cstflat').val('');

});
// switch on the values of the accom_type field which are Flat, House, NFA

switch(ctrl_accom.getValue()) {

// Flat is where I want to make the cstflat and cstbldg custom fields required

case 'Flat':

// this next statement doesn't work

$('#cstflat').attr('required', true);

// next set of statements all work correctly

$('#cstflat').val(flat);

$('#cstbldg').val(bldg);

$('#cstrow1').show(); // this is making the row with cstflat and cstbldg visible

pageObj.hideField('street1');

ctrl_str2.addValidation('IsRequired') ;

break;
I have also tried:

$('#cstflat').prop('required')

document.getElementById("cstflat").required = true;
which didn't work either.
Having trawled through the forum posts, and Googled extensively I'd be very grateful for any suggestions as I'm now stumped.
Thanks

admin 3/28/2017

I think it will be much easier for you if you just add a new calculated field to the SQL Query and setup it as required via PHPRunner settings. This way you can use all PHPRunner APIs and make field required on the fly if neeeded.

T
thamestrader author 3/29/2017

Thanks very much.
I managed to get virtually everything to work, the only thing that isn't working is getting character data passed to the PHP Before record Updated event in the $values['field'], only numeric data is passed back - which makes sense as it is supposed to be a calculated field.
This is what I've done so far...

  1. In the SQL Query I just selected an existing text field ie Text_Field1 + text Field2and gave it an alias. Then set everything else as if it were a normal database field, selecting text options everywhere as I needed text data to be entered and displayed.
  2. To get database values displayed in the 'calc' fields I used the setProxyValue method in the Before_Displayevent and the corresponding ctrl.setValue(proxy.field) in the JS Onload event. I found that setting $value['fieldname'} and $xt-> assign($name, $val) didn't cause the displayed field to be populated. I also tried the Custom option on the View as properties page, but that didn't work either, I was trying to do a preg_split on a database field and then a for next loop to extract the data from the preg_split output array - which was perhaps a little ambitious (which worked in the Before Display event.
  3. The SAVE function initially errored because there are fields on the page which do not have columns on the database table, the solution to this I was able to find in forum post that I remembered from my earlier searches: unset($values['field'] is required in the Before record updated event.
    Hopefully there is a way to get character data passed back to PHP....
    I foiund that the ctrl.AddValidation('IsRequired') does not work quite the same as the Required field checkbox on Fields Properties page. The checkbox displays a red by the field and a message if its empty. The AddValidation('IsRequired') on a field with the required checkbox unticked will display the message but not the . The RemoveValidation('IsRequired') on field that has the required checkbox ticked will remove the message but leave the *.

admin 3/29/2017

You should be able to use $values["fieldname"] in events like BeforeAdd and BeforeEdit to access field value exactly the way it was entered on the page.

T
thamestrader author 3/30/2017

Thanks Sergey, I've now cracked it.....and you are right its so much easier with dummy fields on the SQQL query, and also $values['fieldname'] is available in the before record updated event, with a calculated field it will correctly contain numeric data but if character data has been entered it will be empty or at least appear to be empty.
I was able to resolve this:
On the SQL Query page instead of using a calculated field I created two dummy fields using concat:
concat (" ") AS flatno,

concat (" ") AS building
These behave correctly as varchar fields and I could everything I needed to.
So finally to summarise:

  1. Use Concat for varchar dummy data fields OR calculated for numeric data.
  2. In Before Display event use setProxyValue to transfer values from the database record to populate the dummy field in JS Onload.

    3.in Before Record Update event after updating any data fields using the data in the dummy fields from $values['fieldname'] use unset($values[fieldname]) to remove the dummy fields as they will cause the update to fail as they have no related columns.
    When the RESET button is clicked it sets the two dummy fields to spaces, presumably because that's what they are in the SQL querty concat, not a problem.
    Thanks
    Norman