Then give them a nice message telling them nah you can't do that by overriding the update_onSave method.
public function update_onSave($recordId, $context = null)
{
$thisForm = post('Grade');
$thisRecord = Grade::find($recordId);
// If trying to change the grade, and the new grade is not equal to the old grade
if( isset($thisForm['student_grade']) && $thisForm['student_grade'] !== $thisRecord->student_grade ) {
$isGradingOpen = $thisRecord->semester->is_grading_open;
// If this user does not have the permission to make this change currently
if( !$this->user->hasAccess('example.plugin.edit_all_fields_grades') && !$isGradingOpen ) {
// Then deny
Flash::error('You do not have permission to change the grade at this time.');
}else {
// Otherwise proceed, call the original update_onSave method
return $this->asExtension('FormController')->update_onSave($recordId, $context);
}
}else {
// Otherwise proceed, call the original update_onSave method
return $this->asExtension('FormController')->update_onSave($recordId, $context);
}
}
And then finally, now that all the visual stuff is set up, guarantee that the model won't update when it shouldn't. This is in the model's beforeSave method.
public function beforeSave()
{
/**
* Cancel if attempting to input grade when grading isn't open
*/
if( isset( $this->semester_id ) && isset( $this->student_grade ) ) {
$isGradingOpen = Semester::find($this->semester_id)->is_grading_open;
// Disable grade based on Semester
if( !$user->hasAccess('example.plugin.edit_all_fields_grades') && !$isGradingOpen ) {
return false;
}
}
I don't believe there's any way to get around this, but I do not know if there exists a simpler way to accomplish this.
Switched almost everything from extendFields into filterFields. Works great for changing a field to type=text, change value, and disabling it so to show a locked out RecordFinder.
One thing I ran into is that while I could change the scope parameter from within the filterFields, it would not actually apply the scope.
Last updated
1-4 of 4
You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.