nicklockwood / FXForms

[DEPRECATED]
Other
2.93k stars 339 forks source link

Cannot select Placeholder field #449

Open realenginerd opened 8 years ago

realenginerd commented 8 years ago

I have a form where one of the fields is a "multiple choice":

var key: Key?
private var keys: [Key]

func keyField() -> [String: AnyObject]
{
    let form: [String: AnyObject] = [
        FXFormFieldPlaceholder: "No key",
        FXFormFieldOptions: smartkeys!
    ]

    if let nonNilKey = key
    {
        form[FXFormFieldDefaultValue] = nonNilKey
    }

    return form
}

The form appears correctly. "No key" appears at the top of the list, and is selected (marked with a check mark) if key is nil. When I click on a different key, it gets assigned to key correctly, and the check mark migrates accordingly.

When I try to click back on "No key", however, nothing happens. The (placeholder) cell is briefly highlighted, but the check mark does not migrate. The 'key' field still contains the old value, and does not get reset to nil. What should I do to get it to work this way? Is this supported in FXForms?

realenginerd commented 8 years ago

Further observation: seems as though Placeholder and Default Value doesn't work well together; I can only get one or the other. I can select the Placeholder with no problems if there is no Default Value specified. However, if I cannot specify a Default Value, the form does not get populated correctly.

On the other hand, if a Default Value is specified, the opposite problem occurs: the form shows up correctly -- default value is selected -- but I cannot select the Placeholder.

Am I holding this wrong? Is there a way to make this work? Please advise. Thanks in advance!

coybit commented 7 years ago

I have solved this problem by adding

if ([value unsignedIntegerValue] == NSNotFound) return nil;

to 'value' getter:

- (id)value
{
    if (FXFormCanGetValueForKey(self.form, self.key))
    {
        id value = [(NSObject *)self.form valueForKey:self.key];
        if (value && self.options)
        {
            if ([self isIndexedType])
            {
                if ([value unsignedIntegerValue] == NSNotFound) return nil;
                if ([value unsignedIntegerValue] >= [self.options count]) value = nil;
            }
            else if (![self isCollectionType] && ![self.type isEqualToString:FXFormFieldTypeBitfield])
            {
                //TODO: should we validate collection types too, or is that overkill?
                if (![self.options containsObject:value]) value = nil;
            }
        }
        if (!value && self.defaultValue)
        {
            self.value = value = self.defaultValue;
        }
        return value;
    }
    return self.defaultValue;
}