I want to change the password pattern of FosUser Bundle.. (lets say, i will require the password to be minumum of 6 characters which also require 1 letter and 1 number. Where do i set this?
Yuo can do this in FosUserBundle validator.
Take a look at this file:
/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/validation.xml
Particularly:
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
<option name="groups">Registration</option>
</constraint>
<constraint name="MinLength">
<option name="limit">2</option>
<option name="message">fos_user.password.short</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</property>
I don't know (but I suppose that I'm right) if exists a constraint of type "1 letter and 1 number".
In that case, you have to "build" it yourself and use in the same way you use the NotBlank and MinLenght constraint of this example
I've add into User entity
// src/Acme/AcmeBundle/Entity/User.php
....
class User extends BaseUser{
....
/**
* #Assert\NotBlank(message="fos_user.username.blank", groups={"Registration", "Profile"})
* #Assert\Length(min=5, max="255",
* minMessage="fos_user.username.short", maxMessage="fos_user.username.long",
* groups={"Registration", "Profile"})
*/
protected $username;
/**
* #Assert\NotBlank(message="fos_user.password.blank", groups={"Registration", "ResetPassword", "ChangePassword"})
* #Assert\Length(min=6,
* minMessage="fos_user.password.short",
* groups={"Registration", "Profile", "ResetPassword", "ChangePassword"})
*/
protected $plainPassword;
Related
i'm using sonata admin, i tried to override max length allowed for name of categorie
I have an entity MyEntity who extend Application\Sonata\ClassificationBundle\Entity\Category
// MyEntity admin class
I put this following function, regarding https://sonata-project.org/bundles/core/master/doc/reference/conditional_validation.html#inline-validation
public function validate(\Sonata\Form\Validator\ErrorElement $errorElement, $object)
{
parent::validate($errorElement, $object);
$errorElement->with('name')
->assertLength(['max' => 100])
;
}
Current display
Expected to get ride of this 32 max length on name's field
Thx for helping
It looks like what you need to do instead, is override this validation config: https://github.com/sonata-project/SonataClassificationBundle/blob/3.x/src/Resources/config/validation.xml
<class name="Sonata\ClassificationBundle\Model\Category">
<property name="name">
<constraint name="NotBlank"/>
<constraint name="Length">
<option name="min">2</option>
<option name="max">32</option>
</constraint>
</property>
</class>
i have a entity with path field
/**
* #var string
* #Assert\NotBlank(message="Please, upload scontrino/fattura della stufa acquistata.")
* #Assert\File(mimeTypes={ "application/pdf","image/png" ,"image/jpg","image/jpeg" })
* #ORM\Column(name="path_file", type="string", length=255)
*/
private $path;
i have this field in a form
->add('path', FileType::class, array('data_class' => null))
;
and in the twig file
<div class="row">
<div class="input-field col s12 ">
{{ form_errors(form.path) }}
{{ form_widget(form.path) }}
</div>
</div>
when i press submit button and i put i not allowed file now my message is
Il mime type del file non รจ valido ("application/zip"). I tipi permessi sono "application/pdf", "image/png", "image/jpg", "image/jpeg".
I want to change that message but i don't understand the best way to do that.
If i must do it in entity file or in form file or in twig file
You have to add:
mimeTypesMessage = "with the text you want", in the #Assert\File
in your #Assert\File of your entity
/**
* #var string
* #Assert\NotBlank(message="Please, upload scontrino/fattura della stufa acquistata.")
* #Assert\File(mimeTypes={ "application/pdf","image/png" ,"image/jpg","image/jpeg" }, mimeTypesMessage = "with the text you want")
* #ORM\Column(name="path_file", type="string", length=255)
*/
The symfony Reference
I'm currently working on a Symfony2 project which uses Sonata.
Previous developers have made modifications inside the vendors directly which is obviously a huge mistake so I'm in the process of refactoring this. I'm currently stuck with some modifications that have been made to constraint mapping from the FOSUserBundle.
The file is: /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/validation/orm.xml
They actually disabled the UniqueEntity constraint on the class FOS\UserBundle\Model\User based on the field usernameCanonical, like this:
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="FOS\UserBundle\Model\User">
<!-- <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">usernameCanonical</option>
<option name="errorPath">username</option>
<option name="message">fos_user.username.already_used</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint> -->
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">emailCanonical</option>
<option name="errorPath">email</option>
<option name="message">fos_user.email.already_used</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</class>
</constraint-mapping>
How can I reproduce this change in the overriden FOSUserBundle, or the overriden SonataUserBundle ?
I have a solution, but don't know if it will fit your needs.
You can change de validation groups name in your config.yml for fos_user.
For example :
fos_user:
...
registration:
...
form:
validation_groups: [YourBundleNameRegistration, Default] #before it was [Registration, Default]
profile:
...
form:
...
validation_groups: [YourBundleNameProfile, Default] #before it was [Profile, Default]
Then the validation constraints defined in /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/validation/orm.xml will not applied anymore.
You have to copy the original orm.xml, and paste it in your YourBundle/Resources/config directory. And in this copy you replace Registration and Profile by YourBundleNameRegistration and YourBundleNameProfile. And then you remove the unique constraint on usernameCanonical.
As much as the answer from #Nico pointed me to the right direction, here is the complete setup I've had to do in order to integrate it into the SonataUserBundle and have my custom validation groups taken into account.
As a matter of fact, it seemed that my custom orm.xml file was not loaded. So after changing the validation groups, no more validation occurred at all.
1. Create a file orm.xml
In Application\Sonata\UserBundle\Resources\config\validation\:
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="FOS\UserBundle\Model\User">
<!-- <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">usernameCanonical</option>
<option name="errorPath">username</option>
<option name="message">fos_user.username.already_used</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint> -->
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">emailCanonical</option>
<option name="errorPath">email</option>
<option name="message">fos_user.email.already_used</option>
<option name="groups">
<value>CustomRegistration</value>
<value>CustomProfile</value>
</option>
</constraint>
</class>
</constraint-mapping>
2. Tell FOSUserBundle and SonataUserBundle
To use the custom validation groups (I've stripped down the non-relevant configuration lines):
app/config/fos/fos_user.yml:
fos_user:
registration:
form:
validation_groups:
- CustomRegistration
profile:
form:
validation_groups:
- CustomProfile
app/config/sonata/sonata_user.yml:
sonata_user:
profile:
form:
validation_groups:
- CustomProfile
3. Tell the compiler to load the "overriden" validation file
Add Application\Sonata\UserBundle\DependencyInjection\Compiler\ValidationPass.php class. This is based on the original class from FOSUserBundle:
class ValidationPass implements CompilerPassInterface
{
/**
* {#inheritDoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('fos_user.storage')) {
return;
}
$storage = $container->getParameter('fos_user.storage');
if ('custom' === $storage) {
return;
}
$validationFile = __DIR__ . '/../../Resources/config/validation/' . $storage . '.xml';
if ($container->hasDefinition('validator.builder')) {
// Symfony 2.5+
$container->getDefinition('validator.builder')
->addMethodCall('addXmlMapping', array($validationFile));
return;
}
}
}
Edit Application\Sonata\UserBundle\ApplicationSonataUserBundle:
class ApplicationSonataUserBundle extends Bundle
{
/**
* {#inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ValidationPass());
}
}
4. Override the default validation groups in the UserAdmin class of Sonata (within an overloaded UserAdmin class)
/**
* {#inheritdoc}
*/
public function getFormBuilder()
{
$this->formOptions['data_class'] = $this->getClass();
$options = $this->formOptions;
$options['validation_groups'] =
(!$this->getSubject() || is_null($this->getSubject()->getId())) ? 'CustomRegistration' : 'CustomProfile';
$formBuilder = $this->getFormContractor()->getFormBuilder( $this->getUniqid(), $options);
$this->defineFormBuilder($formBuilder);
return $formBuilder;
}
I don't know if there was a simpler way to achieve this. Nevertheless, it works for me.
How can I make my html Button
(like <button type="button" class="btn btn-warning btn-xs">delete</button>)
call a Controller Action like deleteAction($project)
from within my twig code?(or with java script)
In twig template
Delete
On your controller
/**
* #param User $entity
*
* #Route("/{id}/entity-remove", requirements={"id" = "\d+"}, name="delete_route_name")
* #return RedirectResponse
*
*/
public function deleteActionName(User $entity)
...
see http://symfony.com/doc/current/book/templating.html#linking-to-pages
Home
I'm having trouble writing a Symfony 2 functional test to set checkboxes that are part of an array (i.e. a multiple and expanded select widget)
In the documentation the example is
$form['registration[interests]']->select(array('symfony', 'cookies'));
But it doesn't show what html that will work with and it doesn't work with mine. Here is a cutdown version of my form
<form class="proxy" action="/proxy/13/update" method="post" >
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_1" name="niwa_pictbundle_proxytype[chronologyControls][]" value="1" />
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_2" name="niwa_pictbundle_proxytype[chronologyControls][]" value="2" />
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_3" name="niwa_pictbundle_proxytype[chronologyControls][]" value="3" />
</form>
Once it get it working there I'm going to move on to a manually made form
<input type="checkbox" id="13" name="proxyIDs[]" value="13">
<input type="checkbox" id="14" name="proxyIDs[]" value="14">
<input type="checkbox" id="15" name="proxyIDs[]" value="15">
I have tried things like
$form = $crawler->selectButton('Save')->form();
$form['niwa_pictbundle_proxytype[chronologyControls]']->select(array('3'));
$form['niwa_pictbundle_proxytype[chronologyControls][]']->select(array('3'));
but the first fails saying select is being run on a non-object and the second says Unreachable field "".
Try
$form['niwa_pictbundle_proxytype[chronologyControls]'][0]->tick();
It indexes it from 0 even in the form it says []
Or if it doesn't really helps you, you can try POSTing an array directly to the action instead of using symfony's form selectors. See: Symfony2: Test on ArrayCollection gives "Unreachable field"
Hope one of them helps you.
I think the most bulletproof solution working in 2017 is to extend your test class:
/**
* Find checkbox
*
* #param \Symfony\Component\DomCrawler\Form $form
* #param string $name Field name without trailing '[]'
* #param string $value
*/
protected function findCheckbox($form, $name, $value)
{
foreach ($form->offsetGet($name) as $field) {
$available = $field->availableOptionValues();
if (strval($value) == reset($available)) {
return $field;
}
}
}
And in the test call:
$this->findCheckbox($form, 'niwa_pictbundle_proxytype[chronologyControls]', 3)->tick();