Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Adding a validator a custom manipulator [RSS Feed]

#1 Nov. 30, 2005 15:37:00

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


I'm trying to validate a file upload field to make sure that the users
uploaded file is of a specific type. I know this can be done with a
custom manipulator (and in turn a custom validator). However, I still
want to be able to use the default manipulator for my object for the
rest of the parts of the form.

Is this a case of me trying to have my cake and eat it too?

Im' thinking this would work, but I am wondering if this is the "wrong"
way to go about this....

....
manipulator = my_model.AddManipulator()
manipulator2 = my_model.CustomManipulator()

if request.POST:
new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data) +
manipulator2.get_validation_errors(new_data)
.....
#Not sure exactly how to do the following
form = formfields.FormWrapper(manipulator, new_data, errors??)
.....



Thanks,

Babak

Offline

#2 Nov. 30, 2005 15:49:39

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


On 11/30/05, bsoltani <> wrote:
> I'm trying to validate a file upload field to make sure that the users
> uploaded file is of a specific type. I know this can be done with a
> custom manipulator (and in turn a custom validator). However, I still
> want to be able to use the default manipulator for my object for the
> rest of the parts of the form.
>
> Is this a case of me trying to have my cake and eat it too?

If you add a "validator_list" parameter to the appropriate field in
your model, the automatic manipulator will take care of that
validation for you. Would this solve your problem?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Nov. 30, 2005 17:32:54

oggie r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


Hi Babak,
I think you can have the cake. Don't mess with CustomManipulators, just
add this method into your model:
def _manipulator_validate_filefield(self, field_data, all_data):
# if file is not the right type
# raise validators.ValidationError, 'Please enter a valid file
type'

-rob

Offline

#4 Nov. 30, 2005 17:36:52

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


I think I get what your saying... I'm going to check it out and see if
it works.

The "tech support" here is amazing. You are you guys aren't
outsourcing to India?

Offline

#5 Nov. 30, 2005 17:38:19

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


so:


if I add the above method to my model, it is going to get called
automatically when i do object.AddManipulator()?

Offline

#6 Nov. 30, 2005 18:20:43

oggie r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


Yes

Offline

#7 Nov. 30, 2005 18:23:04

oggie r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


Wait, what? I don't think you need to say object.AddManipulator()
Just put that method in your model, and go to the admin interface. It
should work for both adding and changing an object in the model.

-rob

Offline

#8 Nov. 30, 2005 18:28:18

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


rob,

well, i'm doing this in a custom form/view. It's not being done by the
site administrator.

Offline

#9 Nov. 30, 2005 18:44:31

oggie r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


Babak,
It shouldn't matter. Take a look at django.core.meta.__init__

manipulator_methods[k] = v
and
_reassign_globals(manipulator_methods, new_mod,
new_mod.__dict__)
_reassign_globals(manipulator_methods, new_mod,
new_mod.__dict__)

will add the validator methods to your default Manipulators.

But have you tried it? You should be able to at least see if it is
being called with some print statements

-rob

Offline

#10 Nov. 30, 2005 23:09:16

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Adding a validator a custom manipulator


yes, it worked. I hadn't seen that part of the model documentation
about the automatically called validators.

thanks!

Offline

  • Root
  • » Django
  • » Adding a validator a custom manipulator [RSS Feed]

Board footer

Moderator control

Enjoy the 22nd of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support