Allow empty or existing value not in list when submit editor form

Allow empty or existing value not in list when submit editor form

asleasle Posts: 110Questions: 31Answers: 0
edited June 22 in Editor

I have a <select> list in records from the main db publikasjon with values from a field isbn.litt_isbn. I pick the first available (=0) value from the DB field as an option and after submitting it is marked used (=1). This works. But I want to be able to accept existing field value and also no(empty) value. I have problems getting theValidate::: working and I get an error when submitting

<b>Fatal error</b>
:  Uncaught Error: Call to a member function optional() on string in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Validate.php:174
Stack trace:
#0 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Validate.php(1032): DataTables\Editor\Validate::_common('\xEF\xBB\xBF978-82-345-0...', 'isbn')
#1 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Field.php(778): DataTables\Editor\Validate::DataTables\Editor\{closure}('\xEF\xBB\xBF978-82-345-0...', Array, Object(DataTables\Editor\Field), Array)
#2 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(848): DataTables\Editor\Field-&gt;validate(Array, Object(DataTables\Editor), '4096')
#3 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(1071): DataTables\Editor-&gt;validate(Array, Array)
#4 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(716): DataTables\Editor-&gt;_process(Array)
#5 /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php(118): DataTables\Editor-&gt;process(Array)
#6 {main}
  thrown in <b>/home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Validate.php</b>
on line <b>174</b>
<br/>

Her is the field in js:

{
    label: 'ISBN:',
    name: 'publikasjon.litt_isbn',
    type: "select",
    placeholder: 'Ingen ISBN valgt',
    placeholderDisabled: false
},                   //other fields after this...

And the php code for the field:

Field::inst( 'publikasjon.litt_isbn' )
            ->options(function($db, $field) {
                $options = [];
                // Get the first available ISBN with status = 0
                $row = $db
                    ->query('select')
                    ->get(['isbn'])
                    ->table('isbn')
                    ->where('status', 0)
                    ->order('id ASC')
                    ->limit(1)
                    ->exec()
                    ->fetch();

                if ($row) {
                    $options[] = [
                        'label' => $row['isbn'],
                        'value' => $row['isbn']
                    ];
                }

                // Safely get the current value
                $current = null;
                if ($field !== null && method_exists($field, 'val')) {
                    $current = $field->val(); 
                }

                // If it's not the same as the one above, and it's not already in the list, add it
                if (!empty($current) && (!isset($row['isbn']) || $row['isbn'] !== $current)) {
            $options[] = [
                'label' => $current . ' (tidligere valgt)',
                'value' => $current
            ];
        }
                return $options;
                })
                ->validator(
                Validate::dbValues('isbn', 'isbn'),
                ValidateOptions::inst()->allowEmpty(true)
            )
        ,

Is this the correct way to do the sql to get the first field? I thin there is something I dont have correct in the Validate part but I can not find it out. So my goal is 1) Show current value if exists and accept it 2) Show first available number from isbn.isbn field. 3) Accept empty value on submit

Answers

  • asleasle Posts: 110Questions: 31Answers: 0
    edited June 22

    I think the error is with the Validate. It does not get the value to validate. When I run this

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

    I get an error:
    Table for database value check is not defined for field publikasjon.litt_isbn
    But if I try this:

    ->validator(
            Validate::dbValues(),
            ValidateOptions::inst()
                ->table('isbn')
                ->field('isbn')
                ->allowEmpty(true)
        )
    

    I get this error:

    Fatal error: Uncaught Error: Call to undefined method DataTables\Editor\ValidateOptions::table() in /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php:78 Stack trace: #0 {main} thrown in /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php on line 78

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

    The ValidateOptions class doesn't have table or field methods. See the API reference docs for the class here.

                ->validator(
                    Validate::dbValues('isbn', 'isbn'),
                    ValidateOptions::inst()->allowEmpty(true)
                )
    

    from your first post looks more correct to me. You need to specify the table / column information for the validator since it can't derive it automatically from the options function (since it is a custom options function - which looks fine).

    What happens if you use that?

    Allan

  • asleasle Posts: 110Questions: 31Answers: 0
    edited 9:14AM

    I tried your suggestion now and when I try to add a value or no value and update I get this:

    <b>Fatal error</b>
    :  Uncaught Error: Call to a member function optional() on string in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Validate.php:174
    Stack trace:
    #0 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Validate.php(1032): DataTables\Editor\Validate::_common('\xEF\xBB\xBF978-82-345-0...', 'isbn')
    #1 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Field.php(778): DataTables\Editor\Validate::DataTables\Editor\{closure}('\xEF\xBB\xBF978-82-345-0...', Array, Object(DataTables\Editor\Field), Array)
    #2 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(848): DataTables\Editor\Field-&gt;validate(Array, Object(DataTables\Editor), '4096')
    #3 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(1071): DataTables\Editor-&gt;validate(Array, Array)
    #4 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(716): DataTables\Editor-&gt;_process(Array)
    #5 /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php(116): DataTables\Editor-&gt;process(Array)
    #6 {main}
      thrown in <b>/home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor/Validate.php</b>
    on line <b>174</b>
    <br/>
    

    The value I am sending to the table publikasjon while the select value is coming from table isbn . Is there some logic I am missing? Also The existing value (if present) does not show. It shows always "Ingen ISBN valgt" if I leave the placeholder code in the field definition:

    {
        label: 'ISBN:',
        name: 'publikasjon.litt_isbn',
        type: "select",
        placeholder: 'Ingen ISBN valgt',
        placeholderDisabled: false
    },
    
Sign In or Register to comment.