Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use foreign keys in validation rules #327

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/Shell/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getTableContext($tableObject, $table, $name)
$displayField = $this->getDisplayField($tableObject);
$propertySchema = $this->getEntityPropertySchema($tableObject);
$fields = $this->getFields();
$validation = $this->getValidation($tableObject, $associations);
$validation = $this->getValidation($tableObject);
$rulesChecker = $this->getRules($tableObject, $associations);
$behaviors = $this->getBehaviors($tableObject);
$connection = $this->connection;
Expand Down Expand Up @@ -596,16 +596,8 @@ public function getValidation($model, $associations = [])

$validate = [];
$primaryKey = (array)$schema->primaryKey();
$foreignKeys = [];
if (isset($associations['belongsTo'])) {
foreach ($associations['belongsTo'] as $assoc) {
$foreignKeys[] = $assoc['foreignKey'];
}
}

foreach ($fields as $fieldName) {
if (in_array($fieldName, $foreignKeys)) {
continue;
}
$field = $schema->column($fieldName);
$validation = $this->fieldValidation($schema, $fieldName, $field, $primaryKey);
if (!empty($validation)) {
Expand Down
23 changes: 0 additions & 23 deletions tests/TestCase/Shell/Task/ModelTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,29 +830,6 @@ public function testGetValidationTree()
$this->assertEquals($expected, $result);
}

/**
* test getting validation rules and exempting foreign keys
*
* @return void
*/
public function testGetValidationExcludeForeignKeys()
{
$model = TableRegistry::get('BakeArticles');
$associations = [
'belongsTo' => [
'BakeUsers' => ['foreignKey' => 'bake_user_id'],
]
];
$result = $this->Task->getValidation($model, $associations);
$expected = [
'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
'id' => ['valid' => ['rule' => 'integer', 'allowEmpty' => 'create']]
];
$this->assertEquals($expected, $result);
}

/**
* test getting validation rules with the no-rules param.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ public function initialize(array $config)
]);
}

/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('category_id')
->allowEmpty('category_id', 'create');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the difference with this implementation vs the old one that keys can be blank on create?

Copy link
Member Author

@saeideng saeideng Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks for null able column validation is not necessary , but for not null is important


$validator
->integer('product_id')
->allowEmpty('product_id', 'create');

return $validator;
}

/**
* Returns a rules checker object that will be used for validating
* application integrity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function validationDefault(Validator $validator)
->integer('id')
->allowEmpty('id', 'create');

$validator
->integer('product_id')
->requirePresence('product_id', 'create')
->notEmpty('product_id');

$validator
->dateTime('version')
->requirePresence('version', 'create')
Expand Down