What's new

Welcome to xCrud Community - Data Management and extended PHP CRUD

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

Issues with set_exception when called within before_update

ben74

New member
Joined
Dec 13, 2021
Messages
20
Reaction score
5
Points
3
Location
France
Hello guys,

I don't know if it's a bug or not, so hopefully someone will find out ;-)

So on my xcrud form I have a function that checks data validity. Of course it needs to be called both on inserts and updates, so it's called like that:
PHP:
$xcrud->before_insert('verify_departements_csv');
$xcrud->before_update('verify_departements_csv');

The function itself is located in xcrud's functions.php file:

PHP:
function verify_departements_csv($postdata, $xcrud){

    $dept_csv_error = FALSE;

    $depts_csv = $postdata->get('depts_csv_for_type');
    $depts_csv_array = explode(",", $depts_csv);
    
    foreach ($depts_csv_array as $dept_code) {
        if ( strlen($dept_code)<2 || !is_numeric($dept_code) || empty($dept_code) || is_null($dept_code) ) {
            $dept_csv_error = TRUE;
            break;
        }
    }
    
    if(str_contains($depts_csv, ',,')){
        $dept_csv_error = TRUE;
    }

    if ($dept_csv_error) {
        $xcrud->set_exception('depts_csv_for_type','Error in Syntax of CSV departements list!');
    }
    
}

Now comes the really weird (or maybe buggy) part :

- During the initial insert, it works perfectly...
- But if you edit an originally valid entry to add some invalid entry and save again, the following error message is displayed:

Fatal error: Uncaught Error: Call to a member function set_exception() on string in /home/user76567/public_html/xcrud/dashx/xcrud/functions.php:95 Stack trace: #0 /home/user76567/public_html/xcrud/dashx/xcrud/xcrud.php(3709): verify_departements_csv(Object(Xcrud_postdata), '14', Object(Xcrud)) #1 /home/user76567/public_html/xcrud/dashx/xcrud/xcrud.php(2017): Xcrud->_save() #2 /home/user76567/public_html/xcrud/dashx/xcrud/xcrud.php(1984): Xcrud->_run_task() #3 /home/user76567/public_html/xcrud/dashx/xcrud/xcrud.php(368): Xcrud->render() #4 /home/user76567/public_html/xcrud/dashx/xcrud/xcrud_ajax.php(3): Xcrud::get_requested_instance() #5 {main} thrown in /home/user76567/public_html/xcrud/dashx/xcrud/functions.php on line 95

Anyone has an idea of what is going on here? I've tried tons of variations but still get this error.

I totally don't get why it works on the initial insert and not after entry update...

Thanks a lot,
 

DaDo

Administrator
Staff member
Joined
Dec 1, 2021
Messages
108
Reaction score
23
Points
18
Hello guys,

I don't know if it's a bug or not, so hopefully someone will find out ;-)

So on my xcrud form I have a function that checks data validity. Of course it needs to be called both on inserts and updates, so it's called like that:
PHP:
$xcrud->before_insert('verify_departements_csv');
$xcrud->before_update('verify_departements_csv');

The function itself is located in xcrud's functions.php file:

PHP:
function verify_departements_csv($postdata, $xcrud){

    $dept_csv_error = FALSE;

    $depts_csv = $postdata->get('depts_csv_for_type');
    $depts_csv_array = explode(",", $depts_csv);
   
    foreach ($depts_csv_array as $dept_code) {
        if ( strlen($dept_code)<2 || !is_numeric($dept_code) || empty($dept_code) || is_null($dept_code) ) {
            $dept_csv_error = TRUE;
            break;
        }
    }
   
    if(str_contains($depts_csv, ',,')){
        $dept_csv_error = TRUE;
    }

    if ($dept_csv_error) {
        $xcrud->set_exception('depts_csv_for_type','Error in Syntax of CSV departements list!');
    }
   
}

Now comes the really weird (or maybe buggy) part :

- During the initial insert, it works perfectly...
- But if you edit an originally valid entry to add some invalid entry and save again, the following error message is displayed:



Anyone has an idea of what is going on here? I've tried tons of variations but still get this error.

I totally don't get why it works on the initial insert and not after entry update...

Thanks a lot,

You are unable to use the same function in update or insert try duplicate and pass this sure works!
PHP:
$xcrud->before_update('verify_departements_csv');

function verify_departements_csv_update($postdata, $primary, $xcrud){

    $dept_csv_error = FALSE;

    $depts_csv = $postdata->get('depts_csv_for_type');
    $depts_csv_array = explode(",", $depts_csv);
    
    foreach ($depts_csv_array as $dept_code) {
        if ( strlen($dept_code)<2 || !is_numeric($dept_code) || empty($dept_code) || is_null($dept_code) ) {
            $dept_csv_error = TRUE;
            break;
        }
    }
    
    if(str_contains($depts_csv, ',,')){
        $dept_csv_error = TRUE;
    }

    if ($dept_csv_error) {
        $xcrud->set_exception('depts_csv_for_type','Error in Syntax of CSV departements list!');
    }
    
}
 

ben74

New member
Joined
Dec 13, 2021
Messages
20
Reaction score
5
Points
3
Location
France
Thanks, I got it working by randomly trying around.

Indeed I needed 3 things:

1) Different function names

$xcrud->before_insert('verify_departements_csv');
$xcrud->before_update('verify_departements_csv_update');

2) The insert function with only 2 arguments
function verify_departements_csv($postdata, $xcrud){}

3) The update function with 3 arguments
verify_departements_csv_update($postdata, $primary, $xcrud){}

It was a huge pain to find out why but it's working now.

Hopefully we'll soon have tons of snippets in the forum.

Cheers,
 
Top Bottom