Is there a way to have a has_one or has_many be required in the CMS? For example, I want to force a user to upload an image for a page. Additionally, is there any documentation on validating data in the CMS?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
- Page 11(current)
- 2
- 3
- Next 10 entries
Have you tried making it a required field when you create the CMS form field for the image in getCMSFields()?
I've never tried that with any CMS fields before so not sure how it'd work!
Rich
when I do this
public function getCMSValidator() {
return new RequiredFields('Date', 'Time', 'Location', 'Image');
}
I always get a validation error on image - even if there is one. if I remove image from the required fields everything is groovy. I also have some related dataobjects I need to check for.
also, there is documentation of public facing form validation - but what about CMS validation? that's what I'm most concerned about - even the best trained people make mistakes - I'd like to catch them.
nothing? this is a pretty important thing for insuring content editors don't make mistakes...
bumping this up - nobody has any ideas? is there any documentation at all on setting up data validation in the CMS?
How about making a validator....
inside MyDataObject.php
function getCMSValidator()
{
return new MyValidator();
}
calls MyValidator.php
class MyValidator extends Validator
{
function javascript()
{
return false;
}
function php($data)
{
$bRet = true;
//do some validation on the data
if ($data['HasOneID'] SOME VALIDATION HERE)
{
$this->validationError(
'HasOneID',
'Some message here',
"required"
);
$bRet = false;
}
return $bRet;
}
}
Hope this helps...
Barry
Thanks for the help, but I have a few more questions ;-)
First, when I put the class in a new file it wasn't finding it - is there an include statement I need to put in someplace? Second, when I tried it with a normal text value it worked, but when I tried with my Image it doesn't work. I tried both 'Image' and 'ImageID' - any suggestions?
Also, is there anyway to do a javascript alert?
class MyValidator extends Validator{
function javascript(){
return false;
}
function php($data){
$bRet = true;
//do some validation on the data
if (!$data['Image']){
$this->validationError(
'Image',
'We need an image',
"required"
);
$bRet = false;
}
return $bRet;
}
}
1) if your class name is the same as your file name it will auto load it. Never had problems with that.
2) if this is your dataobject...
static $has_one = array('MyImage' => 'Image');
I'd expect it to be...
$data['MyImageID']
but to be sure, turn the site to dev mode, enable logging and send $data to the logger and have a look at it :)
- Page 11(current)
- 2
- 3
- Next 10 entries