Correct way to use validator(Validate::required()) -> crashes

Correct way to use validator(Validate::required()) -> crashes

asleasle Posts: 107Questions: 30Answers: 0

When I use->validator(Validate::required()) my page crashes. It seems it complains about an empty array. Strange because this works:

Field::inst( 'publikasjon.fylke' )
        ->options(
                Options::inst()
                    ->table('fylke')
                    ->value('fylke')
                    ->label('fylke')
            )
->validator(Validate::required())
->validator(Validate::dbValues()),

When I change the validator to

Field::inst( 'publikasjon.fylke' )
        ->options(
                Options::inst()
                    ->table('fylke')
                    ->value('fylke')
                    ->label('fylke')
            )
->validator(Validate::required()) 

the page crashes.
Am I writing the code wrong?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 64,594Questions: 1Answers: 10,683 Site admin

    Can you show me the error message please?

    Thanks,
    Allan

  • asleasle Posts: 107Questions: 30Answers: 0

    DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see https://datatables.net/tn/1

    and I turned on debug and can see this

    Fatal error: Uncaught Error: Call to a member function dbField() on array in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php:1142 Stack trace: #0 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(1034): DataTables\Editor->_get(NULL, Array) #1 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(716): DataTables\Editor->_process(Array) #2 /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php(68): DataTables\Editor->process(Array) #3 {main} thrown in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php on line 1142

  • asleasle Posts: 107Questions: 30Answers: 0

    I am sorry but I did not see that the code I wrote was not correct but just two equal code blocks!. The problem is when I use validator(Validate::optional()) So this code crashes the page and gives the error I wrote in the last post

    Field::inst( 'publikasjon.fylke' )
            ->options(
                    Options::inst()
                        ->table('fylke')
                        ->value('fylke')
                        ->label('fylke')
                )
    ->validator(Validate::optional()) 
    
  • allanallan Posts: 64,594Questions: 1Answers: 10,683 Site admin
    edited June 19

    That's not where I would have expected such an error to show! This is the code in question it is in the get routine, which is what surprises me.

    Can you show me the full PHP for the Editor instance please?

    Also to confirm, you are saying using optional makes it crash, but required doesn't?

    Allan

  • asleasle Posts: 107Questions: 30Answers: 0
    edited June 20

    Yes optional makes it crash while required doesn´t. I don´t want the value to be required.. I am using the dropdown "fylke" with the text value as both value and label.

    With debug I see this in Chrome inspector:

    <b>Fatal error</b>:  Uncaught Error: Call to a member function dbField() on array in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php:1142
    Stack trace:
    #0 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(1034): DataTables\Editor-&gt;_get(NULL, Array)
    #1 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(716): DataTables\Editor-&gt;_process(Array)
    #2 /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php(69): DataTables\Editor-&gt;process(Array)
    #3 {main}
      thrown in <b>/home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php</b> on line <b>1142</b><br />
    

    Her is the full code for the php:

    <?php
    // DataTables PHP library
    include( "../lib/DataTables.php" );
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'publikasjon' )
        ->debug(true)
        ->fields(
            Field::inst( 'publikasjon.tittel' ),
            Field::inst( 'publikasjon.forfatter' ),
            Field::inst( 'publikasjon.litt_year' ),
            Field::inst( 'publikasjon.litt_type' ),
            Field::inst( 'publikasjon.litt_nr' ),
            Field::inst( 'publikasjon.litt_isbn' ),
            Field::inst( 'publikasjon.litt_ref' ),
            Field::inst( 'publikasjon.litt_sammendrag' ),
            Field::inst( 'publikasjon.fylke' )
            ->options(
                    Options::inst()
                        ->table('fylke')
                        ->value('fylke')
                        ->label('fylke')
                )
                ->validator(Validate::optional())
                ,
            Field::inst( 'publikasjon.kommune' ),
            Field::inst( 'publikasjon.tiltakstype' ),
            Field::inst( 'publikasjon.oppdragsgiver' ),
            Field::inst( 'publikasjon.image' )
                ->setFormatter( Format::ifEmpty( null ) )
                ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__NAME__' )
                    ->db( 'files', 'id', array(
                        'filename'    => Upload::DB_FILE_NAME,
                        'filesize'    => Upload::DB_FILE_SIZE,
                        'web_path'    => Upload::DB_WEB_PATH,
                        'system_path' => Upload::DB_SYSTEM_PATH
                    ) )
                    ->validator( Validate::fileSize( 10000000, 'Filen må ikke overstige 10MB' ) )
                    ->validator( Validate::fileExtensions( array( 'pdf','png', 'jpg', 'jpeg', 'gif' ), "Vennligst last opp en PDF eller et bilde" ) )
                    ->dbClean( function ( $data ) {
                        // Remove the files from the file system
                        for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                            unlink( $data[$i]['system_path'] );
                        }
    
                        // Have Editor remove the rows from the database
                        return true;
                    } )
                )
        )
        ->process( $_POST )
        ->json();
    
  • allanallan Posts: 64,594Questions: 1Answers: 10,683 Site admin
    Answer ✓

    Thank you! And apologies for having not seen the error before - there is no Validate::optional() method. There is an optional() on the ValidateOptions class though, which can be passed as the second parameter to Field->validator().

    I think this is what you want:

                ->validator(
                    Validate::dbValues(),
                    ValidateOptions::inst()->optional(true)
                )
    

    Allan

  • asleasle Posts: 107Questions: 30Answers: 0

    Thank you Allan! This works. I could not find out where to put "optional" from reading the manual page about validation . So I need the Validate::dbValues() line also? What I want is to allow for empty(no) values for the field.

  • allanallan Posts: 64,594Questions: 1Answers: 10,683 Site admin
    Answer ✓
    ->validator(
        Validate::dbValues(),
        ValidateOptions::inst()->allowEmpty(true)
    )
    

    Would be what you need to allow an empty string. The optional defines if the field must be submitted or not. allowEmpty defines that if it is submitted is it allowed to be empty.

    Allan

  • asleasle Posts: 107Questions: 30Answers: 0

    Thanks Allan! I tested and it worked fine with empty value with ValidateOptions::inst()->optional(true) Did not complain at least. But I will use the allowEmpty(true) as you suggest!

Sign In or Register to comment.