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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Required Fields in CMS - has_one, has_many


Go to End
Reply


22 Posts   4915 Views

Avatar
ttyl

Community Member, 114 Posts

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.

Avatar
ttyl

Community Member, 114 Posts

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

Avatar
swaiba

Forum Moderator, 1811 Posts

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!

Avatar
ttyl

Community Member, 114 Posts

10 July 2010 at 3:18am

Edited: 10/07/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;
		}

Avatar
ttyl

Community Member, 114 Posts

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;
		}

Avatar
Zyzzza

Community Member, 25 Posts

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 ?

Go to Top