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 ...