21301 Posts in 5736 Topics by 2603 members
| Go to End | ||
| Author | Topic: | 3308 Views |
-
Re: Required Fields in CMS - has_one, has_many

9 July 2010 at 5:50am
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.
-
Re: Required Fields in CMS - has_one, has_many

9 July 2010 at 6:09am
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...
-
Re: Required Fields in CMS - has_one, has_many

9 July 2010 at 7:33pm
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!
-
Re: Required Fields in CMS - has_one, has_many

10 July 2010 at 3:18am Last edited: 10 July 2010 3:19am
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;
} -
Re: Required Fields in CMS - has_one, has_many

10 July 2010 at 3:23am
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;
} -
Re: Required Fields in CMS - has_one, has_many

11 March 2012 at 7:05am
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 ?
| 3308 Views | ||
| Go to Top |



