give an example of how you can use the model class' custom validation
methods to handle a validation of two fields at the same time.
From the book, check out how to manage custom validations (http://
book.cakephp.org/view/152/Adding-your-own-Validation-Methods), then
look over the code below.
function oneOfTwoNotEmpty($fieldValue, $fieldNameToCompare){
if(!empty($fieldValue)){
return true;
}else{
if(!empty($this->data[$this->name][$fieldNameToCompare])){
return true;
}
}
return false;
}
This code comes directly from a plugin model class I am working on at
the moment, and is used in validation. In this code, really the only
pertinent part for you to see is the function call:
function functionName ($value_of_field_we_are_validating,
$field_name_1, $field_name_2, [etc...]) {
Since you mentioned "two unique fields", you should only need to pass
2 arguments, the current field's value, and the name of the 2nd field
you want to compare (or validate) it with.
The important part within this method that would be helpful to you
would be:
$this->data[$this->name][$fieldNameToCompare]
This line would contain the value of the 2nd field passed to my
method.
Therefore... I'd set this validation on the building_id field, and...
function checkBuildingRoomExists($building_id, $room_field){
if(empty($building_id) || empty($room_field)){
return false;
}
$room_id = $this->data[$this->name][$room_field];
//continue your own checking here with $room_id and $building_id...
//[...code...]
}
Hopefully this will help you get started on your way. Again, be sure
to read the validation section in the book - both the "Multiple
Validation per Field" and the linked section.
On Feb 3, 2:47 am, sebb86 <kahlc...@googlemail.com> wrote:
> Hello,
> my table **rooms** contains three fields: //id//, //building_id//
> and //room//.
>
> //Building_id// and //room// have to be UNIQUE together, because room
> 9 is in building 1 only one time but room 9 can also conained in
> building 2.
> The //building_id// in the view is realized as a drop-down field. So i
> can only choose buildings, which already exists. Now i want to check
> the existence of the "room"-value, which i type in a form, with the
> actual //building_id//.
>
> How can i solve this problem?
>
> I retrieve my data the following way (view:)
> {{{
> <?php foreach ($rooms as $room): ?>
> <tr>
> <td><?php echo $room['Room']['id']; ?></td>
> <td><?php echo $room['Room']['building_id']; ?> (<?php echo $room
> ['Building']['description']; ?>)</td>
> <td><?php echo $room['Room']['room']; ?></td>
> </tr>
> <?php endforeach; ?>
>
> }}}
>
> Thanks ver much if someone can help.
> Greetings :)
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment