how add validator field dynamicaly in the php file

how add validator field dynamicaly in the php file

UMR5558UMR5558 Posts: 41Questions: 13Answers: 0

Hello everybody.
In my edotor i add somme fields dynamicaly with add() fonction in a loop. I would like to add validator in my php file for all the fields adding in the js file. How can i add dynamicaly in a loop the validator for every fields adding.
Thanks for your help.
Lionel

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    edited June 2023

    Just define the Editor fields in PHP. In the validator check whether or not the field is set and then do the validation.

    Here is an example from my own coding: Only if "sub.sub_name" is set is a real validation being executed:

    Editor::inst( $db, 'ctr' )
    ->field(
        Field::inst( 'ctr.id' )->set( false ),
        Field::inst( 'ctr.is_subsidy_register' )->set(Field::SET_CREATE)
                                                ->setValue( 1 ),
        Field::inst( 'sub.sub_name' )
            ->validator( function ( $val, $data, $opts ) use ( $msg, $signsLit ) {
                if ( isset($val) ) {
                    if ( $val <= '' ) {
                        return $msg[0];
                    }
                    if ( strlen($val) > 100 ) {
                        return $msg[6] . 100 . $signsLit;
                    }
                }
                return true;
            } ),
    
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Just to add to rf1234's response. You must do validation at the server-side (or to wherever you are submitting your data), so the approach I usually take with Editor is to just ignore the client-side validation - an Ajax submission is virtually free compared to the developer time needed to duplicate and maintain two sets of validation.

    You can dynamically add fields on the server-side as well. The exact answer will depend upon how you are adding and naming the fields and how the server-side handles them.

    Allan

  • UMR5558UMR5558 Posts: 41Questions: 13Answers: 0
    edited June 2023

    Thank you for your answers.
    I do validation in the server side by using Field::inst and -> validator.

    In the client side (js file) i add fields with this loop:

    for (var j = 1; j < parseInt(new_number_of_line)+1; j++) {
                        var line;
                        line = '<fieldset class="data_life_table'+j+'">'+
                        '<legend>Data life table '+j+'</legend>'+
                        '<editor-field name="data_life_table'+j+'.mean_parameter"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.sd_parameter"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.n_dead"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.end_age"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.life_table_id"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.modeled_mean_parameter"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.n_alive"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.start_age"></editor-field>'+
                        '</fieldset>';
        
                        
                        $('.data_life_table').append(line);
            
            
                        editor_life_table.add({
                            label: "mean_parameter:",
                            name: "data_life_table"+j+".mean_parameter",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "sd_parameter:",
                            name: "data_life_table"+j+".sd_parameter",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "n_dead:",
                            name: "data_life_table"+j+".n_dead",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "end_age:",
                            name: "data_life_table"+j+".end_age",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "life_table_id:",
                            name: "data_life_table"+j+".life_table_id",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "modeled_mean_parameter:",
                            name: "data_life_table"+j+".modeled_mean_parameter",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "n_alive:",
                            name: "data_life_table"+j+".n_alive",
                            className: 'sample_odd'
                        });
                        editor_life_table.add({
                            label: "start_age:",
                            name: "data_life_table"+j+".start_age",
                            className: 'sample_odd'
                        });
                    }
    

    And the ajax submission is like this

    data[0][data_life_table][number]: 3
    data[0][data_life_table1][mean_parameter]: 1
    data[0][data_life_table1][sd_parameter]: 1
    data[0][data_life_table1][n_dead]: 1
    data[0][data_life_table1][end_age]: 1
    data[0][data_life_table1][life_table_id]: 1
    data[0][data_life_table1][modeled_mean_parameter]: 1
    data[0][data_life_table1][n_alive]: 1
    data[0][data_life_table1][start_age]: 1
    data[0][data_life_table2][mean_parameter]: 2
    data[0][data_life_table2][sd_parameter]: 2
    data[0][data_life_table2][n_dead]: 2
    data[0][data_life_table2][end_age]: 2
    data[0][data_life_table2][life_table_id]: 2
    data[0][data_life_table2][modeled_mean_parameter]: 2
    data[0][data_life_table2][n_alive]: 2
    data[0][data_life_table2][start_age]: 2
    data[0][data_life_table3][mean_parameter]: 3
    data[0][data_life_table3][sd_parameter]: 3
    data[0][data_life_table3][n_dead]: 3
    data[0][data_life_table3][end_age]: 3
    data[0][data_life_table3][life_table_id]: 3
    data[0][data_life_table3][modeled_mean_parameter]: 3
    data[0][data_life_table3][n_alive]: 3
    data[0][data_life_table3][start_age]: 
    action: create
    

    I would like to add in the server side the number of Field::inst and -> validator with the good name. Maybe by a loop varying from 1 to data[0][data_life_table][number].

    I don't know if it's possible and how i can do that.

    Thanks thanks a lot.
    Lionel

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • UMR5558UMR5558 Posts: 41Questions: 13Answers: 0
    edited June 2023
      line = '<fieldset class="data_life_table'+j+'">'+
                        '<legend>Data life table '+j+'</legend>'+
                        '<editor-field name="data_life_table'+j+'.mean_parameter"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.sd_parameter"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.n_dead"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.end_age"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.life_table_id"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.modeled_mean_parameter"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.n_alive"></editor-field>'+
                        '<editor-field name="data_life_table'+j+'.start_age"></editor-field>'+
                        '</fieldset>' 
    

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Can you show me how you are defining the fields on the server-side please? Are you using a loop or something? Or is the problem not just the validator, but that you don't know how to add that number of fields?

    If you add a dynamic number of fields, how are you storing it in the database? Say your database had 10 columns available for these dynamic values, but the user submitted 11 - what happens?

    Allan

  • UMR5558UMR5558 Posts: 41Questions: 13Answers: 0

    I made a logic error while trying to improve my code.
    Your answer made me realize this error. I'm sorry for the time you wasted with my question.
    I would certainly have another question but I have to find the words to make you understand what I do and for what purpose.
    Thank you very much for everything.
    HL

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    No worries. Good to hear you got that one sorted.

    Allan

Sign In or Register to comment.