How to Make a Before_Save Conditional

Combining criteria for BeforeSave event

You were close! A couple changes:

  1. You need to check against the cell's .DisplayFormat since that is conditional formatting.

  2. You were exiting your subroutine before getting to your If condition. Use Exit For instead.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Application.ScreenUpdating = False

    Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1

    Dim rng As Range

    For Each rng In ActiveSheet.UsedRange
    If rng.DisplayFormat.Interior.Color = vbRed Then
    Cancel = True
    Exit For
    End If
    Next rng

    Application.ScreenUpdating = True

    If Cancel Then MsgBox "Please accept the terms and conditions"

    End Sub

Also Application.ScreenUpdating = True needs to be outside your loop, otherwise it may never be turned back on!

UPDATE:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Application.ScreenUpdating = False

Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1

Dim rng As Range

For Each rng In Worksheets(1).UsedRange
If rng.DisplayFormat.Interior.Color = vbRed Then
MsgBox ("Please correct any fields highlighted in red")
Cancel = True
Application.ScreenUpdating = True
Exit Sub
End If
Next rng

Application.ScreenUpdating = True

If Cancel Then MsgBox "Please accept the terms and conditions before saving the Invoice"

End Sub

Is there a way to make a before_save conditional?

you can use :if

before_save do_something, :if => Proc.new {|model| model.some_boolean_attr_or_method }

or simply

before_save do_something, :if => some_condition

EDIT:

for a quick reference, there's an excellent guide about this:

http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks

Register multiple before_save conditional callbacks on Rails 4

Does the second before_save overrides the first?

No

Does the callbacks are executed in the same order in which they are declared?

Yes

Since your attribute is not a boolean, you probably shouldn't use the question mark. Try:

before_save :method1, unless: :some_field_1
before_save :method2, unless: :some_field_2

Rails 5 - conditional before_save callback on belongs_to existence when relation is optional

before_save :set_status, if: :template_exists?

def template_exists?
return !self.template.nil?
end

^ This method works because the callback needs code that it can execute at runtime. You can pass it a symbol or string that maps to the name of a method, or you can pass it a proc or lambda that it can execute at runtime. But if you pass it code like

before_save :set_status, if: self.template.nil?

then it will actually try to execute that code at the time when you are setting up the callback, not at the time when the callback is called. When you are setting up the callback, self refers to the class Card not the instance of the class.

Here's some documentation on ActiveRecord callbacks if you want more detail.

How to make Yii beforeSave breaks the save

As documented here: If you override the beforeSave($event) method in a CActiveRecordBehavior, you have to set the isValid property of the $event to false, if you want to prevent saving of the owner model.

if($preventSave) {
$event->isValid = false;
}

Also note, that it's $model->errors, not $model->validationErrors.

Rails 5 - how to negate a method call on a before_save callback conditional

Try before_save :set_status, unless: :is_template? ;)

before_save if attribute.present?

Change the before_save statement to the following:

before_save :date_started_sets_deadline, if: :date_started?

If you provide a symbol to the if, then rails evaluates it
in the context of the instance. By adding ?, it is a auto generated method which is essentially same as date_started.present?.

Also, if the date_started is required within the date_started_sets_deadline implementation, I would also add the check explicitly instead of solely depending on adding the if condition on the callback logic.

def date_started_sets_deadline
if self.date_started.present? && (self.date_started > Date.tomorrow)
self.deadline = self.date_started
end
end

Refer to Using :if and :unless with a Symbol for more info.



Related Topics



Leave a reply



Submit