CFWheels: Turn off validatesPresenceOf() temporarily

58 views Asked by At

I am have a model where I have using validatesPresenceOf() to check the presence of id column in my table.

<cfscript>
    component extends="Model"
    {   
        public function init()
        {
            validatesPresenceOf(property="id", message="error msg");
            table(mytable);
        }
    }
</cfscript>

Now the id column in my table is a auto generated field via a trigger, so I don't really need to see if the id is NOT NULL except in the below situation.

<select name=data>
<option value=""></option>
<option value=data.id></option>
</select>

Above, I am using cfwheel validation to check if the use submits a option value with an id and not value that is null. So here I am using the validatesPresenceOf() function to see if a id is submitted.

But in another location I have to save data to this table and my code is as below:

data = model(tbl.others).new();
data.name = name;
data.save();

The above code doesn't insert the record to the table because of validatesPresenceOf() on the id column, and as you can see I am not setting the id on my insert data because it is a auto generate field in my table.

My question is there a work around this, maybe I can temporary turn off that particular id validation or may I can temporary validate the id and then remove that validation condition afterwards.

I tried the following and it doesn't work:

public function custom_save(name)
{
    automaticValidations(false);
    data = model(mytable).new();
    data.name = name;
    automaticValidations(true);

    return data;
}
1

There are 1 answers

5
Daniel On

you could try not validating at all (using the model)

http://docs.cfwheels.org/v1.4/docs/save

Model Class Functions using save() such as create() and updateAll() use a validate parameter that defaults to true. You could set it to false and do other validations manually.