简体   繁体   中英

Symfony doesn't validate on iphone

I have set an entity with this code

/**
 * @Assert\Length(
 *      min = 20,
 *      max = 2000,
 *      minMessage = "message.min_length",
 *      maxMessage = "message.max_length"
 * )
 */
protected $message;

When I'm using a desktop navigator it refuses to send when the message is less than 20, but when on iphone or tablet (android) the message is sent, even empty. The field is a textarea. Any ideas ?

Update:

According to the symfony documentation, assert length assumes NULL & empty strings as valid, hence why it passes ( http://symfony.com/doc/current/reference/constraints/Length.html#min ).

Please add Assert\\NotBlank() and this will validate the empty strings

--

What the symfony form builder will do is add the form validation options to the form elements, for example:

<input name="my_field" required>

A desktop browser will validate that this form element is required (client side) & if empty, prevent the form being submitted. On mobile devices, they do not validate the forms automatically (I'm still to find a reason for this..?), so you will need to add some javascript form validation to prevent the form from submitted if it's invalid on mobile/tablet devices.

Please make sure that your form is mapped to the entity class correctly:

// AppBundle/Form/EntityForm.php

<?php

namespace AppBundle\Form;

class EntityForm extends AbstractType
{
    // ...

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\MyEntity',
        ]);
    }
}

To ensure symfony validates annotations, please add this to your app/config/config.yml

# app/config/config.yml
framework:
    validation: { enable_annotations: true }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM