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)
Related
return new hasilsurvey([
'survey' => $row[1],
'question1' => $row[2],
'question2' => $row[3],
]);
I has setting my table columns 'id' to primary key but still get same problem
Please help my problem
When creating your migration you must define id field as autoincrement:
$table->bigIncrements('id');
If you want to update id field for that table you can do it with this:
$table->bigIncrements('id')->change();
When a user first visits the page, the ListEntries table is ordered ascending by id and none of the ordering icons in the table header are active.
I would like to have the ListEntries table ordered by a column of my choosing, including having the icon next to this column active (either ascending or descending).
Is there a way to have the ListEntries table ordered by a column of my choosing when a user visits the page?
In your Controller's setup() method you can use:
$this->crud->orderBy('name', 'DESC');
Anything you pass to the orderBy() statement will be used on the Eloquent query.
By default, columns for real attributes (that have a correspondent column in the database) should be orderable. But you can also manually specify 'orderable' => true for columns, or define your own order logic. Note that relationship columns (1-n, n-n), model_function or model_function columns are NOT orderable by default, but you can make them orderable with orderLogic.
Hope it helps.
This can be done by manipulating the request object, which may be frowned upon in some circles.
This solution will also update the order icon on the appropriate column.
There are a number of ways to accomplish this, but one way would be to add the following to the setupListOperation() method of your Controller.
/** #var \Illuminate\Http\Request $request */
$request = $this->crud->getRequest();
if (!$request->has('order')) {
$request->merge(['order' => [
[
'column' => 'column-index-here',
'dir' => 'asc'
]
]]);
}
Use this key orderable it is a boolean
this->crud->addColumn([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id', // the db column for the foreign key
'entity' => 'category', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'orderable' => true,
'orderLogic' => function ($query, $column, $columnDirection) {
return $query->leftJoin('categories', 'categories.id', '=', 'articles.select')
->orderBy('categories.name', $columnDirection)->select('articles.*');
}
]);
Reference
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).
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);'
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');