Answer the question
In order to leave comments, you need to log in
What is the best way to implement re-validation of a form whose fields are converted from string to \DateTime?
Let's say we have a model:
class ProductDatesCheckerForm extends Model {
const INPUT_DATE_TIME_FORMAT = "d.m.Y H:i";
/**
* @var string
*/
public $startDateTime;
/**
* @var string
*/
public $endDateTime;
/**
* @var array
*/
public $productReferences = [];
/**
* @inheritDoc
*/
public function rules() {
return [
[['startDateTime', 'endDateTime', 'productReferences'], 'required'],
[['startDateTime', 'endDateTime'], 'datetime', 'format' => self::INPUT_DATE_TIME_FORMAT],
[['productReferences'], 'safe']
];
}
}
Answer the question
In order to leave comments, you need to log in
You can write a custom validator in which you will check the type and validate depending on it.
But storing data in different types in the same field is a very, very bad idea. Go around huge problems in the future. Better make a couple of methods that will convert to DateTime. Or keep only DateTime in the fields, and convert the strings as you fill in the data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question