Welcome Guest, Not a member yet? Register   Sign In
basic but....
#15

[eluser]nmweb[/eluser]
You pass values to your model, your model should have no knowledge of $_POST nor should it do something with it. Your controller should give the model the values.

(primitive state)
Code:
class Some_Model{
protected $fields=array('email');

protected $validate=array(
'field_name'=>array('rules'=>array('required','email'))
);

public function save()
{
   if($this->validate())
   {
      //db insert here with values from $this->field array
return true;
   }
return false;
}
public function validate()
{ $validate=true;
    foreach($this->fields as $field)
    {
    //iterate over rules and what not
      if(!$this->is_valid($field)
      {
       $validate=false;
      }
    }
   return $validate;
}
public function is_valid($field)
{
//get rules for field and validate it
}
public function get_errors(){}
}
In the model above you can handle form field/db field differences with some property of the object.

Now, what does the controller look like:
Code:
//controller
$model=new Some_Model;
$model->email='some wrong email';
if($model->save())
{
echo 'correct mail';
}
else
{
$model->get_errors();
}

To show how Django handles it and how I plan to handle it:
Code:
//bit of pseudo code
class Email_Form extends Form_generation_class{
   protected $form_fields=array('email);
}

$form=new Email_Form(new Some_Model);
echo $form->render();
That's it. It will generate a form based on the model with proper validation including nice error messages and javascript validation. Say you want to edit an existing record.
Code:
$form=new Email_Form(new Some_Model(10); //record number 10
echo $form->render();
if($form->save()) //validates then tries to save the model
{
}
That's it in a nutshell.


Messages In This Thread
basic but.... - by El Forum - 03-11-2008, 11:46 AM
basic but.... - by El Forum - 03-11-2008, 11:57 AM
basic but.... - by El Forum - 03-11-2008, 04:24 PM
basic but.... - by El Forum - 03-12-2008, 03:30 AM
basic but.... - by El Forum - 03-12-2008, 03:41 AM
basic but.... - by El Forum - 03-12-2008, 04:36 AM
basic but.... - by El Forum - 03-12-2008, 04:39 AM
basic but.... - by El Forum - 03-12-2008, 05:00 AM
basic but.... - by El Forum - 03-12-2008, 06:30 AM
basic but.... - by El Forum - 03-12-2008, 06:55 AM
basic but.... - by El Forum - 03-13-2008, 02:25 AM
basic but.... - by El Forum - 03-13-2008, 02:58 AM
basic but.... - by El Forum - 03-13-2008, 04:11 AM
basic but.... - by El Forum - 03-13-2008, 05:04 AM
basic but.... - by El Forum - 03-13-2008, 06:13 AM
basic but.... - by El Forum - 03-13-2008, 06:41 AM
basic but.... - by El Forum - 03-13-2008, 07:12 AM
basic but.... - by El Forum - 03-13-2008, 02:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB