Don't save "select" value in database if select is not picked

Support General Don't save "select" value in database if select is not picked

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #30489
    Maria O.Maria O.
    Participant

    Hi!

    I have a group field with two subfields:
    1) "Select" field
    2) "Text" Field

    Let's imagine select have the followings options:

    
    'options'         => array(
            'java'       => 'Java',
            'javascript' => 'JavaScript',
            'php'        => 'PHP',
        ),
    

    I wan't to save in the database "select" and "text" value group only if "text" value is not empty. How can achieve that?

    Thanks!

    #30503
    Long NguyenLong Nguyen
    Moderator

    Hi Maria,

    I think you can use the extension MB Conditional Logic to show the select field when the text field is not empty. Then mark them as Required.

    Get more details on the documentation https://docs.metabox.io/extensions/meta-box-conditional-logic/

    #30526
    Maria O.Maria O.
    Participant

    Sorry but this doesn't fit my needs.

    I need to pick one option in select menu and after that fill the text field. In my example (that is not the case in my project), the select could be "javascript" and the text field the "version". I need to select a language before fill the text field.

    In case the user doesn't touch the select, I don't want to save anything in the database. Now if a user doesn't select anything in the database the default select value is saved and the text field not.

    Thanks

    #30536
    Long NguyenLong Nguyen
    Moderator

    Hi,

    So you can show the text field only the users select an option and mark required for the text field. Is that easier?

    Here is the sample code

    'fields'     => [
        [
            'name'            => 'Select',
            'id'              => $prefix . 'select',
            'type'            => 'select',
            'options'         => [
                'java'       => 'Java',
                'javascript' => 'JavaScript',
                'php'        => 'PHP',
                'csharp'     => 'C#',
            ],
            'placeholder'     => 'Select an Item',
        ],
        [
            'name'            => 'Text',
            'id'              => $prefix . 'text',
            'type'            => 'text',
            'visible'         => [ 'select', '!=', '' ],
            'required'        => true
        ]
    ],
    #30542
    Maria O.Maria O.
    Participant

    But I don't want the select to be required. In my project, the user could not select anything, the same way he can leave a text field empty.

    I would suggest for Meta Box developers: like when you leave an empty "text field" it is not saved in the database, It could be good that when the user doesn't touch the "select field" it is not saved as well (unless you have filled a Default Value). How is developed now, a default value is saved, regardless of whether we have put a default value for the select or not.

    Thanks

    #30553
    Long NguyenLong Nguyen
    Moderator

    Hi,

    On my sample code above, there is no required setting of the select field. You just need to set this to the text field.

    Regarding the standard (Default) value, you can use the setting placeholder instead to prevent save the default value.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.