select2 reset value
  • Hi!
    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?
  • There is a "first_option" parameter you can pass to your form field that will give you that empty option.
  • 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?

    Thanks!!
  • Actually, you can just use:
    $fields['lugar_nacimiento_provincia_id']['first_option'] = 'Select one...';
  • It saves "0" too instead of NULL value.
  • If you look at the options on your select, is there a 0 value for the first option or an empty value=""?
  • 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.
  • 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.
    http://docs.getfuelcms.com/general/models#hooks
  • 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!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!