Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)
#61

[eluser]OverZealous[/eluser]
@Mareshal
I don't know if this will help, but you need to check the return of the save function, every time. It should always look like this:
Code:
if($u->save()) {
    // Everything was saved OK
} else {
    // An error occurred, check $u->error
}

I know the examples in the manual aren't very clear about this, but the example application is better. Even if you don't use the HTMLForm extension, the pattern is the same:
Code:
function edit($id) {
    $o = new Object($id);
    if(isset($_POST['submit'])) {
        $o->value1 = $_POST['value1'];
        if($o->save()) {
            redirect('wherever');
        }
    }
    $this->load->view('edit', array('object' => $o));
}

Then, in your view, you can handle showing errors. If the item hasn't been edited yet (ie: not submitted), there are no errors. If it was submitted and there were no errors, the page is redirected. If it was submitted and there are errors, then you can see them in $o->error.

The SQL UPDATE is never run before the validation. Validation is the first thing the save function does.
#62

[eluser]Buso[/eluser]
is it possible to use this with an already existing auth library without any conflict?
#63

[eluser]OverZealous[/eluser]
@Buso
DMZ is not an authorization library. It's an ORM. But I can't answer yes or no, you just have to set them up and try it.
#64

[eluser]NachoF[/eluser]
Im having a little problem with validation... here's my code

Code:
if ($this->upload->do_upload("url"))
                    {
                         $data =$this->upload->data();
                         $a->url=$data['file_name'];
                    }
                    else
                    {
                        $a->error_message('custom', $this->upload->display_errors());
                    }
                    
                    if($a->save($t))
                    {
                        $this->session->set_flashdata('success', 'Tip Creado');
                        redirect("Welcome");
                    }
                    //$a->error_message('custom', $this->upload->display_errors());  If I use it this way it works but I would preffer adding the error right after I catch it

So want I want to do is add to the model validation errors the file upload error.. the problem is that (apparently) all errors get deleted when the save method is called... so if I set the error afterwards it does work cause it doesnt get deleted
#65

[eluser]OverZealous[/eluser]
@NachoF

Why are you trying to save the object if there's an error uploading?

Code:
if ($this->upload->do_upload("url")) {
    $data = $this->upload->data();
    $a->url = $data['file_name'];
    if($a->save($t)) {
        $this->session->set_flashdata('success', 'Tip Creado');
        redirect("Welcome");
    }
} else {
    $a->error_message('custom', $this->upload->display_errors());
}

Otherwise, what you are trying to do doesn't make sense, anyway: if there are any validation errors, DMZ won't save the object. This means, if you set an error manually, (and the errors weren't cleared) DMZ would never save. (That's why it clears the errors before validating.)
#66

[eluser]NachoF[/eluser]
I try to save anyway cause I want all the errors listed in the form.... the form has many input text fields and the userfile to upload... I want the form to list all of the errors and the upload error as well..... with your code I will only get the upload error and not the rest of the errors of the form in the case that it couldnt upload the file.
#67

[eluser]Mareshal[/eluser]
Why not create a function for every step? One to check all errors, one to check upload errors, one to save the data and one to render the page back. Complicated?
#68

[eluser]NachoF[/eluser]
Why would I do that at all?? have you used datamapper error validation???... it does all of that already.. the only problem Im having is when adding a custom error.. cause when the save method is called it deletes every error for that object.
#69

[eluser]Conerck[/eluser]
[quote author="NachoF" date="1269213266"]I try to save anyway cause I want all the errors listed in the form.... the form has many input text fields and the userfile to upload... I want the form to list all of the errors and the upload error as well..... with your code I will only get the upload error and not the rest of the errors of the form in the case that it couldnt upload the file.[/quote]

You could try this

Code:
// This is mostly Phil's code
if ($this->upload->do_upload("url")) {
    $data = $this->upload->data();
    $a->url = $data['file_name'];
    if($a->save($t)) {
        $this->session->set_flashdata('success', 'Tip Creado');
        redirect("Welcome");
    }
} else {
    // If the upload failed, this will validate the fields without saving the object
    $a->validate();
    // After validate has run you can append your custom errors
    $a->error_message('custom', $this->upload->display_errors());
}
#70

[eluser]NachoF[/eluser]
Thank you that works.




Theme © iAndrew 2016 - Forum software by © MyBB