Last active
June 18, 2021 02:45
-
-
Save angelxmoreno/0581c7ddc95325c40fd42a9166450670 to your computer and use it in GitHub Desktop.
CRUD Not Validating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller\Api; | |
use App\Controller\AppController; | |
use Crud\Controller\Component\CrudComponent; | |
use Crud\Controller\ControllerTrait; | |
use Exception; | |
/** | |
* ApiApp Controller | |
* @property-read CrudComponent $Crud | |
*/ | |
class ApiAppController extends AppController | |
{ | |
use ControllerTrait; | |
public $paginate = [ | |
'maxLimit' => 100, | |
'limit' => 20, | |
]; | |
/** | |
* @throws Exception | |
*/ | |
public function initialize() | |
{ | |
$this->loadComponent('RequestHandler'); | |
$this->loadAuth(); | |
$this->loadComponent('Crud.Crud', [ | |
'actions' => [ | |
'index' => [ | |
'className' => 'Crud.Index', | |
], | |
'Crud.View', | |
'add' => [ | |
'className' => 'Crud.Add', | |
'saveOptions' => ['validate' => true] | |
], | |
'Crud.Edit', | |
'Crud.Delete' | |
], | |
'listeners' => [ | |
'Crud.Api', | |
'Crud.ApiPagination', | |
] | |
]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Controller\Api; | |
use App\Model\Table\RssDomainsTable; | |
use Cake\Event\Event; | |
use Cake\Http\Response; | |
use Cake\ORM\Query; | |
use Exception; | |
/** | |
* Class DomainsController | |
* @package App\Controller\Api | |
* | |
* @property RssDomainsTable $RssDomains | |
*/ | |
class DomainsController extends ApiAppController | |
{ | |
public $modelClass = 'RssDomains'; | |
public $paginate = [ | |
'limit' => 10, | |
'contain' => [], | |
'order' => [ | |
'created' => 'asc' | |
], | |
]; | |
public function initialize() | |
{ | |
parent::initialize(); | |
$this->loadModel('RssDomains'); | |
} | |
/** | |
* @param $id | |
* @return Response | |
* @throws Exception | |
*/ | |
public function view($id): Response | |
{ | |
$this->Crud->on('beforeFind', function (Event $event) { | |
/** @var Query $query */ | |
$query = $event->getSubject()->query; | |
$query->contain(['Users']); | |
}); | |
return $this->Crud->execute(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Table; | |
use App\Model\Entity\RssDomain; | |
use AuthUserStore\Event\AuthUserStore; | |
use Cake\Datasource\EntityInterface; | |
use Cake\Event\Event; | |
use Cake\ORM\Association\BelongsTo; | |
use Cake\ORM\Association\HasMany; | |
use Cake\ORM\Behavior\TimestampBehavior; | |
use Cake\ORM\RulesChecker; | |
use Cake\ORM\Table; | |
use Cake\Validation\Validator; | |
/** | |
* RssDomains Model | |
* | |
* @property UsersTable&BelongsTo $Users | |
* @property DomainFeedsTable&HasMany $DomainFeeds | |
* | |
* @method RssDomain get($primaryKey, $options = []) | |
* @method RssDomain newEntity($data = null, array $options = []) | |
* @method RssDomain[] newEntities(array $data, array $options = []) | |
* @method RssDomain|false save(EntityInterface $entity, $options = []) | |
* @method RssDomain saveOrFail(EntityInterface $entity, $options = []) | |
* @method RssDomain patchEntity(EntityInterface $entity, array $data, array $options = []) | |
* @method RssDomain[] patchEntities($entities, array $data, array $options = []) | |
* @method RssDomain findOrCreate($search, callable $callback = null, $options = []) | |
* | |
* @mixin TimestampBehavior | |
* @mixin UserCommentsTable | |
*/ | |
class RssDomainsTable extends Table | |
{ | |
/** | |
* Initialize method | |
* | |
* @param array $config The configuration for the Table. | |
* @return void | |
*/ | |
public function initialize(array $config) | |
{ | |
parent::initialize($config); | |
$this->setTable('rss_domains'); | |
$this->setDisplayField('name'); | |
$this->setPrimaryKey('id'); | |
$this->addBehavior('Timestamp'); | |
$this->addBehavior('UserCommentable'); | |
$this->belongsTo('Users', [ | |
'foreignKey' => 'user_id', | |
'joinType' => 'INNER', | |
]); | |
$this->hasMany('DomainFeeds', [ | |
'foreignKey' => 'rss_domain_id', | |
]); | |
} | |
/** | |
* Default validation rules. | |
* | |
* @param Validator $validator Validator instance. | |
* @return Validator | |
*/ | |
public function validationDefault(Validator $validator): Validator | |
{ | |
$validator | |
->uuid('id') | |
->allowEmptyString('id', null, 'create'); | |
$validator | |
->boolean('is_active') | |
->allowEmptyString('is_active'); | |
$validator | |
->scalar('name') | |
->maxLength('name', 100) | |
->minLength('name', 2) | |
->notEmptyString('name'); | |
$validator | |
->urlWithProtocol('url') | |
->maxLength('url', 200) | |
->notEmptyString('url') | |
->add('url', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); | |
$validator | |
->scalar('description') | |
->allowEmptyString('description'); | |
$validator | |
->integer('feed_count') | |
->notEmptyString('feed_count'); | |
return $validator; | |
} | |
/** | |
* Returns a rules checker object that will be used for validating | |
* application integrity. | |
* | |
* @param RulesChecker $rules The rules object to be modified. | |
* @return RulesChecker | |
*/ | |
public function buildRules(RulesChecker $rules): RulesChecker | |
{ | |
$rules->add($rules->isUnique(['url'])); | |
$rules->add($rules->existsIn(['user_id'], 'Users')); | |
return $rules; | |
} | |
public function beforeSave(Event $event, RssDomain $entity, \ArrayObject $options) | |
{ | |
if ($entity->isNew()) { | |
$entity->is_active = false; | |
$entity->user_id = AuthUserStore::getUser()->id; | |
$entity->feed_count = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
request from Postman as curl: