Validate attribute at controller

Permalink Browser Info Environment
hi,
i created an attribute and i want users to fill this attribute in the add/edit form.
i wonder how can i get the value entered by user in attribute textfield at the validate method?
i mean i want to check if user fill the attribute textfield.

Type: Discussion
Status: New
shahroq
View Replies: View Best Answer
JohntheFish replied on at Permalink Reply
JohntheFish
Hi Sharoq

As you pointed me here I thought I would have a look.

I am not sure what your question means. Is it the attribute add/edit form you are referring to? Or a block add/edit form where you are also editing some attributes?

When I have created attributes that needed validation, I have always done it in the browser in jQuery, but I have not done anything of this sort for at least a year, so its not fresh in my mind.

For that sort of add/edit validation, I usually put the script directly into the add/edit form and usually attach a click event to the save button that returns true or false to allow the click to propagate.

John
shahroq replied on at Permalink Reply
shahroq
Thanx john for answering, i've always use jquery validation for this kind of data, but there is this attribute that need to validate on server side, also i want to use the value for modifying page slug url. so i should get the value at the controller.
I was struggling for awhile, but no luck.
as andrew addon suggest this lines of code saves the data:

Controller:
private function saveData($p) {
   Loader::model("attribute/categories/collection");
   $cak = CollectionAttributeKey::getByHandle('my_attribute');
   $cak->saveAttributeForm($p);
}

View:
<!-- 'my_attribute' attribute -->
<?php
   Loader::model("attribute/categories/collection");
   $akt = CollectionAttributeKey::getByHandle('my_attribute');
   if (is_object($sc)) {
      $tvalue = $sc->getAttributeValueObject($akt);
   }
?>
<div class="sc-attributes clearfix">
   <?php  echo $akt->render('label') ?>
      <div class="input">
         <?php  echo $akt->render('form', $tvalue, true) ?>
      </div>
</div>

but i could not get it directly.
JohntheFish replied on at Permalink Reply
JohntheFish
Maybe unpicking an address attribute will give some ideas.
shahroq replied on at Permalink Reply
shahroq
Thanx john, i dig into address attribute, couln't find anything.
i come with a way for now, but it's not a cool way to do it.
i found AttributeKeys.akID of mentioned attribute from db, and got the value at controller with this code:
$tmp = $this->post('akID');
var_dump($tmp[144]["value"]);

it worked for now, but i hope someone guide me a through a decent approach.
mkly replied on at Permalink Reply
mkly
Hi @sharoq,
I'm getting some stuff wrapped up, but I'll get you some info a little later as this is a bit of a more complex topic and I want to make sure I don't give you any inaccurate information.

Best Wishes,
Mike
mkly replied on at Permalink Best Answer Reply
mkly
Ok, this gets a little confusing fast, so I'll do my best to point you in the right direction.

https://github.com/concrete5/concrete5/blob/master/web/concrete/core...

That is the logic that extracts the values from $_POST. You'll notice from that, that the attribute needs to know it's ID for this to work so what you do is get the attribute key and create the controller instance with it.

This is a bit of a code dump but I think it walks you through it a bit
<?php
// So first we'll get the Attribute Key
$cak = CollectionAttributeKey::getByHandle('meta_title');
// now a quick form
echo '<form method="post">';
// The attribute renders it's own form.
// This is important because it sets the appropriate
// value for the akID etc
$cak->render('form');
echo Loader::helper('form')->submit('submit', 'submit');
echo '</form>';
// Now we'll get the Attribute Type
$at = $cak->getAttributeType();
var_dump($at);
// Then the AttributeTypeController


That's probably not the most succinct method but hopefully it gets you started.

EDIT: I should add that you should likely toss this in a tools file to use.

Best Wishes,
Mike
shahroq replied on at Permalink Reply
shahroq
Thanx Mike,
It's kinda complicated, but it seems working.

btw, i create this post, i press 'monitor' button and subscribed to this topic, still did not get any email when someone replay to this one, maybe it's a bug or something?
mkly replied on at Permalink Reply
mkly
Indeed, part of it is that the attribute controls most of this internally. It takes a little getting used to and some source code reading but once you get used to the idiom its not too bad.

Best Wishes,
Mike

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.