Foreign_keys field that can be null sometimes

edited April 2019 in Support

Hi,
I have a table with a field with foreign key, but it can be null sometimes or empty.
When I edit a record in the CMS it is not possible to select "nothing" in this field.
Can you advice me what to do to have such possibility to select null or empty for such field?
In the model I have defined this field with $foreign_keys.

Comments

  • You will need to modify the field in the form_fields method to include a "first_option" parameter:

    public function form_fields($values = array(), $related = array())
    {
         $fields = parent::form_fields($values, $related);
         ....
         $fields['my_foreign_key']['first_option'] = 'Select an option...';
         ....
         return $fields;
     }
    
  • edited April 2019

    It doesn't work. I've got this code.
    public function form_fields($values = array(), $related = array())
    {
    $fields = parent::form_fields($values, $related);
    $fields['parent_id']['first_option'] = 'Select one item or none ...';
    return $fields;
    }

  • Does the first option of the select not say "Select an option..."? Also, when you say it doesn't work, do you mean you can't select the empty value (e.g. Select an option) or do you get a validation error saying that it's required or something?

  • Sorry for the lack of precise info.
    The "Select an option..." appears as first option instead of default "Select one ...".
    If I select the "Select an option..." and click Save button the page reloads and message "Data has been saved." appears. But the content of the field doesn't change, like nothing happend.

  • So when you select the "no option" (e.g. Select an option.../ Select one...) and save, it still retains the same old values and does not set the foreign key in the database to null or 0?

Sign In or Register to comment.