Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject Validation on AddForm or EditForm CMS

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2826 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    DataObject Validation on AddForm or EditForm CMS Link to this post

    I have a DataObject that can be added and edited in a ModelAdmin. I have a particular field in the CMS that must have a number entered.

    I tried using:

    public function getCMSValidator() {
    return new RequiredFields('FirstName', 'Surname', 'BestNotesID');
    }

    But when I submit the EditForm without the number I just get a blank grey area in the CMS_right area and this error message:

    validationError('BestNotesID', '\"Best Notes ID\" is required.', 'required'); statusMessage('Validation failed', 'bad');Behaviour.addLoader(hideLoading);

    So I can see that it does want to require the Fields but the behavior I get is unexpected. How do I handle the exception so that a nice error message is displayed and the EditForm is put back so the error can be corrected?

  • BLU42 Media
    Avatar
    Community Member
    71 Posts

    Re: DataObject Validation on AddForm or EditForm CMS Link to this post

    Did you ever get this figured out? I'm having the same issue with 2.3.3

    In digging through the code, it looks like FormReponse is just dumping back plain html and it's not being recognized as javascript even though Director::is_ajax() is returning true.

    Are there any js experts out there who want to take a crack at this?

    Thanks in advance!
    John

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: DataObject Validation on AddForm or EditForm CMS Link to this post

    The ModelAdmin form validation works a bit differently. It assumes the form to be returned as HTML, not JavaScript, and the validator interferes with that. I think you'll have to disable javascript validation for ModelAdmin forms (haven't tested though).

    class MyModelAdmin extends ModelAdmin {
    function init() {
    parent::init();
    Validator::set_javascript_validation_handler('none');
    }
    }

    In a development branch, we've already changed this behaviour to be more consistent: The assumed response will always be HTML, and validation errors are displayed in the HTML response, and picked up by jQuery selectors for JavaScript usage.

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: DataObject Validation on AddForm or EditForm CMS Link to this post

    Still using SS 2.3.3 and getCMSValidator() and getting unexpected results. I put Validator::set_javascript_validation_handler('none'); in my ModelAdmin. What happens is that if I submit a form that doesn't validate the Main CMS window goes grey, but if I click the button to add the object again that is when I see the error messages next to the required fields which are now blank.

    What I need it to do is just reload the add Form with the data still populated and the error messages showing so I can make the corrections and resubmit the add form.

    2826 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.