I need your help because I’m not sure of the best way to develop part of my site.
I need to be able to add Items which can have different types but which have attributes in common (name, date, type, id etc.).
When creating I have to choose between 3 different types and depending on this type they have specific attributes (an image field, a link field, nothing).
During the modification I must be able to change the type of the item they have and therefore the fields that go with this type.
Example :
Each item has a title, a date and therefore a type.
A post type item has only text,
A video type item with only a video field and the text field in addition.
I can first create a post type item and then change it to a video.
Any ideas on the best way to do this?
Obviously, it’s the same controller for everyone, but in terms of form validation, it might be complicated?
I just looked quickly but I understand that you have to choose the validator before displaying the form except I would like to use the same form for all my type of items and specify the item in a select of the form.
I don’t know if it’s well explained? haha
I may have found a solution with the on arrays of the validator in my model:
$validator->add('script',
'valid-script', [
'rule' => function ($value, $context){
return !empty(trim($value));
},
'message' => "Il faut remplir la description.",
'on' => function ($context) {
return in_array($context['data']['type'],['reportage','extrait']);
},
]
);
$validator->add('UrlYoutube',
'valid-yt-url', [
'rule' => function ($value, $context){
if (preg_match('/(.+)youtube\.com\/watch\?v=([\w-]+)/',$value)) {
return true;
}else{return false;}
},
'message' => "Il faut une URL Youtube valide pour un type reportage.",
'on' => function ($context) {
return in_array($context['data']['type'],['reportage']);
},
]
);
you can have different custom-validation in the Model.
// in Model/Table/ArticlesTable.php
class ArticlesTable extends Table
{
public function validationCustomType1Update($validator)
{
// todo, own validation here
return $validator;
}
public function validationCustomType2Update($validator)
{
// todo, own validation here
return $validator;
}
public function validationCustomType3Update($validator)
{
// todo, own validation here
return $validator;
}
}
Then you can access them easily in the Controller
// in /Controller/ArticlesController.php
$article = $articlesTable->newEntity(
$this->request->getData(),
['validate' => 'customType1Update']
);
// or you can use another way
$type2Validator = $articlesTable->getValidator('customType2Update');
$errors = $type2Validator->validate( $this->request->getData() );
$article->setErrors($errors);
Is there something I don’t understand but I think it’s a mistake in my code?
I create an empty entity as soon as the user arrives on the form page (first line of the function) so I don’t know what he will choose as a post because he decides via a select in the form.
I think that’s meant for situations where you need the same validation rule across multiple models, e.g. validating phone numbers based on international standards.