validate without fks

I need to run validation of a couple of models befoe saving any data into db.

Si, I need to validate model without required ids that are fk as fk model doesn’t exist yet.

For example:

model Address

model Person

Address contains person_id as required field.

I need validate Person,

I need validate Address. (Validation fails as person_id is required, but not exists yet).

There are at least 10 models.

Only then I need to save Person and save Address.

What would be the best way to do this? Currently I see only using scenarios, but this means writing my own rules and not using auotmatically generated rules in my basemodel. I do not like this way.

If this is a rule in your application… that the person and address are always added together… then just remove the validation rule for adress->person_id…

You don’t need the validation if you are assigning that value to the model…

So your execution logic would be:

  • validate (validation will pass)…
  • open a transaction…
  • save the person record…
  • get the ID of the saved record…
  • assign that ID to the address->person_id…
  • save the adress record…
  • close transaction

I have such a logic that I really need validate all models before saving.

I need do $person->validate, $adress->validate (withougt fk validation) and only then saving data.

I cannot so easily remove person_id from rules as rules are generated automatically.

R.

It all depends on your needs and how your application is designed…

Understand that Gii does not "know" how you intended this to work… it just automatically generates the rules without asking you what you intended here…

Validations are there to validate user input !

Read again the line above… rules validates user input… they serve to reject non valid input… because users can make mistakes…

But

if you have a field that gets it’s value not from the user… but automatically by some algorithm or some program rule (assign person->id to addres->person_id)… then why validate…

and why would it be hard to remove a validation rule if it’s not needed ?

On the other side… if you need this validation on other parts (like update)… then just use scenarios ;)

I have BaseModel which is generated automatically. And I have my models extending corresponding BaseModels.

So, I cannot edit basemodel code. Instead I can write my own validation rules in mymodels, however, it is not very handy, as every time db changes I need to edit my validation rules (even if automatically generated rules would be good enough).

And also I do not see a good solution of the following problem. If I decide not to include existed (automatically generated) rules from BaseModel (parent), like:

// array(’<fields>, person_id’, ‘required’)

and replace them with my scenario

array(’<fields>’, ‘required’, ‘on’=>‘myscenario’) (removign person_id)

Then existing project will work incorrectly, as it is already validating model using this existing scenario in basemodel (as there are no scenarios currently at all)… Maybe I an add some default scenario (are there such scenarios for save and update)? So my code will look like:




 array('<fields>', 'required', 'on'=>'myscenario') (removign person_id)

 array('<fields>, person_id', 'required', 'on'=>'insert')  //some defualt scenario for insert if such exists

 array('<fields>, person_id', 'required', 'on'=>'update')  //some default scenario for update if such exists