Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

CustomRequiredField used in CMS backend form validation


Reply


6 Posts   2309 Views

Avatar
Ben_W

Community Member, 80 Posts

1 September 2010 at 6:24pm

In the past, I have been trying to find a way to add custom validation for the backend and failed. I have searched through the forum and come cross many people like me. I am confident to say that many of you would agree that you can not rely on administrator to 'do the right thing'. No matter how many time you train them or remind them, accident do happen from time to time. I am in pain, because SilverStripe, in many area, is very promising, fast growing cms. So ... I waited, hoping the 2.4 will grant developer the ability to add custom validation. I really had high hope for it, as I have heard it would have some dramatic changes coming to play.

At first glance, 2.4.1 looks promising. I love the multi level url. That is one thing that our SEO guru complained about many times over. Security has been strengthened and more privilege options for developer to play with.

then I see the getCMSValidator(), this function allow you to add additional validator. If I use RequiredFields then it works all right, all error message shown on the page when you hit save button, yet as soon as I take one step further, ie using customeRequiredField to add additional validation, it only shown one error message.

Here is my code.
http://www.pastie.org/1130892

this CustomRequiredFields contains two major parts, one is the mandatory fields and the other is a server side validation for 'Headline', 'Summary'. CC is just some value I choose, main purpose here is to see if validation will throw a error.

The first part went all right, both Headline and Summary will display silverstripe error message, ie 'Please fill out "Headline", it is required'. However for the second part, if I fill in 'CC' for both Headline and Summary, then only one error message gets displayed, in this case is for Summary, and the error message appears at the bottom of the page. Now the strange thing is, if I take out Summary validation like this,

public function getCMSValidator() {
      $validator = new MyCustomRequiredFields(
         array(
               "Title",
               'Headline',
                'Summary',
               array(
                     "php" => 'if($data["Headline"] == "CC") {
                           $this->validationError( "Headline", "Headline is invalid", "required");
                     }',
               ),
         )
      );
      
      return $validator;   
}

then it works fine, the error message 'Headline is invalid' will shown right under the Headline field. Then it hits me, this happened before in the previous version, prior to 2.4. In the earlier version one need to patch CMSMain->getEditForm() in order to take advantage of the getCMSValidator.

see this post. http://silverstripe.org/form-questions/show/256268#post256268

this is exactly why I gave up on the CustomRequireField, since you can only validate one field. Now after all these months, I am back to square one.

Could someone from the silverstripe core develop team take a note of this, please help to get a custom validation working for the backend? For the love of SilverStripe, please help, at least set a milestone for this. For the love of open source, someone please help!

I AM IN PAIN ... WAITING FOR THE GOOD NEWS ...

Avatar
Willr

Forum Moderator, 5513 Posts

1 September 2010 at 10:12pm

I've never done anything with the backend required fields so can't vouch for whether it should work or not but having a quick play with it its either documented incorrectly or doesn't work at all.

As has been mentioned the form validation framework is going to be overhauled as part of the jQuery rewrite, this will form part of the next major release (SS 3.0).

I have dug up an enhancement ticket which covers something like this (http://open.silverstripe.org/ticket/5148). You might want to add yourself as a CC to the ticket so you're notified when the work on that takes place.

Avatar
Ben_W

Community Member, 80 Posts

2 September 2010 at 4:33pm

willr, thanks for the quick reply. you are doing great job of helping member here as well as in irc channel. However, last time I was told the validation will be looked after in 2.4 as well as the rumor that jquery will take over prototype (even though, I understand it will be a huge task to accomplish). Now it seems it has been pushed back to 3.0. Do I ... Do I dare to ask what is the ETA of 3.0? Please understand, I have been very patient, I have been waiting for this feature since 2008. So forgive me being whinging.

If any of you, fellow developers, think this is necessary feature for silverstripe, PLEASE reply to this post so that we can keep this post to the top, and gather more attention.

Avatar
Ben_W

Community Member, 80 Posts

7 October 2010 at 2:45pm

bump ...

Avatar
Zyzzza

Community Member, 25 Posts

11 March 2012 at 6:47am

Bump again :)

Need this solution working on 2.4.5 with verifying if has_one in complextable was selected.

Anyone ?

Avatar
soolan

Community Member, 11 Posts

6 April 2013 at 7:05pm

Bump again.

Can anyone give us a direction please?