-3

I am currently validating a URL using the Regex Pattern and it appears to be working correctly. However, if I leave the URL field blank, it should not check the Regex Validation or perhaps just return a message like "No URL given".

Here is an example of my current code I'm working with:

array(
    'name'       => 'programurl1',
    'attributes' => array(
        'type'      => 'text',
        'error_msg' => 'Enter Valid Program URL 1',
        'label_msg' => 'Program URL 1 *'
    ),
    'validation' => array(
        'required'   => true,
        'filters'    => array(
            array(
                'name' => 'StripTags'
            ),
            array(
                'name' => 'StringTrim'
            )
        ),
        'validators' => array(
            array(
                'name'    => 'Regex',
                'options' => array(
                    'pattern' => '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/'
                )
            )
        )
    )
)

I'm not certain how to accomplish what I am looking for when the URL field is blank.

Diemuzi
  • 3,507
  • 7
  • 36
  • 61
Shahbaz
  • 3,433
  • 1
  • 26
  • 43
  • I don't even see a `?`. Questions generally have those. – pguardiario Sep 26 '13 at 13:35
  • 2
    i m asking that can i add another validaor in the same array? in generally i want that if field is empty Regex validaor not to be executed. – Shahbaz Sep 26 '13 at 13:48
  • You are asking question regarding, removing validation from URL and yet you posted form that is showing a text field? doesnt make sense to me? if you can rephrase question or provide relevent code i may understand. – noobie-php Sep 26 '13 at 14:26
  • @noobie-php So you're not using text-inputs for a url?... He wants to know how to allow empty urls (i.e. no url) – Sam Sep 26 '13 at 14:38
  • @noobie-php i have edit the Question, i think now it will be clear to you. – Shahbaz Sep 26 '13 at 14:40
  • 1
    @Shahbaz: try using something like this $filter->remove('programurl1'); $filter->add(array( 'name' => 'programurl1', 'required' => true, 'validators' => array ( 'stringLength' => array ( 'name' => 'StringLength', 'options' => array ( 'max' => '3', ), ), ), )); its just an example you can modify it too lets say you want to remove required tue you can set it to false here, you can also change other things like strip tags etc, the idea is simply to remove an element from Filter so it should not be validated, and then re-apply validation, obviuosly should be conditional – noobie-php Sep 26 '13 at 14:42
  • 3
    I don't know ZF, but isn't it a simple change from `'required'=>true,` to `'required'=>false,`? – dev-null-dweller Sep 26 '13 at 20:46
  • @dev-null-dweller: reuired false will not validate any thing i mean it is possible to pass alot of ugly data via it, so its used as in worse case. – noobie-php Oct 01 '13 at 15:22
  • 'required'=>false, is a good approach for me and i konw it already. why you have vote down? – Shahbaz Oct 02 '13 at 05:51

1 Answers1

0

Instead of a type text, you can use the url type. That is specifically meant to enter url values:

$this->add(array(
    'name' => 'programurl',
    'type' => 'Zend\Form\Element\Url',
    'options' => array(
        'label' => 'Program URL 1'
    ),
    'attributes' => array(
        'required' => 'required'
    )
));

The url element is a special HTML5 element, see also the docs.

Zend\Form\Element\Url is meant to be paired with the Zend\Form\View\Helper\FormUrl for HTML5 inputs with type url. This element adds filters and a Zend\Validator\Uri validator to it’s input filter specification for validating HTML5 URL input values on the server.

Afaik if the browser cannot render the url input element, it just shows the text input as a fallback.

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99