Field validation for mjoin multi select field

Field validation for mjoin multi select field

rf1234rf1234 Posts: 628Questions: 37Answers: 100

I couldn't integrate a field validator into my mjoin. I would like to make the user select at least one department from a dropdown. I ended up using a global validator but I don't like that. Is there any way to get a field validator into this?

This is my Javascript

    label: lang === 'de' ? 'Abteilungsauswahl:' : 'Department selection:',
    name: "govdept[].id", //render gov_name, govdept_name, (regional_12)
    type: "selectize",
    opts: {
        create: false,
        maxItems: null,
        openOnFocus: true,
        allowEmptyOption: false,
        placeholder: lang === 'de' ? 'Bitte wählen Sie eine oder mehrere Abteilung(en)' : 'Please select one or more Department(s)',

And the PHP mjoin

Mjoin::inst( 'govdept' )
    ->link( '', 'report_has_govdept.report_id' )
    ->link( '', 'report_has_govdept.govdept_id' )
    ->order( ' asc' )
        Field::inst( 'id' )->set( false )                
            ->options( Options::inst()
                ->label( 'name' )
                ->render( function ( $row ) {   
                    return getFormatterGovdeptOptions($row['id']);
                } )
                ->order( 'name asc' )
                //where clause MUST be a closure function in Options!!!
                ->where( function($q) {
                    //the user does not need more than reading rights for the
                    //respective department
                    $q ->where( function($r) {
                        $r  ->where( 'id',  
                            '( SELECT DISTINCT    
                                FROM user, govdept_has_user, govdept  
                                WHERE = :id                             AND  
                             = govdept_has_user.user_id        AND  
                                      govdept_has_user.govdept_id = 
                                )', 'IN', false);
                        $r  ->bind( ':id', $_SESSION['id'] );
                } )
        Field::inst( 'name' )->set( false ) 

And the global validator I use right now and which I would like to replace with a field validator:

->validator( function ( $editor, $editorAction, $data ) {
    if ( $editorAction === Editor::ACTION_CREATE || 
         $editorAction === Editor::ACTION_EDIT       ) {
        foreach ( $data['data'] as $pkey => $values ) {
            if ( $values['govdept-many-count'] <= 0 ) {
                return 'Please select at least one department.';
        return null;
} )


  • rf1234rf1234 Posts: 628Questions: 37Answers: 100

    any idea?

  • allanallan Posts: 46,964Questions: 1Answers: 6,603 Site admin


    Sorry for the delay - I missed this one completely. Have a look at this thread which discusses the same issue. Using a global validator to check if any items are submitted or not is the correct way to do it in the libraries at this time.


  • rf1234rf1234 Posts: 628Questions: 37Answers: 100

    ok, if that's the only way I got it right already. Thanks, Allan.

Sign In or Register to comment.