We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon validation 2 fields and concat

Hi,

I have 2 input fields, the first input is a prefix and the second is the number, example : prefix -> AB, number ->005.

I pass this parameter by post and server side I'll concat this data separated by - for example : AB-005.

I want to add to my custom validation class the presence of the prefix field and after the number field. And I want to add the Uniqueness validation on the full string AB-005to check if I have this in my database. Soooo... I did that

$this->add(
    'prefix',
    new PresenceOf(
        array(
            'message' => 'The prefix is required'
        )
    )
);

$this->add(
    'number',
    new PresenceOf(
        array(
            'message' => 'The number is required'
        )
    )
);

$this->add(
    'prefix'.'-'.'number',  // <--------- I don't know how I can do that...
    new Uniqueness(
        array(
            'model' => 'Users',
            'message' => 'This data should be unique'
        )
    )
);

Do you know how I can do to concat the prefix and number data to add the Uniqueness validation ?

Edit : replaced $validation by $this

edited May '16

So you bascially have this validation in model right ? What is exactly a problem ? Just set value of this field in model in backend - that's it.

This is not in my model, this is in a personnal class Validation who extends Validation Phalcon\Validation\Validator;. The first parameter is the POST data sended by AJAX. I use this to get the validation $messages = $validation->validate($this->request->getPost());.

I have to concat prefix and number data.



11.6k

as Woljciech says, you can do the concatenation and final validation in your model file, i.e. using Phalcon\Mvc\Model::beforeSave(), Phalcon\Mvc\Model::beforeCreate() or Phalcon\Mvc\Model::beforeUpdate()

edited May '16

Then you just need to create in this post data this field :

'prefix'.'-'.'number'

with correct value from those others fiels. I never used uniquness just as custom validation. Not sure how what it expects if it's not used with model. Maybe just namespace of model and it will do check ?