select2 reset value

edited January 2018 in Feature Requests
I'm using the "select2" type of field for several fields in my form.
Everything works as expected, but once a value is selected and saved i cannot "empty" or reset the value anymore.
I get an "Add" and a "Edit" buttons beside the dropdown, but I need a "reset" button too.
Those fields are not required.

Any help?
How can i do this?


  • Normally, you'd edit the list of items such that you had a blank one with an empty value?
  • edited 11:49AM
    There is a "first_option" parameter you can pass to your form field that will give you that empty option.
  • edited 11:49AM
    Thank you for your answers!
    I don't want to add a "no value" record to the table.
    I used the "first_option" parameter, as suggested by admin, but i had to implement the "on_before_save" function to replace the "0" value to NULL. Otherwise, the "0" is saved.

    $fields['lugar_nacimiento_provincia_id']['first_option'] = array(0=>' ');

    Is there a better way to do that in order to avoid the on_before_save changes?

  • edited 11:49AM
    Actually, you can just use:
    $fields['lugar_nacimiento_provincia_id']['first_option'] = 'Select one...';
  • edited 11:49AM
    It saves "0" too instead of NULL value.
  • edited 11:49AM
    If you look at the options on your select, is there a 0 value for the first option or an empty value=""?
  • edited 11:49AM
    There is no value attribute for that option in the select but it keeps saving a "0".
    And no default value for that field in the db.
  • edited 11:49AM
    What type of field is it in the database? If it's an int type field, an empty string is the equivalent to a "0". If you don't want a "0" then you'll need to set the default value to NULL. You may also need to add some code in an on_before_save model hook to convert empty strings to NULL values.
  • edited 11:49AM
    Everything looks ok but it keeps saving "0".
    It's OK, it's not a big problem keeping the on_before_save hooks I implemented.
    Thanks for your help!
Sign In or Register to comment.