Đây là loại hình thức của tôi:Symfony2 trường mẫu không cập nhật khi xác nhận lỗi xảy ra
class TestFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('thumbnail', 'hidden', array(
'label' => 'Thumbnail',
'label_attr' => array(
'class' => 'col-xs-2 control-label'
),
'required' => false,
'error_bubbling' => true,
'required' => false
));
$builder->add('thumbnail_data', 'file', array(
'error_bubbling' => true,
'required' => false
));
}
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'X\LibraryBundle\Entity\Test',
'cascade_validation' => true,
'error_bubbling' => true,
));
}
public function getName()
{
return 'test';
}
}
Đây là thực thể, một phần quan trọng là setThumbnailData ($ file) phương pháp, mà các cửa hàng các tập tin hình ảnh thu nhỏ và đặt thumbnail đường dẫn thông qua phương thức setThumbnail (string).
<?php
namespace X\LibraryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Test
*
* @ORM\Table(name="test")
* @ORM\Entity(repositoryClass="X\LibraryBundle\Repository\TestRepository")
*/
class Test
{
/**
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $thumbnail;
/**
* Set thumbnail
*
* @param string $thumbnail
* @return Test
*/
public function setThumbnail($thumbnail)
{
$this->thumbnail = $thumbnail;
return $this;
}
/**
* Get thumbnail
*
* @return string
*/
public function getThumbnail()
{
return $this->thumbnail;
}
/**
* This will save file to disk
* @param $file
*/
public function setThumbnailData($file) {
if($file !== null && $file !== false)
{
$fileName = $file->getClientOriginalName();
$baseDir = __DIR__ . '/../../../../../../../web/uploads/promotional_material/';
$dir = sha1(microtime());
while (is_dir($baseDir . $dir)) {
$dir = sha1(microtime());
}
mkdir($baseDir . $dir);
$this->setThumbnail('/uploads/promotional_material/' . $dir . '/' . $fileName);
$file->move($baseDir . $dir, $fileName);
}
}
public function getThumbnailData() {
return '';
}
}
Bây giờ vấn đề là, nếu các hình thức chạy vào một lỗi xác nhận, các đường cành lá sau sản xuất sản lượng khác nhau, giá trị đúng outputted từ dòng thứ hai, người kia tạo ra con đường thumbnail gốc. Vì vậy, nếu tôi xuất đầu vào hình thu nhỏ bằng cách sử dụng {{form_widget (form.thumbnail)}}, tôi nhận được đường dẫn hình thu nhỏ ban đầu, không phải là đường dẫn đã thay đổi thông qua phương thức setThumbnailData().
{{ dump(form.thumbnail.vars.data) }}
{{ dump(form.vars.data.thumbnail) }}
Sự cố có gây ra bằng cách đặt hình thu nhỏ bằng phương thức setThumbnailData() không? Bạn không chắc chắn làm thế nào để sửa lỗi này không phải là cứng mã hóa các đầu vào trong cành lá như vậy:
<input type="hidden" name="test[thumbnail]" value="{{ form.vars.value.thumbnail }}"/>
Tôi cũng muốn lưu tệp vào đĩa ngay cả khi nó không xác thực, do đó người dùng không phải tải lên lại hình ảnh nếu xảy ra lỗi xác thực. – Trololololol
Sau đó, tạo một 'Type' và' Validator' mới làm cho nó liền mạch với khuôn khổ của symfony, có thể với trường 'file' và' hidden' là kiểu con. Nếu bạn chỉ lưu tệp của mình vào đĩa, điều gì sẽ xảy ra nếu người dùng không bao giờ nhập dữ liệu hợp lệ? – Aitch