1

I need to create field that as option gets regex string.

So, i made PatternType field:

public function getDefaultOptions(array $options)
{

    $defaultOptions = array(
        'data'              => null,
        'data_class'        => null,
        'trim'              => true,
        'required'          => true,
        'read_only'         => false,
        'max_length'        => null,
        'pattern'           => null,
        'property_path'     => null,
        'by_reference'      => true,
        'error_bubbling'    => false,
        'regexp'            => false,
        'error_mapping'     => array(),
        'label'             => null,
        'attr'              => array(),
        'invalid_message'   => 'This value is not valid',
        'invalid_message_parameters' => array()
    );

    $class = isset($options['data_class']) ? $options['data_class'] : null;

    // If no data class is set explicitly and an object is passed as data,
    // use the class of that object as data class
    if (!$class && isset($options['data']) && is_object($options['data'])) {
        $defaultOptions['data_class'] = $class = get_class($options['data']);
    }

    if ($class) {
        $defaultOptions['empty_data'] = function () use ($class) {
            return new $class();
        };
    } else {
        $defaultOptions['empty_data'] = '';
    }


    $patt = $options['regexp'];

    unset($options['regexp']);

    $defaultOptions['validation_constraint'] = new Regex(
                                                      array(
                                                          'pattern' => $patt,
                                                          'match' => true,
                                                          'message' => 'Niewłaściwy format'
                                                           )
                                                        );


    var_dump($defaultOptions);


    return $defaultOptions;
}

var_dump returns well formatted settings array, with regex object within - but when form is generated validation doesn't work - pass any value. Any idea why?

2 Answers 2

1

Why are you doing this? There is a regex validator already. Just use a normal text field with that validator.

In case you need a form without a model class to bind to, read the corresponding section.

Sign up to request clarification or add additional context in comments.

5 Comments

I've done this above: created field that extends Abstract Type and returned array of default options with validator_constant = new Regex(params).
(continuation of previous post) What I'm doing is aplication that generates form based on xsd files. At first we translate it to php static classes with attributes for fields (like pattern, or type) writed in annotations. Every xsd file has quite big tree like structure, that includes other files, with their attributes. Obviously I needed some custom fields to validate for example Polish tax payer number (we have pattern written as attribute for field). Problem is that dosent work.
...And annotations that we make are not symfony2 validation annotations - we pass much more info through that, so form is created by formbuilder->add();
Are you sure you need a form for this? You can use validators without a form.
Yeah, client pays for website that allows you to send your income statement to tax office so probably i will need forms for that ;) but government was so nice to publish those statements with their sequestration validation in xsd files.
0

Ok, I found what was wrong - you can only add validator constant to root form object (others symfony simply ignore). So it seems that what I need is simply get root form, add there validator_constant with validator_group option set. Then just assign field proper validator_group.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.