then there just isn't a field. this is a page btw so even if I totally get rid of getCMSFields() I still get title, navigation label and content.
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.
- Previous 10 entries
- 1
- 2
- Page 33(current)
this is all kinds of ugly but it seems to work...
<?php
class MyValidator extends Validator{
function javascript(){
return false;
}
function php($data){
$result = DB::query("SELECT * FROM EventPage WHERE ID = ".$data['ID']);
foreach ($result as $row){
if($row['ImageID'] != '0'){
$has_image = true;
break;
} else {
$has_image = false;
break;
}
}
$bRet = true;
if (!$has_image){
$this->validationError(
'Image',
'We need an image',
"required"
);
$bRet = false;
}
return $bRet;
}
}
?>
there *must* be a better way...
Yeah that isn't the greatest, I'd at least use DataObject::get_by_id and not have two if's, but hey if it works, go go go :)
Glad you got something in the end!
turns out I only had to do this for images, for other relations you can check that there is anything returned in the array associated with the has_one or has_many. in this case I'm checking to make sure a page has related people:
function php($data){
$bRet = true;
if (!$data['People'][0]){
$this->validationError(
'Content',
'We Need Instructors',
"required"
);
$bRet = false;
}
return $bRet;
}
and I'm trying to make a general purpose class for images, this is what I have so far but it's horrible code. :)
<?php
class RelationshipValidator{
function has_image($table_name, $id, $field_name){
$result = DB::query('SELECT * FROM '.$table_name.' WHERE ID = '.$id);
foreach ($result as $row){
if($row[$field_name] != '0'){
return true;
} else {
return false;
}
}
}
}
?>
the above in use:
if (!RelationshipValidator::has_image('EventPage', $data['ID'], 'ImageID')){
$this->validationError(
'Image',
'We need an image',
"required"
);
$bRet = false;
}
Another thing I just realized, you need to append 'Form_EditForm_' to the field name when you are validating in the CMS to get the errors to appear next to the correct fields. again, hacky - there has to be a better way...
if (!$data['Location'] || strlen($data['Location']) > 255){
$this->validationError(
'Form_EditForm_Location',
'Location is Either Missing or Longer than 255 Characters',
"required"
);
$bRet = false;
}
Bump -have you found any Javascript popup solution for that ? As I have has_one attached to another tab, thus user won't see the validation text - I need it to popup in a JS alert. Any ideas ?
- Previous 10 entries
- 1
- 2
- Page 33(current)