Field validation for mjoin multi select field

Field validation for mjoin multi select field

rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

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

->join(
Mjoin::inst( 'govdept' )
    ->link( 'report.id', 'report_has_govdept.report_id' )
    ->link( 'govdept.id', 'report_has_govdept.govdept_id' )
    ->order( 'govdept.name asc' )
    ->fields(
        Field::inst( 'id' )->set( false )                
            ->options( Options::inst()
                ->table('govdept')
                ->value('id')                    
                ->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 govdept.id    
                                FROM user, govdept_has_user, govdept  
                                WHERE user.id = :id                             AND  
                                      user.id = govdept_has_user.user_id        AND  
                                      govdept_has_user.govdept_id = 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;
    }
} )

Replies

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    @allan
    any idea?

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    Hi,

    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.

    Allan

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

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

This discussion has been closed.