"unique" validation rule not working? - laravel-4

I can't get the 'unique' validation rule working. The validation passes and returns an SQL error instead.
My rule:
$validateRules = array(
'sku' => 'required|unique:items,sku|alpha_num|min:1);'
Returns SQL error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '123' for key 'items_sku_unique'
Could it be something wrong with my DB? (Table "items", column "sku" varchar(10) UNIQUE)
Please advice

I don't think you need to add sku with table name. Try
$validateRules = array(
'sku' => 'required|unique:items|alpha_num|min:1);'

Related

Laravel 8: Rule::unique ignore doesn't work?

Duplicates:
Laravel 8 Unique form validation ignore
following this doc
This is my request validation:
public function rules()
{
return [
'LFNumber' => ['required', 'integer', Rule::unique('lost_and_found', 'id')->ignore($this->id, 'id')],
];
}
I'm trying to edit some fields of the form but I either get LFNumber already exist or SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'LFNumber'
I tried Rule::unique('lost_and_found', 'LFNumber')->ignore($this->id, 'id') to select the LFNumber column and ignore the id. But still he same errors.
If the unique column is LFNumber then you don't need to provide it:
'LFNumber' => ['required', 'integer', Rule::unique('lost_and_found')->ignore($this->id)],
This will check lost_and_found.LFNumber column while ignoring row with Primary Key matching $this->id (assuming this value is really the id on that column)

Laravel 5.5 validate unique email with model custom id

How do you validate unique email where model has a custom id.Here is my validation rule on update
'email' => 'required|unique:accounts,email,' . $user->account_id,
but this fails with sql error Unknown column 'id' in 'where clause'
id should be account_id
here is the attempted query
select count(*) as aggregate from `accounts` where `email` = test#test.com and `id` <> 2445
making some researches I found the following which would validate on custom index a unique requirement as Rule
'email' => ['required',
Rule::unique('accounts')->ignore($user->account_id, 'account_id')
]
'email' => 'required|unique:accounts,email,'.$user->account_id.',my_custom_user_id'
Specify the ID of the table you are trying to alter. It's often a custom ID.

Validate field as unique with scope in cakephp

I am creating a CakePHP application which have an edit form which contain one text box named air_id. In my table I am using project_id and air_id as composite primary key. So while updating air_id I need to validate uniqueness.
My table structure is like:
project_id air_id
1 test#test.com
1 test1#test.com
Currently I am using cakephp3.0 and I am using validateUnique rule with scope,
Following is my code:
$validator
->add('air_id', [
'unique' => [
'rule' => ['validateUnique', ['scope' => 'project_id']],
'provider' => 'table',
]
]);
And my controller is like this
$projectCustomers = $this->ProjectCustomers->newEntity($formData);
Now it is giving validation message every time. What I need is when I change the value test#test.com to test1#test.com it should raise the error and if I change it to some other value it should not raise the error. Is there something wrong in my validation?
CakePHP Unique Field Rules:
We have cakephp unique field rules that might be even better:
In your table(eg. UsersTable.php):
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(
['air_id', 'project_id'],
'Your validation error here.'
));
return $rules;
}
At the top of your table , don't forget to include this class:
use Cake\ORM\RulesChecker;
See Here (CakePHP Unique Fields Rules).

Laravel 4: avoid duplicate entry

In my app there is a simple form with one field (email) that give the possibility to register to the newsletter.
If i entry a new email, all it works fine.
If i entry an email that already exists in the database i get the error SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry...
Because i had defined that field as unique in the database.
All i want to do is to redirect::back()->with('message', 'email already registered')
But i do not know how can i do this?
I can just put an if statement in the method controller?
Or i have to define it in $rules in the model, adding another rule:
public static $rules = array(
'email' => 'required',);
Thank you!
Just define a unique rule on your users table:
public static $rules = array(
'email' => 'required|unique:users|email');

how to get rid of primary column in doctrine queries

Doctrine always includes an ID column in a query, for example:
$new_fees = Doctrine::getTable('Stats')->createQuery('s')
->select('s.sid')->where('s.uid = ?', $this->uid)
->andWhere('s.operation = ?', PPOperationType::FEE_PAID_BY_REFERRED_OWNER)
->andWhere('s.created_at > ?', $lastwd)
->groupBy('s.sid')->execute();
won't work, because s.id is included (which I didn't ask doctrine for). How do I get rid of that id column? Having to use a raw SQL here kills the usefulness of doctrine.
You have to set some column of that table to be the primary in the setTableDefinition, so that doctrine doesn't use default primary as id.
Let's say you sid is you actual primary key.. then...
public function setTableDefinition(){
....
$this->hasColumn('sid', 'decimal', 2, array(
'type' => 'decimal',
'length' => 2,
'unsigned' => 0,
'primary' => true,
'default' => '0',
'notnull' => true,
'autoincrement' => false,
));
}
Notice the 'primary' => true, this prevents doctrine to use id as the default primary key (even when it's not even defined in the table definition file.
This isn't the prettiest solution, but you can call isSubquery(true) on the Doctrine_Query to remove the primary key, in your case s.id.
http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_query.html#isSubquery()

Resources