[html-formfu] Validate DBIx::Class unique constraints

Tim Rayner tfrayner at gmail.com
Mon Dec 6 11:22:19 GMT 2010


On 6 December 2010 11:15, Carl Franks <fireartist at gmail.com> wrote:
> On 6 December 2010 11:09, Tim Rayner <tfrayner at gmail.com> wrote:
>> On 6 December 2010 10:58, Carl Franks <fireartist at gmail.com> wrote:
>>> On 6 December 2010 10:47, Erez Schatz <moonbuzz at gmail.com> wrote:
>>>> On 12/03/2010 01:35 PM, Carl Franks wrote:
>>>>
>>>>> There's also a HTML::FormFu::Constraint::DBIC::Unique
>>>>
>>>> Thanks, its a good solution, however...
>>>>
>>>>> I'm not sure what you mean by "this doesn't work if the user enters
>>>>> his own information", can you give an example?
>>>>
>>>> When a user profile page loads, it is populated with current
>>>> information. If a user clicks on "submit" it then submit all information
>>>> that is in the form. Any "Unique" test will fail, seeing as the
>>>> information is already in the DB - under the user's name!
>>>>
>>>> Is there a way to tell HTML::FF "validate only values that have been
>>>> changed"?
>>>
>>> Ah, I getcha.
>>> I think the solution here would be to use different forms for the "add
>>> profile" and the "edit profile" functions.
>>> The first has the Unique constraint, the second doesn't.
>>>
>>> An easy way to reuse the same basic form, if the only difference is
>>> the constraint, would be something like this:
>>>
>>> edit.yml
>>> ===
>>> elements:
>>>    - name: username
>>>
>>>
>>> add.yml
>>> ===
>>> load_config_file: 'edit.yml'
>>> constraints:
>>>    - type: 'DBIC::Unique'
>>>      name: username
>>>
>>> Will this work for you?
>>> Carl
>>
>> Hi,
>>
>> I've been using just a single form for both add and edit, using the
>> self_stash_key option from H::F::C::DBIC::Unique. The idea is that you
>> put the DBIC row object being edited onto the Catalyst stash and then
>> name that stash key in the form config. The uniqueness constraint will
>> ignore this one object if it's on the stash. A simplified (but
>> working) example from our project:
>>
>>  - type: Text
>>    name: date
>>    label: Visit Date
>>    constraints:
>>      - Required
>>      - type:    DBIC::Unique
>>        resultset:   Visit
>>        self_stash_key:  object
>>        message: This visit already exists
>>        others:  patient_id
>>
>> In this case the 'visit' object being edited is put on the stash under
>> the 'object' key (this example also illustrates including 'patient_id'
>> as a unique constraint, alongside 'date'; i.e. only one visit per
>> patient per date). There are formatting constraints I've omitted here
>> for the sake of clarity.
>
> Thanks Tim!
> I'd only just found this in the source, when your email came in :)
>
> btw, it looks like the object needs to be on the form's stash, not the
> Catalyst stash.

You're quite correct, my mistake.

Tim



More information about the HTML-FormFu mailing list