Hi today i must make a validation for files(there images) sending via ajax, i have huge problems with validation as annotation that work only when we send normaly form! not if files data coming via ajax :/ then that is code for symfony that can valid our collection of files and set error messages to their propertyPath.
$validatorImage = new Image(); // Symfony\Component\Validator\Constraints\Image
$validatorImage->mimeTypesMessage = 'image.mimeTypesMessage';
if ($form->isSubmitted()) {
$i = 0;
foreach ($form->get('images') as $image) {
$errorList = $this->get('validator')->validateValue(
$image->get('file')->getData(),
$validatorImage
);
if (count($errorList)) {
foreach ($errorList as $error) {
$image->addError(
new FormError(
$error->getMessage(),
null,
array(),
null,
array('propertyPath' => 'children[images].data['.$i.'].file')
)
);
}
}
$i++;
}
}
// is valid etc. our js action like similar to this one:
$('form[name="product"]').on('submit', function () {
var _self = $(this);
var data = _self.serialize();
data = new FormData(_self[0]);
data.append('ajax',true);
$.ajax({
method: "POST",
url: url,
data: data,
cache: false,
contentType: false,
processData: false,
enctype: 'multipart/form-data',
});
});