framework icon indicating copy to clipboard operation
framework copied to clipboard

Soft delete ignored with exists validation rule

Open robclancy opened this issue 13 years ago • 30 comments

I would like to make sure a resource exists using the exists validation rule like normal however I would like to not include soft deleted resources. Since the database will only check against null it doesn't seem to work since the rule will use a string. For now I have hacked in...

$extra[$segments[$i]] = $segments[$i + 1] == '{NULL}' ? null : $segments[$i + 1];

into getExtraExistConditions and have my rule have {NULL} but obviously that isn't a good solution, wondering if something can be done about this?

robclancy avatar Jul 05 '13 10:07 robclancy

Maybe a less hacky solution is to use:

if( $this->db->connection($this->connection)->getSchemaBuilder()->hasColumn($collection, 'deleted_at') ) 
{
    $query->whereNull('deleted_at');
}

In Illuminate\Validation\DatabasePresenceVerifier::getCount() and getMultiCount() methods. And maybe even wrap the call to getSchemaBuilder() method (in a method called schema or similar).

Disclaimer: Haven't really tried it. I'll try it later and maybe make a PR. Don't know how to go around when the deleted_at column is renamed tho.

ronaldcastillo avatar Jul 06 '13 15:07 ronaldcastillo

Yeah that would work but would force it so it is always checking the ones that aren't soft deleted, sometimes people might want to check against them too.

However it gives me an idea of a better solution, in each method there is...

                foreach ($extra as $key => $extraValue)
        {
            $query->where($key, $extraValue);
        }

Perhaps this could be changed to (another hacky method)...

                foreach ($extra as $key => $extraValue)
        {
            if ($key == 'deleted_at' and empty($extraValue))
            {
                $query->whereNull($key);
                continue;
            }

            $query->where($key, $extraValue);
        }

The main issue really is that there is no way to imply null from the rules as a string as they could just as easily be empty strings. Which is why the check for the 'deleted_at' key is there.

None of these methods are good enough to get in, they are all hacky and have flaws. But hopefully something can be done.

robclancy avatar Jul 06 '13 15:07 robclancy

Maybe passing an extra parameter to the rule, something like: exists:table,column,include_deleted Where the default behavior would be to ignore deleted records.

ronaldcastillo avatar Jul 06 '13 16:07 ronaldcastillo

You can pass extra conditions to exists... passing "NULL" will do a whereNull check.

Like:

'exists:table,column,deleted_at,NULL'

taylorotwell avatar Jul 09 '13 02:07 taylorotwell

Thanks.

robclancy avatar Jul 09 '13 06:07 robclancy

This doesn't work. Throws an error column NULL not found

abishekrsrikaanth avatar Jan 21 '14 07:01 abishekrsrikaanth

Because you are doing it wrong. NULL goes in the value spot, you are putting it in the column spot. So you are doing exists:table,column,NULL instead of exists:table,column,null_column_check,NULL or something similar.

robclancy avatar Jan 21 '14 07:01 robclancy

so does this need to be changed on the docs as well? http://laravel.com/docs/validation#rule-exists Can you give me an example for how the rule should actually look?

abishekrsrikaanth avatar Jan 21 '14 07:01 abishekrsrikaanth

No? You are doing it wrong. The docs are fine. You would think column NULL not found is pretty clear on what you are doing wrong. Basically, you have the NULL on the column field, so it is not found.

robclancy avatar Jan 21 '14 07:01 robclancy

so if I wanted to check WHERE email='x' AND deleted_at=NULL. how should my rule look like. I am kinda confused..

abishekrsrikaanth avatar Jan 21 '14 07:01 abishekrsrikaanth

Nope docs are right. I need more sleep. (if you read the reply I just deleted, I quit life).

You do exists:table,email,deleted_at,NULL.

robclancy avatar Jan 21 '14 07:01 robclancy

For exists:table,email,deleted_at,NULL throws column NULL does not exist. One question, what does id in this example (from the doc) do?

'email' => 'unique:users,email_address,NULL,id,account_id,1'

In the above I would like to do

unique:table,email,NULL, deleted_at,deleted_at,NULL

and it works but doesn't make sense.

felixcheruiyot avatar Jan 21 '14 08:01 felixcheruiyot

I even told you the code! What the hell. exists:table,email,deleted_at,NULL. No where does it say to do exists:table,email,NULL,deleted_at. Are you high?

robclancy avatar Jan 21 '14 08:01 robclancy

Sure someone is high! @robclancy have you tried the above code? Or just writing. Here is the link to the doc http://laravel.com/docs/validation#rule-unique. Can you explain what is going on under "Adding Additional Where Clauses" or just leave it!

felixcheruiyot avatar Jan 21 '14 08:01 felixcheruiyot

Yes I have tried the code that is in the docs. I use it in a production environment, hence requesting the feature.

'user_id' => 'exists:users,id,deleted_at,NULL' will check if there is a user exists at the id and that deleted_at is not null (so they aren't soft deleted).

'user_id' => 'exists:users,id,deleted_at,NULL,type,douche' will check that a user at id exists with deleted_at null and type == 'douche'.

So for example if @anlutro was the user clearly the type would be douche and it would come back as a pass ;)

robclancy avatar Jan 21 '14 08:01 robclancy

Do note the difference in argument order for unique and exists, I don't know for what reason - it seems rather silly and apparently @robclancy is the one who suggested it.

exists:table,column,extra_column,1,extra_column_two,2 unique:table,column,1,extra_column,extra_column_two,2

anlutro avatar Jan 21 '14 08:01 anlutro

FUUUU: I PR'd it exactly the same: https://github.com/laravel/framework/pull/1263

robclancy avatar Jan 21 '14 08:01 robclancy

In the docs example. 'email' => 'unique:users,email_address,NULL,id,account_id,1'

First is table, second is column, third is excluded value, fourth is the excluded column, then the following pairs are your where conditions. So in this case fifth is the where column and sixth the where value.

The NULL in this case is because there is on exclusion going on.

robclancy avatar Jan 21 '14 08:01 robclancy

Wish you explained as clear as this before and I wish docs was this clear.

abishekrsrikaanth avatar Jan 21 '14 08:01 abishekrsrikaanth

Wish @anlutro never pissed me off on IRC and made me rage on issues ;) Blame him.

I'll PR a change to the docs.

robclancy avatar Jan 21 '14 08:01 robclancy