validator in symfony

Pubblicità

BigIssue

Utente Attivo
Messaggi
226
Reazioni
18
Punteggio
45
CIao, qualcuno si intende di symfony?
Su internet le soluzioni che ho trovato non funzionano.


Codice:
# config/validator/validation.yaml
App\Entity\Contact:
    properties:
        name:
            - NotBlank: ~
                message: 'The name must filled with valid value.'
            - Type: string
            - Length:
                min: 2
                max: 500
                minMessage: 'Your name must be at least {{ limit }} characters long'
                maxMessage: 'Your name cannot be longer than {{ limit }} characters'
              
        email:
            - NotBlank: ~
                message: 'The name must filled with valid value.'
            - Email:
                message: 'The email "{{ value }}" is not a valid email.'
        phone:
            - Blank: ~
                message: 'This value should be blank.'
            - Type:
                type: [digit, alpha] 
        message:
            - Blank: ~
                message: 'This value should be blank.'
            - Length:
                max: 1000
                    maxMessage: 'Your message cannot be longer than {{ limit }} characters'

PHP:
public function getpostmessage(Request $request): Response
    {
        $dataincontact = $request->request->all();
      
        $contact = new Contact();
        $contact = $contact->setName($dataincontact["name"]);
        $contact = $contact->setEmail($dataincontact["email"]);
        $contact = $contact->setPhone($dataincontact["phone"]);
        $contact = $contact->setMessage($dataincontact["message"]);
      
        $validator = Validation::createValidator();
        $errors = $validator->validate($contact);
        var_dump($errors);
        exit();
  
        if (count($errors) > 0) {
            //gli errori
            var_dump($errors);
          
            foreach ($errors as $key => $value) {
            
            }
        }
      
        var_dump("nessun errore");

PHP:
<?php // src/Entity/Contact.php
namespace App\Entity;

class Contact
{
    private $name;

    private $email;

    private $phone;

    private $message;
  
    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
  
    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }
  
    public function getPhone(): string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }
  
        public function getMessage(): string
    {
        return $this->message;
    }

    public function setMessage(string $message): self
    {
        $this->message = $message;

        return $this;
    }
  
  
}


Errore:
object(Symfony\Component\Validator\ConstraintViolationList)#367 (1) {
["violations":"Symfony\Component\Validator\ConstraintViolationList":private]=>
array(0) {
}
}

Non mi valida i campi settati nel file YAML ma restituisce quel errore. L'errore è giusto in se, vuol dire che non trova alcuna violazione. Ma nel file YAML ho definito i vincoli

Codice:
# config/validator/validation.yaml
App\Entity\Contact:
    properties:
        name:
            - NotBlank: ~
                message: 'The name must filled with valid value.'
            - Type: string
            - Length:
                min: 2
                max: 500
                minMessage: 'Your name must be at least {{ limit }} characters long'
                maxMessage: 'Your name cannot be longer than {{ limit }} characters'

dovrebbe dirmi che il campo name non deve essere bianco per esempio in quel errore. invece è vuoto.

Ho scordato di inserire ValidatorInterface $validator che al momento della richiesta la classe viene caricata dal container. Ora funziona.
Ma sto usando il package Mailer e ottengo questo errore:

Unable to send emails via "gmail" as the bridge is not installed; try running "composer require symfony/google-mailer".

Inutile dire che ho eseguito il comando che dice:

composer require symfony/google-mailer

Ho riavviato il server ma mi si ripresenta lo stesso errore: "Unable to send email..."
 
Ultima modifica:
Pubblicità
Pubblicità
Indietro
Top