Validate on database using dbValues

Validate on database using dbValues

tchristforttchristfort Posts: 22Questions: 7Answers: 0

I am trying to validate a customer number on the customer table. As far as I can read on this forum This should be the correct validator:

Field::inst( 'analysis.customer')
->validator( 'Validate::dbValues', array(
'table' => 'customer',
'field' => 'number',
'message' => 'My error message'
)),

Uncaught Error: Call to a member function value() on null in /var/www/html/weight/php/Editor/Validate.php:

This question has an accepted answers - jump to answer

Answers

  • tchristforttchristfort Posts: 22Questions: 7Answers: 0

    I am trying to validate a customer number on the customer table. As far as I can read on this forum This should be the correct validator:

    Field::inst( 'analysis.customer')
    ->validator( 'Validate::dbValues', array(
    'table' => 'customer',
    'field' => 'number',
    'message' => 'My error message'
    )),

    however I get this error server side:

    Uncaught Error: Call to a member function value() on null in /var/www/html/weight/php/Editor/Validate.php:

    joining the tables with an option works:

    Field::inst( 'analysis.customer')
    ->options( Options::inst()
    ->table( 'customer')
    ->value( 'number')
    ->label( 'name')
    )
    ->validator( 'Validate::dbValues', array(
    'message' => 'My error message',
    ) ),

    But this is not convenient, as I will then be sending the large customer base to the client. Any clues to why the first solution is not working?

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin
    Answer ✓

    Try this:

    Field::inst( 'analysis.customer' )
      ->validator( Validate::dbValues( 
            ValidateOptions::inst()
                ->message( 'My error message' ),
            'number',
            'customer'
      ) )
    

    Using dbValues as a string name function is the old way of doing (although it should work still, so I'll look into that). The documentation here shows the full function signature.

    Allan

  • tchristforttchristfort Posts: 22Questions: 7Answers: 0

    Working fine with above validator, I will use new notatino going forward,
    Thank you Allan,

This discussion has been closed.