You have to modify onSuccess method of RegistrationFormHandler class. Check Overriding Form Handlers section of the doc. In your overridden method you just have to add
$user->setRefId($this->request->cookies->get('ref_id'));Right before calling the parent's onSuccess method.
As for validation you have to create custom validation constraint where you have to inject @request and @doctrine.orm.entity_manager services to get the cookie and validate it across the database. You can check this cookbook entry.