Validation Syntax

Validation Syntax

hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1

Can someone provide the correct syntax for the following:

Editor::inst( $db, 'Words', 'WCount' )
    ->fields(
        Field::inst( 'Word' )
            ->validator( Validate::unique( new ValidateOptions::inst()
                ->message( 'Duplicate Entry') ),
        Field::inst( 'Type'),
        Field::inst( 'Phonetic' ),
        Field::inst( 'Definition' )
    )
    ->process( $_POST )
    ->json();

I'm trying to append an error message "Duplicate Entry" through the validator. No go. I get the following error:

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

If I delete the following text
new ValidateOptions::inst()
->message( 'Duplicate Entry')

all otherwise works properly.

Any help much appreciated.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    You're missing the closing bracket for validator:

                ->validator( Validate::unique( new ValidateOptions::inst()
                    ->message( 'Duplicate Entry') ) ),
    
  • hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1

    Still getting error after inserting the code, above.

    DataTables warning: table id=words - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @hamlet1964 ,

    Looking at the examples on this page it looks like you don't need the new before ValidateOptions.

    Hope that does the trick,

    Cheers,

    Colin

  • hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1

    Thanks for the suggestion, Colin, however, that does not work, either.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    ->validator(
      Validate::unique(
        ValidateOptions::inst()
          ->message( 'Duplicate Entry')
      )
    ),
    

    should do it. If that isn't working for you, can you show me the response from the server (i.e. what the invalid JSON is) - likely it contains an error message.

    My guess is that ValidateOptions isn't in your use statement.

    Allan

  • hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1

    Allan - still not working - here's the response i received:

    <br />
    <b>Fatal error</b>:  Uncaught Error: Class 'ValidateOptions' not found in /Applications/XAMPP/xamppfiles/htdocs/ourpage/words/php/table.words.php:26
    Stack trace:
    #0 {main}
      thrown in <b>/Applications/XAMPP/xamppfiles/htdocs/ourpage/words/php/table.words.php</b> on line <b>26</b><br />
    

    Here is the entire PHP file:

    <?php
    
    /*
     * Editor server script for DB table table1
     * Created by http://editor.datatables.net/generator
     */
    
    // DataTables PHP library and database connection
    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;
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'Words', 'WCount' )
        ->fields(
            Field::inst( 'Word' )
                ->validator( Validate::unique(
        ValidateOptions::inst()
          ->message( 'Duplicate Entry'))),
            Field::inst( 'Type'),
            Field::inst( 'Phonetic' ),
            Field::inst( 'Definition' )
        )
        ->process( $_POST )
        ->json();
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Perfect - thanks. As I suggested above you don't have a ValidateOptions definition in your use statement. Add that and I would expect it to work - e.g.:

    DataTables\Editor\ValidateOptions
    

    Allan

  • hamlet1964hamlet1964 Posts: 43Questions: 8Answers: 1

    Yes, this now works! Thanks!!

This discussion has been closed.