Don't save "select" value in database if select is not picked
- This topic has 5 replies, 2 voices, and was last updated 3 years, 8 months ago by
Long Nguyen.
-
AuthorPosts
-
August 29, 2021 at 11:55 PM #30489
Maria O.
ParticipantHi!
I have a group field with two subfields:
1) "Select" field
2) "Text" FieldLet'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!
August 30, 2021 at 6:12 PM #30503Long Nguyen
ModeratorHi Maria,
I think you can use the extension MB Conditional Logic to show the
select
field when thetext
field is not empty. Then mark them as Required.Get more details on the documentation https://docs.metabox.io/extensions/meta-box-conditional-logic/
September 1, 2021 at 12:40 AM #30526Maria O.
ParticipantSorry 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
September 1, 2021 at 5:22 PM #30536Long Nguyen
ModeratorHi,
So you can show the
text
field only the users select an option and markrequired
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 ] ],
September 1, 2021 at 10:33 PM #30542Maria O.
ParticipantBut 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
September 2, 2021 at 10:04 PM #30553Long Nguyen
ModeratorHi,
On my sample code above, there is no
required
setting of theselect
field. You just need to set this to thetext
field.Regarding the standard (Default) value, you can use the setting
placeholder
instead to prevent save the default value. -
AuthorPosts
- You must be logged in to reply to this topic.