0
votes

I have a set of validation rules I am imposing on my model in Cakephp 2.0 and since they are all for fields that deal with web addresses, they are exactly the same. Cut and pasted exact. However, when I modify my db in my edit form, only the first rule works; all the others validate and update my record. Here is my model code:

public $validate=array(
    'unit_website'=>array(
        'rule'=>'url',
        'message'=>'You must enter a valid website address.'
    ),
    'rates'=>array(
        'rule'=>'url',
        'message'=>'You must enter a valid website address.'
    ),
    'book'=>array(
        'rule'=>'url',
        'message'=>'You must enter a valid website address.'
    ),
    'contact'=>array(
        'rule'=>'url',
        'message'=>'You must enter a valid website address.'
    )
);

UPDATE* Here is my form:

<?php    
echo $this->Form->create('Unit', array(
    'action' => 'edit',
    'inputDefaults' => array(
        'label' => false,
        'div' => false
        )
    )
);
echo $this->Form->hidden('id');
echo $this->Form->hidden('location_id');
echo $this->Form->hidden('Location.id');
echo $this->Form->hidden('Location.lat');
echo $this->Form->hidden('Location.lon');
echo $this->Form->input('Location.prop_name', array('label'=>'Property name where your unit is located', 'div'=>true));
if($this->data['Unit']['type']=='condo' || $this->data['Unit']['type']=='house') {
echo $this->Form->input('Unit.unitnum', array('label' => 'Unit\'s Name/Number'));
}
echo $this->Form->input('type', array('label'=>'Type of Property',    'onChange'=>'toggle()', 'options'=>array('condo'=>'Condo', 'house'=>'House',   'hotel'=>'Hotel or Motel', 'rentalco'=>'This is a rental company')));
 echo $this->Form->input('Location.address', array('onChange'=>'getLatLong(address)',  'size'=>'50', 'div'=>true));
echo $this->Form->input('Location.city', array('onChange'=>'getLatLong(address)'));
echo $this->Form->input('Location.state', array('onChange'=>'getLatLong(address)',  'size'=>'2'));
echo $this->Form->input('Location.zip', array('size'=>'5'));
echo '<br />';
echo $this->Form->input('Location.area_code', array('size'=>'3'));
echo $this->Form->input('Location.exchange', array('size'=>'3'));
echo $this->Form->input('Location.sln', array('size'=>'4'));
echo '<br />';
echo $this->Form->input('unit_website', array('size'=>'65', 'label'=>'Your unit\'s   website', 'error' => array('class' => 'error')));
echo '<br />';
echo $this->Form->input('specials', array('label' => 'Your website\'s Specials page',  'size'=>'65', 'error' => array('class' => 'error')));
echo '<br />';
echo $this->Form->input('rates', array('size'=>'65', 'error' => array('class' =>  'error'), 'label'=>'Your unit\'s rates page'));
echo '<br />';
echo $this->Form->input('book', array('size'=>'65', 'error' => array('class' =>  'error'), 'label'=>'Your unit\'s booking page or calendar'));
echo '<br />';
echo $this->Form->input('contact', array('size'=>'65', 'error' => array('class' =>  'error'), 'label'=>'Your unit\'s contact or email page'));
echo '<br />';
if($this->data['Unit']['type']=='condo' || $this->data['Unit']['type']=='house') {
echo '<br /><div id="unit_info">';
echo $this->Form->input('sq_ft', array('label'=>'Square feet', 'min'=>'1000',  'max'=>'10000', 'step'=>'50'));
echo $this->Form->input('bedrooms', array('label'=>'Bedrooms', 'min'=>'1', 'max'=>'15', 'step'=>'1'));
echo $this->Form->input('baths', array('label'=>'Bathrooms', 'min'=>'1', 'max'=>'10', 'step'=>'.5'));
echo $this->Form->input('sleeps', array('label'=>'How many people does your unit sleep?', 'min'=>'1', 'max'=>'45', 'step'=>'1'));
echo '</div><br />';
echo $this->Form->input('unit_desc', array('div'=>true, 'cols'=>'150', 'rows'=>'4',  'label'=>'Describe your unit <span style="font-size:14px; font-style:italic">(Remember to  include the sizes of beds in each bedroom, whether you have a sleeper sofa, deluxe kitchens  and baths, balconies and their views, access to amenities, and any other items that  distinguish your property.)</span>'));
}
else {
echo '<br />';
echo $this->Form->input('unit_desc', array('div'=>true, 'cols'=>'150', 'rows'=>'4',  'label'=>'Describe your hotel or rental company'));
}
echo '<br />&nbsp;';
echo $this->Form->end('Update Property');?>
<?php echo $this->Session->flash(); ?>

Does Cake have some sort of "you can only make this rule once" rule? Or do my model field names have to actually have the word website in them or something? Or have I just made some dumb syntax error?

UPDATE Here is my controller code:

function edit($id) {
$this->set('title', 'Edit your property');
$this->Unit->id = $id;    
if (empty($this->data)) {        
$this->data = $this->Unit->read();    
} else { 

    if ($this->Unit->saveAll($this->data)) {            
        $this->Session->setFlash('Your property has been updated.', 'success');            

        }   
}
}
1
Will you show the controller and then entire form? For example, I would like to see your form->create() call in the view. I would also like to see the controller method for handling the edit. - Chuck Burgess

1 Answers

1
votes

What is the URL you are validating and what are you expecting? The url validation will not check for protocol unless you specify it. If you want to make sure http is in the url, you should specify your rule like this:

'rule' => array('url', true)

Please show sample URLs that are passing validation but are not valid.

UPDATE

$this->data should be changed to $this->request->data. This may not change the validation process. But $this->data is deprecated.

Try adding a beforeValidate callback in the Unit model and echo $this->data to see what is coming through.