Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation fails #4

Open
nakhor opened this issue Oct 31, 2019 · 1 comment
Open

Validation fails #4

nakhor opened this issue Oct 31, 2019 · 1 comment

Comments

@nakhor
Copy link

nakhor commented Oct 31, 2019

I've implemented the Select2AjaxField using a DataObject class as the source on a CMS form and it all works fine except for saving - the validation fails. Looking through the code it appears that the validate method is not overridden and ends up being handled by the SingleSelectField parent which checks the selection against what it knows as the list of valid values ( which in Select2AjaxField case is blank) and cannot find the selected value and so fails the validation.

I'd suggest the validate method should be overridden in Select2AjaxField and checks that the value is a valid member of the source DataList. Happy to contribute the changes of course (once I've done them)

@nakhor
Copy link
Author

nakhor commented Oct 31, 2019

Something like this should do it:

public function validate($validator)
{
    $value = $this->Value();
    $item = $this->getList()->find($this->getIDField(), $value);
    if($item!==null)return true;

    $validator->validationError(
        $this->name,
        _t(
            'SilverStripe\\Forms\\DropdownField.SOURCE_VALIDATION',
            "Please select a value within the list provided. {value} is not a valid option",
            array('value' => $value)
        ),
        "validation"
    );
    return false;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant