Reputation: 43
Validator ignores the values written in the fields of the form (ignores name, id and title, for example) Am I doing something wrong?
.
1. I created a basic HTML form in Symfony 4 without using the Forms component:
<form action="{{ path("register") }}" method="post">
<input type="text" name="username">
<input type="text" name="email">
<input type="password" name="password">
<input type="hidden" name="submitted">
<button type="submit">Create user</button>
</form>
2. Now, before insert the user into the DB, I'm trying to validate the form data using Symfony's Validator component, but remember: I'm not using the Forms component and this is the way I'm doing it:
use App\Entity\User
use /-- ... --/
class SecurityController extends Controller {
/**
* @Route("/register", name="register")
*/
public function showRegisterPage(Request $request) {
if ($request->request->get("submitted") !== null) {
$user = new User();
$validator = $this->get("validator");
$errors = $validator->validate($user);
if (count($errors) === 0) {
/-- Insert the user in the database. --/
}
}
return $this->render("security/register.html.twig");
}
}
3. To validate the data, Validator uses an Entity called User that contains, among other things, the validation rules - only NotBlank() for now:
namespace App\Entity
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User implements UserInterface {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank()
*/
private $email;
/-- Getters and Setter here --/
}
I need to know why Validator does not recognize the form fields; the Debug bar shows that there are indeed 3 empty fields. I thought Validator used the name of the field, I also tried the id and the title, all failed.
How can I take the data from the fields to validate them with Validator without using the Forms component?
Upvotes: 1
Views: 990
Reputation: 54649
Given your code, there is absolutely no interaction between the user entity and the form. So regardless of what is entered into the form, the user entity will stay blank.
Meaning at least, you'd need to populate the user entity by hand:
if ($request->request->get("submitted") !== null) {
$user = new User();
// populate:
$user->setUsername($request->request->get('username'));
//...
$validator = $this->get("validator");
$errors = $validator->validate($user);
if (count($errors) === 0) {
/-- Insert the user in the database as above. --/
}
}
Note that this would not help you actually show the validation errors inside the view. For this you'd need to pass the $errors
to the view and manually check each field. But all this could be done (more or less) automatically using the form component.
Upvotes: 2