Check Foreign Key before save

Hey, I want to check ForeignKey before save, if data is not in the foreign table then get an error message. Can anyone help me how to do that?

here is the relation

public function relations()

{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


		'role' => array(self::BELONGS_TO, 'Role', 'Role_id'),


		


	);


}

public function create()

{


	if(!isset($this->Role_id)) {


		$this->addError('', 'Role is not set');


	} else {


               $this->Role_id = $this->role->id;


	}


	


	$this->save();


}

$this->Role_id = $this->role->id;

this is not working. Am I wrong?

You can use a CRequiredValidator instead of checking it, and for the value in the foreign table you can use a CExistValidator