Validation only after editor.dependant makes the field available

Validation only after editor.dependant makes the field available

rob1strob1st Posts: 84Questions: 22Answers: 0
edited August 2022 in Editor

Hi, I tried following this thread which was making a select field required if visible.
https://datatables.net/forums/discussion/66081/how-to-use-required-validation-on-editor-dependent-show-function

I wish to make a number field required when visible, I know i need the preEdit, but when I add it in, it doesn't seem to work.
I am all up to date with my versions.

server side code for the dependent field (statusID) and the field to be required if visible (sharePointID) are (showing only the fields in question).

Editor::inst( $db, 'asset A', 'A.id' )
Field::inst( 'A.assetStatus', 'statusID' )
        ->options( Options::inst()
            ->table('assetstatus')
            ->value('id')
            ->label('assetStatus')
            ->order('statusOrder')
        )
        ->validator( Validate::dbValues() )
        ->validator( Validate::notEmpty( ValidateOptions::inst()
        ->message( 'What status are we allocating?' )
        ) ),
Field::inst( 'A.sharePointID', 'sharePoint' ), 

The preEdit is:

->on('preEdit', function ($editor, $values) {
        if ($values['asset']['assetStatus'] == 12) {
            $editor->field('sharePoint')->validator( Validate::notEmpty(ValidateOptions::inst()
                ->message('Please enter the ID of the sharepoint file you uploaded.  It can be found on the far right of the folder you loaded the file in to?')
            ));
            }
        }
    )

I am assuming it is the if with $value not being picked up, the table is 'asset' and the column is 'assetStatus'. I have tried just changing this to the alias of ['statusID'] but doesn't make any difference. I assume I am making a sill mistake that I cannot see.

The showing and hiding of sharePointID works perfectly, just that when I select update when it is empty it takes it without the validation.

If you cannot see any issues I will set it up on a test page to show you, but I am assuming it's an ID10T error at the moment!

appreciate the help,

Answers

  • rob1strob1st Posts: 84Questions: 22Answers: 0

    Found it, didn't add $id or & before $value, then worked.

    working code for others:

    ->on('preEdit', function ($editor, $id, &$values) {
            if ($values['statusID'] == 12) {
                $editor->field('sharePoint')->validator( Validate::notEmpty(ValidateOptions::inst()
                    ->message('Please enter the ID of the sharepoint file you uploaded.  It can be found on the far right of the folder you loaded the file in to?')
                ));
                }
            }
        )
    
  • allanallan Posts: 61,627Questions: 1Answers: 10,090 Site admin

    Yup, that will do it - otherwise the arguments aren't what you'd expect. The & probably isn't required in this case, but it will do no harm. It causes PHP to use pass by reference, so you can modify the original object, rather than its default of copy on write.

    Allan

Sign In or Register to comment.