Domanda

Voglio validare un campo di modulo per l'URL. Ho impostato il valore predefinito per il campo a http: //. Quando l'utente non immette un URL, e se ne va il http: //, si dice URL non valido. URL non è richiesto, quindi se è solo http: //, non dovrebbe mostrare il messaggio di errore. Come posso farlo ignorare se le sottomette persona http: // come URL

Grazie

È stato utile?

Soluzione

http:// is not a valid url, so if you want to allow it anyway there are 2 options

  1. create a custom validation rule, that returns true for a real url + http://
  2. use the before validate callback and set the url field to blank if it is just http://

hope that helps

EDIT

I forgot: you don't need to set required but 'allowEmpty' => true

required -> the form must contain a field xyz that is send to the server

allowEmpty-> a field may be blank


I added the link to the callback function above, but anyway .. here it is:

in your model class (I just suppose it's User):

class User extends AppModel {
    ..

    function beforeValidate() {
        if (isset($this->data['User']['url']) && $this->data['User']['url'] == 'http://') {
            $this->data['User']['url'] = '';
        }
        return true;
    }
    ..
}

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top