Image

Imagebobalien wrote in Imagecakephp

validating models without a database table

occasionally, elements of my cake apps call for forms to be validated that aren't linked to a database table - for instance a Contact form that emails the form to a certain email address, as opposed to logging contacts in a database. You would probably want to be sure that the user has filled in all of the necessary information before creating and sending the email.



my contact model: contains a validate array for standard cake validation

To use a model without a corresponding database table, you use $Model->useTable = false;...

<?php

class Contact extends AppModel 
{
	var $name = 'Contact';
	var $useTable = false;
	
	var $validate = array(
		'name' => VALID_NOT_EMPTY,
		'email' => VALID_EMAIL,
		'message' => VALID_NOT_EMPTY
	);
}

?>


my contact controller

<?php

class ContactController extends AppController 
{
	var $name = 'Contact';
	var $uses = array('Contact');
	var $components = array('Email','Output'); // uses Email and Output components from rdOS

	function index()
	{
		
		if(empty($this->data))
		{
			// no data has been passed to page from form
			$this->render();
		}
		else 
		{
			// data has been passed from form
			
			// validate model using ->validates() not ->save()
			if($this->Contact->validates($this->data))
			{
				// Validation passed
				$this->Sanitize->cleanArray($this->data);
				
				$defaultLayout  = $this->layout;
				$this->layout = 'email';
				$this->Email->headers = "Content-type: text/plain; charset=iso-8859-1\n";
				$this->Email->to = "email@server.com";
				$this->Email->from = $this->data['Contact']['name'].' <'.$this->data['Contact']['email'].'>';
				$this->Email->subject = 'website contact';
				$this->Email->thtml = 'email_layout';
				$this->Email->controller = &$this;
				$this->set('data',$this->data);
				$this->Email->send();				

				$this->set('sent',1);
				$this->render('index',$defaultLayout);
			}
			else 
			{
				// Validation failed
				$this->validateErrors($this->Contact); // for triggering $html->tagErroMsg()
			}
		}
		
		/*					
		$this->set('email', $this->data['User']['email']);
		$this->set('password', $passwordOrig);

		//Send email
		

		*/
		
	}
	
}

?>


views/contact/index.thtml - my contact form view file

<div class="page-content" id="contact">
<div id="page-header-img"><?php echo $html->image('layout/header-generic-sm.jpg',array('alt'=>'contact us'))?></div>

<?php if(isset($sent)): ?>

<fieldset id="left" style="height: 300px;">
	
	<div class="set-row" id="header">
		<div class="label"><label>Thank <strong>you</strong></label></div>
	</div>
	
	
	<div class="set-row" id="submit">
		<p align="right">We have received your message.</p>
		<p align="right"> If a response was requested, a member of our staff <br/>will follow up with you as soon as possible.</p>
	</div>
</fieldset>

<?php else: ?>


<fieldset id="left">
	<?php echo $html->formTag('/contact') ?>	

	<div class="set-row" id="header">
		<div class="label"><label>Contact <strong>us</strong></label></div>
	</div>

	<div class="set-row">
		<div class="label"><label>your name:</label></div>
		<div class="input">
			<?php echo $html->input('Contact/name',array('class'=>'text')) ?>
			<?php echo $html->tagErrorMsg('Contact/name','your name is required') ?>
		</div>
		<div class="clear">&nbsp;</div>
	</div>
	
	<div class="set-row">
		<div class="label"><label>email:</label></div>
		<div class="input">
			<?php echo $html->input('Contact/email',array('class'=>'text')) ?>
			<?php echo $html->tagErrorMsg('Contact/email','you must provide a valid email address') ?>
		</div>
		<div class="clear">&nbsp;</div>
	</div>

	<div class="set-row">
		<div class="label"><label>message:</label></div>
		<div class="input">
			<?php echo $html->textarea('Contact/message') ?>
			<?php echo $html->tagErrorMsg('Contact/message','you must write a message') ?>
		</div>
		<div class="clear">&nbsp;</div>
	</div>		
	
	<div class="set-row" id="submit">
		<div class="input"><?php echo $html->submit('send message',array('class'=>'submit')) ?></div>
	</div>
</fieldset>
</form>

<?php endif; ?>

<fieldset id="right">
<?php echo $html->image('layout/contact-inset.jpg') ?>
</fieldset>

<div class="clear">&nbsp;</div>


</div>


/views/contact/email_layout.thtml

Website Contact
-------------------------------------------------------------------------

From: <?php echo $data['Contact']['name'] ?> <?php echo $data['Contact']['email'] ?>


Message:
<?php echo $data['Contact']['message'] ?>


That's about it. I could also use the extra Model->invalidate() validation method discussed in my previous post in my Contact Controller ex.-> $this->Contact->invalidate('name_length')

This isn't anything very complex, but something I run into occasionally.