Validation rule unique requires at least 1 parameters. laravel 5.4 - laravel-5.4

I've got problem with my laravel 5.4 I can't save the data
public function rules()
{
return [
'permission_id' => 'required|unique',
'name' => 'required',
'label' => 'required',
];
}
Error
Validation rule unique requires at least 1 parameters.

the unique rule need at least the name of ur database table.
ur rules function should be like this:
public function rules()
{
return [
'permission_id' => 'required|unique:db_table_name',
'name' => 'required',
'label' => 'required',
];
}
for more informations check laravel doc unique rule

Related

Laravel controller validation Request does not exist

I'm using Laravel 8 validation, and what i'm trying to do is to validate a form with some input name, unique to table Sizes column "name" where it also depends on another column speciesId that the value from $request->speciesId.
The function in Controller is looks like this
public function storeSize(Request $request)
{
$validated = $request->validate(
[
'name' => [
'required', Rule::unique('sizes')->where(function ($query) {
return $query->where('speciesId', $request->speciesId);
})
],
'speciesId' => 'required'
]
);
}
already add use Illuminate\Http\Request; and use Illuminate\Validation\Rule; but still got "ErrorException Undefined variable: request".
when i'm try to var_dump($request) or echo $request->speciesID, the variable and value is present.
my question is, why the validation function doesn't recognize the Request variable?
The error occurs because inside your function call $request is not available. You need to make it available by adding use $request to function ($query):
$validated = $request->validate([
'name' => [
'required', Rule::unique('sizes')->where(function ($query) use ($request) {
return $query->where('speciesId', $request->speciesId);
})
],
'speciesId' => 'required'
]);

yii2 migration generates createTable instead of addColumn

I'm having trouble auto-creation migrations with bizley/yii2-migration-creator extensions on table updates. Initially, it works as expected with new tables:
<?php
use yii\db\Migration;
class m200122_110631_update_table_yii_urban_tourdate extends Migration
{
public function up()
{
$this->createTable('{{%urban_tourdate}}', [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'time' => $this->dateTime(),
'duration' => $this->integer(),
'tour_id' => $this->integer(),
'tourguide_id' => $this->integer(),
'tourcourse_id' => $this->integer(),
'start_station_id' => $this->integer(),
'stop_station_id' => $this->integer(),
'status' => $this->integer(3)->notNull(),
'created_by' => $this->integer(),
'updated_by' => $this->integer(),
'created_at' => $this->integer(),
'updated_at' => $this->integer(),
]);
}
public function down()
{
$this->dropTable('{{%urban_tourdate}}');
}
}
Then after adding a column directly in database and creating another migration, I get a createTable statement as above (with the added column), which results in error (table already exists) when applying migration.
My expectation would have been to get only addColumn statement like this:
public function up()
{
$this->addColumn('urban_tourdate', 'position', $this->integer());
}
What am I doing wrong? Thanks!
You have to call
yii migration/update urban_tourdate
the second time (not create).

Grading System Validation using Yii2

I am developiging a Grading System as shown below:
Model
public static function tableName()
{
return 'grade_item';
}
public function rules()
{
return [
[['grade_max', 'grade_min'], 'required'],
[['grade_max', 'grade_min'], 'number'],
];
}
How do I validate, probably from the Model between the grade_min and grade_max. Also, grade_min should not be greater that or equal to grade_max. I want to do it as shown in the diagram below. None of the contents of each row should be the same.
Use Compare validator
This validator compares the specified input value with another one and make sure if their relationship is as specified by the operator property.
public function rules()
{
return [
[['grade_max', 'grade_min'], 'required'],
[['grade_max', 'grade_min'], 'number'],
['grade_max', 'compare', 'compareAttribute' => 'grade_min', 'operator' => '>', 'type' => 'number'],
['grade_min', 'compare', 'compareAttribute' => 'grade_max', 'operator' => '<', 'type' => 'number'],
];
}

CakePHP 3 is saying a table doesn't exist in another database when using an ORM query

I have an application in CakePHP 3.5.13 which uses four different databases.
In my config/app.php I have configured two of these as follows:
'Datasources' => [
'default' => [ 'database' => 'sdb5_hub_subdb', ... ],
'db_orgs' => [ 'database' => 'dev_hub_subdb_orgs', ... ]
];
So this means there are 2 databases with the names sdb5_hub_subdb and dev_hub_subdb_orgs.
I'm attempting to do a query which does a join on a table in each database.
I have my Table entities configured as follows:
// src/Model/Table/TblListsSubstancesCommentsTable.php
public function initialize(array $config)
{
$this->belongsTo('Substances', [
'foreignKey' => 'substance_id',
'joinType' => 'INNER'
]);
}
public static function defaultConnectionName()
{
return 'db_orgs';
}
// src/Model/Table/SubstancesTable.php
public function initialize(array $config)
{
$this->belongsTo('TblListsSubstancesComments', [
'foreignKey' => 'substance_id'
]);
}
When I attempt to do the following with the ORM:
$query = $Substances->find()->select(['id' => 'Substances.id'])->distinct();
if ($this->request->getData('org_information')) {
$org_information = $this->request->getData('org_information');
$query = $query->matching('TblListsSubstancesComments', function ($q) use ($org_information) {
return $q->where(['TblListsSubstancesComments.comment LIKE' => '%'.$org_information.'%' ]);
});
}
It's producing an SQL error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sdb5_hub_subdb.tbl_lists_substances_comments' doesn't exist
I don't understand this because the table definitely exists. Furthermore my TblListsSubstancesCommentsTable.php has defaultConnectionName() in it to specify that it should use the db_orgs connection. So I assume it must know to go to the second database to load that table? It's like it's looking in the default database and not finding it, but I don't know why, because the Table entity is telling it where it needs to look.

Laravel not responding with validator errors

I validate a model
$validator = $c->validate($collection);
This is the validate function
public function validate($data){
return Validator::make($data, $this->rules());;
}
These are the rules
public function rules() {
return array([
'name' => [
'required', 'You need to choose a name for your collection.',
'unique:collections,table_name', 'A collection or collection table with this name already exists'
],
...
]);
}
I'm trying to send back a JSON response with the validator's errors, as such:
return response()->json($validator->errors(), 200);
I'm currently testing validation for the 'name' rule, and the validator is failing, as expected.
However, I'm expecting it to return that rule's message ("A collection or collection table with this name already exists")
Instead, I'm getting this returned:
My goal is to have laravel send back the error that I need, thank you in advance for any help.
edit: updated code:
Messages:
public function messages(){
return [
'name.required' => 'A name must be specified for the collection',
'name.unique' => 'A collection or collection table with this name already exists',
'name.min' => 'The collection name is too short',
'fields.*.fieldName.unique' => 'Field names must be unique',
'fields.*.fieldName.required' => 'One or more fields must be specified for the collection',
'fields.*.fieldName.not_in' => 'Illegal field name, please try another one',
'fields.*.fieldName.min' => 'The field name is too short',
'fields.*.dataType.required' => 'A data-type must be specified for fields',
'fields.*.dataType.in' => 'Illegal data-type'
];
}
public function rules() {
return array([
'name' => [
'required', 'You need to choose a name for your collection.',
'unique:collections,table_name', 'A collection or collection table
with this name already exists',
'min:2'
],
'fields.*.fieldName' =>
[
'unique' => 'Please ensure that the fields are uniquely named.',
'required' => 'You must specify a name for your fields.',
'not_in:'.implode(',', self::$illegalFieldNames),
'min:2'
],
'fields.*.dataType' =>
[
'required', 'You must specify a data type for your fields.',
'in:'.implode(',', self::$allowedDataTypes)
]
]);
}
public function validate($data){
return Validator::make($data, $this->rules(), $this->messages());
}
The validator make method takes the third parameter as the messages array. You can't mix the rules and messages like that.
public function rules()
{
return [
'name' => 'required|unique:collections,table_name'
];
}
public function messages()
{
return [
'name.required' => 'You need to choose a name for your collection',
'name.unique' => 'A collection or collection table with this name already exists',
];
}
public function validate($data)
{
return Validator::make($data, $this->rules(), $this->messages());
}
$this->rules($request, array(
'name' =>
'required|alpha_dash|min:5|max:255|unique:posts
));
use java script for revealing error
or you can use something like this .
public function store(Request $request)
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the blog post...
}
}