Setting default value from server script

Setting default value from server script

cpshartcpshart Posts: 246Questions: 49Answers: 5

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi
I am using the Dependent fields example https://editor.datatables.net/examples/api/dependentFields.html

Each EQUITY_ACCOUNT has an associated portfolio_id, but CASH_ACCOUNTS do not associate with a portfolio_id, so I want
to hide the portfolio_id select2 field where
dm_cash_accounts.type = 'CASH_ACCOUNT'

otherwise show the portfolio_id select2 field

The editor is displaying the field as expected correctly where dm_cash_accounts.type = 'EQUITY_ACCOUNT', but where dm_cash_accounts.type = 'CASH_ACCOUNT' an error results on committing the row with the Edit Button

error display in chrome

I have defined a default value of 0 in the portfolio_id column in the database table dm_cash_accounts

but it is passing a value of '' from the server, unless I define a valid portfolio_id for the given user.

Can I ensure that if there is no selection of portfolio in the case of CASH_ACCOUNTS that a default value of say 0 is written to the field.

extract of client

$(document).ready(function() {  
    var siteEditor = new $.fn.dataTable.Editor({
        ajax: {
            url: "../../" + EDITOR_DIR + "/controllers/dview-cash_accounts.php",

... etc.
...
                label: "Name:",
                name: "dm_cash_accounts.name"
            }, {
                label: "Type:",
                name: "dm_cash_accounts.type",
                type:  "select2",
                options: [
                    { label: "Cash Account", value: "CASH_ACCOUNT"},
                    { label: "Equity Account", value: "EQUITY_ACCOUNT"}
                ],
                def: "CASH_ACCOUNT"             
            }, {
                label: "Portfolio:",
                name: "dm_cash_accounts.portfolio_id",
                         type: "select2",
                placeholder: "Select a portfolio",
                def: 0
            }
        ]
    });
    
    siteEditor.dependent( 'dm_cash_accounts.type', function ( val ) {
        return val === 'CASH_ACCOUNT' ?
            { hide: ['dm_cash_accounts.portfolio_id'] } :
            { show: ['dm_cash_accounts.portfolio_id'] };        
    } );

extract of server script

Editor::inst( $db, 'dm_cash_accounts' )

    ->debug(true)
    ->fields( 
        Field::inst( 'dm_cash_accounts.id' )->set(false),
etc ..

        Field::inst( 'dm_cash_accounts.portfolio_id' )
            ->options( Options::inst()
                ->table( 'dm_portfolios' )
                ->value( 'id' )
                ->label( array('code', 'name') )
                ->where( function ( $q ) use ( $userid) {
                    $q
                    ->where( 'dm_portfolios.user_id', $userid );
                } )
            ),

        Field::inst( 'dm_portfolios.code' )

            ->searchPaneOptions( SearchPaneOptions::inst()
                ->value( 'dm_portfolios.code')
                ->label( 'dm_portfolios.code' )
                ->leftJoin( 'dm_portfolios', 'dm_portfolios.id', '=', 'dm_cash_accounts.portfolio_id' )
                ->where( function ( $q ) use ( $userid) {
                    $q
                    ->where( 'dm_cash_accounts.user_id', $userid )
                    ->and_where('dm_portfolios.reporting_status', 'yes');
                } )
                ),
...etc

        
->leftJoin( 'dm_portfolios', 'dm_portfolios.id', '=', 'dm_cash_accounts.portfolio_id' )
                ->where( function ( $q ) use ( $userid) {
                $q->where( 'dm_cash_accounts.user_id', $userid);
                } )
->debug(true)
->process($_POST)
->json();

You can access my system to test if required, with thanks.

https://www.dividendview.co.uk/cash-accounts/

https://www.dividendview.co.uk/wp-admin/post.php?post=16207&action=edit

public_html/Editor-PHP-1.9.6/controllers/dview-cash_accounts.php

I can PM access details if you have not received them yet.

Many thanks

Colin

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Hi Colin,

    Can I ensure that if there is no selection of portfolio in the case of CASH_ACCOUNTS that a default value of say 0 is written to the field.

    Yes, use a server-side event. Specifically have a look at the section about settings values.

    Allan

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Allan

    Sorry, I have been offline all day, it makes sense to use a server side event, I have used this functionality before for another problem, so I will give it a try.

    Many Thanks

    Colin

This discussion has been closed.