Symfony 2詳細小計4

7348 ワード

検証制約
         :
# app/config/config.yml
framework:
    validation: { enable_annotations: true }
        :
$validator = $this->get('validator');
$errors = $validator->validate(    ); //             
---count($errors) AND ($errorsString = (string) $errors); 
|
---{% for error in errors %} {{error.message}} {% endfor %}  #          ConstraintViolation

    :
$form = $this->createForm(new    (),      );
$form->handleRequest($request); 
if ($form->isValid()) {
  return $this->redirectToRoute(...);
} 
return $this->render('author/form.html.twig', array(
   'form' => $form->createView(),
));
 
         :
 $emailConstraint = new Assert\Email();
 $emailConstraint->message = 'Invalid email address';
 $errorList = $this->get('validator')->validate(
        $email,
        $emailConstraint
    );
use Symfony\Component\Validator\Constraints as Assert;
       :     、 Getters    (get,is,has     )、    (  callback       )


   ,            ,             :default 、    。
  ,registration :
$errors = $validator->validate($author, null, array('registration')); //        ,       ,   default    

         :
 * @Assert\GroupSequence({"User", "Strict"}) //            
 
      provider(         )
 * @Assert\GroupSequenceProvider  //           provider
     : implements GroupSequenceProviderInterface
     : getGroupSequence
               

フォーム#フォーム#
         :
                           
                   
      ,                

##         ##
#             
#                
#             
      ->         ->           ->          ->       

##      ##
//////        /////////
namespace xxBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('summary', 'textarea')
            ->add('authorEmail', 'email')
            ->add('publishedAt', 'datetime')
            ->add('save','submit',array('label'=>'yes')); #           ,     
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'xxBundle\Entity\My',
            'validation_groups' => array('registration'), #        ,           
                                                          # 'validation_groups'=>false     ,          
                                   # array('AppBundle\Entity\Client','determineValidGp')  client              
                                   #          FormInterface $form
        ));
    }

    public function getName()
    {
        return 'My';
    }
}
//////           //////////
use AppBundle\Entity\Post; #   
public function xxAction(Request $request)
{
   $form = $this->createFormBuilder(new Post()【, $options】)
		        【->setAction($this->generateUrl('target_route'))】
		        【->setMethod('GET')】
		        ->add('name')  #  add      null,        
		        ->add('file')
		        ->getForm();
}
// $options = array(
    'validation_groups' => array('registration'), //       ,           
)
##################################################################


##       ##
use AppBundle\Entity\Post; #   
use xxBundle\Form\MyType; #   
public function xxAction(Request $request)
{
    $post = new Post();
    $post->setTitle('this is title');
    $form = $this->createForm(new MyType(), $post【, array(
        'action' => $this->generateUrl('target_route'),
        'method' => 'GET',
    )】);
    ...
}
##################################################################


##           ##
# isValid        isSubmitted,       ,         isSubmitted
use AppBundle\Entity\Post; #   
use xxBundle\Form\MyType; #   
public function xxAction(Request $request) {
	$post = new Post();
	$form = $this->createForm(new MyType(), $post);
	$form->handleRequest($request); //  post    ,           ,            。
                                        //get      。
	if ($form->isSubmitted() && $form->isValid()) { //    
		$em = $this->getDoctrine()->getManager();
		$em->persist($post); //   $em->persist($form->getData());
		$em->flush();
        	
		return $this->redirect($this->generateUrl(
		    'post_show',
		    array('id' => $post->getId())
		));
	} else { //     
		return $this->render('form.html.twig', array(
		   'form' => $form->createView(),
		));
	}
}
##################################################################


##        ##
/////////        ///////////
{{ form_start(form【, {'attr': {'class': 'blog-form'}}】) }}
    {{ form_widget(form) }} <!--           -->
    <input type="submit" value="yes" class="btn" />
{{ form_end(form) }} <!-- CSRF Token          -->
///////          ////////
{{ form_start(form【, {'action': path('target_route'), 'method': 'GET'}】) }}
    {{ form_errors(form) }} <!--       -->
    {{ form_row(form.title) }} <!-- title       -->
    {{ form.vars.value.title }} <!--      title     -->
{{ form_end(form) }}
///////             ////////
{{ form_start(form) }}
    {{ form_errors(form) }} <!--       -->

    <div> <!--      div   form_row -->
        {{ form_label(form.dueDate【, '2015-10'】) }}  <!--           -->
        {{ form_errors(form.dueDate) }}
        {{ form_widget(form.dueDate【,{'attr': {'class': 'dateclass'}}】) }}
        {{ form.dueDate.vars.id }} <!--          -->
    </div>
    {{ form_widget(form.save) }}
{{ form_end(form) }}
##################################################################
##            ##
$form->get('dueDate')->getData();
$form->get('dueDate')->setData(new \DateTime());

##          ##
$form->get('save')->isClicked(); //  save       

##         required      ##
{{ form(form, {'attr': {'novalidate': 'novalidate'}}) }}
 
submit       'attr' => array('novalidate' => 'novalidate'),

##        ##
$builder->add('  ', '  ', array(
'label'    =>    'yes',  #    ,false    
'attr'     =>    array('class'=>'btn'),
'validation_groups' => false, #    submit  
'required  =>    true, #  html5      
'mapped'   =>    false, #         ,          ,     ,    :         
))
      : 
   :text、textarea、email、integer、number、password、url
   :choice(  )
   :date、datetime、time
   :checkbox、radio、file
  :collection
   :hidden
   :button、reset、submit