Domanda

Here's how my model works. I have a Post model that has a expires_at column. Users provide an integer for the number of days they would like the Post to last.

This was pretty straightforward when I was doing validation in the controller. Just validate the "days" field and then transform that into a future expiration date. But now I'm doing the validation in the model with Ardent and I'm not sure how to accomplish this. I think I need to use a mutator but setting expires_at as an integer seems counterintuitive. I'm also not sure if Ardent validates before or after mutation.

È stato utile?

Soluzione

I figured out how to do it using the code that removes redundant from data like confirmation fields.

public $autoPurgeRedundantAttributes = true;

function __construct() {
    parent::__construct();

    $this->purgeFilters[] = function($key) {
        $purge = array('tempData', 'myAttribute');
        return ! in_array($key, $purge);
    };
}

You need to enable $autoPurgeRedundantAttributes or the filters won't be called. Then you add a closure to the $purgeFilters array which returns false if the provided attribute should be removed. ("days" in this case)

Then you just need to make sure to manually set the field you want validated

$this->attributes['days'] = $value;

Edit: This is in the official docs now. I replaced my code above with theirs.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top