Hi,
I have a controller and model to perform basic message request validation, but I can’t get the data into the model. The validation returns ‘xxx cannot be blank’. I’m testing with Postman.
I’ve tried Yii::$app->request, and post() and bodyParams and tried overriding fields() but I just don’t get any data into my model. I can’t think of anything else to try. Any thoughts or help would be much appreciated.
My controller code is:
public function actionValidate()
{
/*
** 1. Validate Request.
** 2. If valid, start Controller for requested service.
** 3. Respond.
*/
$model = new lcms();
$lcmsRequest = Yii::$app->request;
$lcmsParams = $lcmsRequest->bodyParams;
//$model->attributes = \Yii::$app->request->post();
// This will load data to the safe attributes as defined in the model rules.
//$model->load($lcmsParams, '');
$model->load(\Yii::$app->request->post());
$model->type = $lcmsRequest->post('type');
//Yii::debug(var_export($lcmsParams, true));
//var_dump($model->type);
//die;
if ($model->validate()) {
// save API call.
//$model->startService();
} else {
return $model->getErrors();
}
/*
** Build response TBD...
*/
return $model->getErrors();
}
A snippet from my model is:
class Lcms extends Model
{
//API message structure - Header and Data...
public $serviceRequestId;
public $requestorRef;
public $credential;
public $type;
public $service;
public $version;
public $timestamp;
public $data;
private $requestorCredential;
private $requestorId;
private $apiVersion;
// explicitly list every field, best used when you want to make sure the changes
// in your model attributes do not cause your field changes (to keep API backward compatibility).
public function fields()
{
return [
// field names are the same as the attribute names
'serviceRequestId',
'requestorRef',
'credential',
'type',
'service',
'version',
'timestamp',
'data'
];
}
public function rules()
{
return [
[['serviceRequestId'], 'integer'],
[['serviceRequestId', 'requestorRef', 'credential', 'type', 'service', 'version', 'timestamp','data'], 'required'],
[['requestorRef'], 'string', 'max' => 50],
[['credential'], 'string', 'max' => 255],
[['type'], 'string', 'max' => 10],
[['service'], 'string', 'max' => 255],
[['version'], 'string', 'max' => 5],
[['timestamp'], 'integer'],
[['data'], 'string'], // validated by the requested service.
// Additional specific vaidation...
['type','validateType', 'skipOnEmpty' => false, 'skipOnError' => false],
['version','validateVersion', 'skipOnEmpty' => false, 'skipOnError' => false],
['requestorRef','validateRequestorRef', 'skipOnEmpty' => false],
['credential','validateCredential','skipOnEmpty' => false],
['service','validateService','skipOnEmpty' => false, 'skipOnError' => false],
['service','validateRequestorService', 'skipOnEmpty' => false, 'skipOnError' => false],
];
}
Any help would be much appreciated.