मुझे सिम्फनी 2 में एक फॉर्म का उपयोग करके एक अजीब समस्या है।सिम्फनी 2 फॉर्म सत्यापन हमेशा त्रुटि लौटाता है: यह मान खाली नहीं होना चाहिए
सबसे पहले मैं इकाई वर्ग Job
यहाँ अंदर annotations
के रूप में मान्यता जोड़ दिया है:
class Job
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank()
* @Assert\Choice(callback="getTypeValues")
*/
protected $type;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $company;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $logo;
/**
* @Assert\Image()
*/
protected $file;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Url()
*/
protected $url;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $position;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $location;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $description;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $how_to_apply;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
*/
protected $token;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $is_public;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $is_activated;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;
/**
* @ORM\Column(type="datetime")
*/
protected $expires_at;
/**
* @ORM\Column(type="datetime")
*/
protected $created_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $updated_at;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="jobs")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $category;
}
मैं एक JobType
वर्ग बनाया है और एक फार्म के अंदर यह प्रयोग किया है। तो मैं नौकरियां जोड़ सकता हूं।
class JobType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', 'choice', array('choices' => Job::getTypes(), 'expanded' => true))
->add('category')
->add('company')
->add('file', 'file', array('label' => 'Company logo', 'required' => false))
->add('url')
->add('position')
->add('location')
->add('description')
->add('how_to_apply', null, array('label' => 'How to apply?'))
->add('is_public', null, array('label' => 'Public?'))
->add('email')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Ibw\JobeetBundle\Entity\Job',
));
}
public function getName()
{
return 'job';
}
}
यहाँ मेरी नियंत्रक है: अब
public function createAction(Request $request)
{
$entity = new Job();
$form = $this->createForm(new JobType(), $entity);
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('ibw_job_preview', array(
'company' => $entity->getCompanySlug(),
'location' => $entity->getLocationSlug(),
'position' => $entity->getPositionSlug(),
'token' => $entity->getToken(),
)));
} else {
return new Response(var_dump($form->getErrorsAsString()));
// return new Response($form->getErrorsAsString());
// return $this->render('IbwJobeetBundle:Job:new.html.twig', array(
// 'form' => $form->createView(),
// ));
}
}
जब मैं कर var_dump($form->getErrorsAsString())
मैं:
string 'ERROR: This value should not be blank.
type:
0:
No errors
1:
No errors
2:
No errors
category:
No errors
company:
No errors
file:
No errors
url:
No errors
position:
No errors
location:
No errors
description:
No errors
how_to_apply:
No errors
is_public:
No errors
email:
No errors
' (length=355)
या जब मैं करना var_dump($form->getErrors())
मैं:
array (size=1)
0 =>
object(Symfony\Component\Form\FormError)[614]
private 'message' => string 'This value should not be blank.' (length=31)
protected 'messageTemplate' => string 'This value should not be blank.' (length=31)
protected 'messageParameters' =>
array (size=0)
empty
protected 'messagePluralization' => null
मेरे पास है पता नहीं क्या जी इस ERROR: This value should not be blank.
त्रुटि को दर्शाता है। मुझे मुश्किल लग रहा है। किसी भी तरह की सहायता का स्वागत किया जाएगा।
से आप जब प्रपत्र दृश्य में प्रदर्शित किया जाना चाहिए प्राप्त अनुरोधों पर इन त्रुटियों को हो रही है या आप क्या मतलब है जब आप नियंत्रक करने के लिए फ़ॉर्म पोस्ट प्रपत्र कार्रवाई करने के लिए? –
सिर्फ एक विचार है ... मुझे लगता है कि आपको या तो @Assert \ NotBlank() या कॉलबैक फ़ंक्शन का उपयोग करना चाहिए - दोनों नहीं। – stwe
@ केनहैनेल यह तब होता है जब पोस्ट नियंत्रक पोस्ट फॉर्म को संसाधित करता है। तो POST अनुरोध में हाँ। –