Editor - Inline Select Field Options

Editor - Inline Select Field Options

stuartsjb-icrstuartsjb-icr Posts: 59Questions: 12Answers: 0
edited April 2016 in Editor

Just a couple of quick questions regarding how Editor works with Select box options.

A. I'm using the Editor PHP library to populate the select boxes in my tables, e.g. :

Field::inst('ModuleCourseworkMarks.MarkerAID')
    ->options('Markers', 'ID', 'Name')

and this works great, providing all the IDs and Name values from the requested table. What it doesn't provide me with is the ability to create a NULL value in the options list, allowing the end user to effectively de-select an option entirely. Is it possible to inject one at the top of the list before all the database values? I'd like to avoid adding a 0 or NULL value in the database if possible.

B. I've found that when using in-line editing that the first option in the list is effectively un-selectable: although the first entry from the database appears as the default option when clicking in the cell, it does not submit it if you press enter, tab, or click out of the cell. I can see this as being desired behaviour 99% of the time (as you wouldn't necessarily want that first option to be saved for every entry when just tabbing through the cells), but it prevents that first option being selected (unless a different one is selected first!).

I expect that being able to add a NULL "SELECT FROM LIST" value as the first option in the list would also rectify the problem I have in question 2, if anyone knows how to do this I'd be ever so grateful.

This question has an accepted answers - jump to answer

Answers

  • stuartsjb-icrstuartsjb-icr Posts: 59Questions: 12Answers: 0
    edited April 2016

    I just found a previous thread where Allan addresses this by using array_unshift on the PHP options array, but adding this to my sever-side script:

    array_unshift(
        $data['options']['ModuleCourseworkMarks.MarkerAID'],
        [ "label" => "SELECT", "value" => NULL ]
    );
    

    ..results in an unsuccessful submission and this PHP log error whenever selecting from the resulting select list:

    [12-Apr-2016 12:30:20 Europe/London] PHP Warning: array_unshift() expects parameter 1 to be array, null given

    I feel like I'm really close to getting the result I want now, but even changing the NULL value to an empty string or a zero results in in the same error.

  • stuartsjb-icrstuartsjb-icr Posts: 59Questions: 12Answers: 0
    edited April 2016

    I believe I have finally solved my own problem: of course the $data array is being cleared and the options are not being passed back to the PHP when submitting data to be saved, and so there is no 'options' array present to pre-pend to when saving.

    I've added an if clause to my server-side script as follows and this has provided me with the functionality I was seeking. Hopefully this will come in useful to others in future and save them some time!

    if (array_key_exists('options', $data)) {
        array_unshift ($data['options']['ModuleCourseworkMarks.MarkerAID'], ["label" => "SELECT", "value" => NULL]);
        array_unshift ($data['options']['ModuleCourseworkMarks.MarkerBID'], ["label" => "SELECT", "value" => NULL]);
        array_unshift ($data['options']['ModuleCourseworkMarks.MarkerCID'], ["label" => "SELECT", "value" => NULL]);
    }
    
  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Answer ✓

    Since Editor 1.5.4 there is a much easier way of doing it rather than messing around with arrays in PHP - you can use the placeholder and placeholderValue options for the select field type.

    Just set the placeholder to something like "No value selected" and the placeholder value to be an empty string. You can then use a formatter on the server-side (ifEmpty) to convert the empty string to null if you need null on the database.

    Regards,
    Allan

  • stuartsjb-icrstuartsjb-icr Posts: 59Questions: 12Answers: 0

    Ah no, that's so annoying! I spent hours scratching my head on this one today, too.

    Thanks Allan. I've updated my Editor field definitions with the placeholders and it's working nicely. Adding the setFormatters to the PHP means they're updating properly with NULLs instead of zeros too.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Good to hear that is working well.

    I'm working on a new site search which I hope will make finding this sort of information much easier. Hoping to deploy it soon.

    Allan

This discussion has been closed.